Author Topic: GetMainGameViewSize will cause 40B alloc each time which can be avoid.  (Read 3532 times)

beyonddoor

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Hi, I really like NGUI, here's a little improvement.
  1.         Vector2 GetWindowSize ()
  2.         {
  3.                 UIRoot rt = root;
  4. #if UNITY_EDITOR
  5.                 Vector2 size = GetMainGameViewSize();
  6.                 if (rt != null) size *= rt.GetPixelSizeAdjustment(Mathf.RoundToInt(size.y));
  7. #else
  8.                 Vector2 size = new Vector2(Screen.width, Screen.height);
  9.                 if (rt != null) size *= rt.GetPixelSizeAdjustment(Screen.height);
  10. #endif
  11.                 return size;
  12.         }
  13.  
  14.         static public Vector2 GetMainGameViewSize ()
  15.         {
  16.                 int frame = Time.frameCount;
  17.                 if (mSizeFrame != frame)
  18.                 {
  19.                         mSizeFrame = frame;
  20.                         if (s_GetSizeOfMainGameView == null)
  21.                         {
  22.                                 System.Type type = System.Type.GetType("UnityEditor.GameView,UnityEditor");
  23.                                 s_GetSizeOfMainGameView = type.GetMethod("GetSizeOfMainGameView",
  24.                                         System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
  25.                         }
  26.                         mGameSize = (Vector2)s_GetSizeOfMainGameView.Invoke(null, null);
  27.                 }
  28.                 return mGameSize;
  29.         }
  30.  
here s_GetSizeOfMainGameView is System.Reflection.MethodInfo type in UIPanel, which can be replaced by Func<Vector2>, as the following, which will not alloc extra memory.

  1. s_GetSizeOfMainGameView = (Func<Vector2>)Delegate.CreateDelegate(typeof(Func<Vector2>), methodInfo);
  2.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Curious suggestion.. thanks. Just note though that this only happens in the Editor, so it's not much of an issue in practice.

beyonddoor

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Curious suggestion.. thanks. Just note though that this only happens in the Editor, so it's not much of an issue in practice.
Oh, my fault,  :-[, I test my game in Editor with profiler, and neglect the UNITY_EDITOR macro. Thanks to point it out.