Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: arkon3 on November 21, 2016, 09:58:44 PM

Title: Sprite jumps off screen when enabling anchor
Post by: arkon3 on November 21, 2016, 09:58:44 PM
This is a pretty frustrating bug. Using Unity 4.7.2 and NGUI 3.10.2 I place a UISprite in the top left of the screen. Then using the sprites anchoring dropdown, choose unified. Before I can do anything else the sprite jumps off screen somewhere random and I have to use the anchor variables to bring it back on screen. It used to be the sprite would stay where it was when you enabled the anchor.
Title: Re: Sprite jumps off screen when enabling anchor
Post by: ArenMook on November 27, 2016, 04:02:01 PM
Are you on OSX?
Title: Re: Sprite jumps off screen when enabling anchor
Post by: arkon3 on November 27, 2016, 04:17:15 PM
yes.
Title: Re: Sprite jumps off screen when enabling anchor
Post by: ArenMook on November 27, 2016, 05:14:40 PM
Unity 5.4 broke the ability to determine screen size in OSX on retina devices while in the editor when the game view is hidden, I know that much. Is your game view visible?
Title: Re: Sprite jumps off screen when enabling anchor
Post by: arkon3 on November 27, 2016, 05:49:49 PM
This problem is on 4.7.2 and yes my game view is visible.
Title: Re: Sprite jumps off screen when enabling anchor
Post by: ArenMook on November 27, 2016, 11:22:26 PM
Find line 1902 of NGUITools.cs and remove the #if !UNITY_EDITOR_OSX.

P.S. Replacing the function with this one should also work:
  1. static public Vector2 screenSize
  2.         {
  3.                 get
  4.                 {
  5.                         int frame = Time.frameCount;
  6.  
  7.                         if (mSizeFrame != frame || !Application.isPlaying)
  8.                         {
  9.                                 Profiler.BeginSample("Editor-only GC allocation (NGUITools.screenSize)");
  10.                                 mSizeFrame = frame;
  11.  
  12.                                 if (s_GetSizeOfMainGameView == null && !mCheckedMainViewFunc)
  13.                                 {
  14.                                         mCheckedMainViewFunc = true;
  15.                                         System.Type type = System.Type.GetType("UnityEditor.GameView,UnityEditor");
  16.  
  17.                                         // Post-Unity 5.4
  18.                                         var methodInfo = type.GetMethod("GetMainGameViewTargetSize",
  19.                                                 System.Reflection.BindingFlags.Public |
  20.                                                 System.Reflection.BindingFlags.NonPublic |
  21.                                                 System.Reflection.BindingFlags.Static);
  22.  
  23.                                         // Pre-Unity 5.4
  24.                                         if (methodInfo == null)
  25.                                                 methodInfo = type.GetMethod("GetSizeOfMainGameView",
  26.                                                         System.Reflection.BindingFlags.Public |
  27.                                                         System.Reflection.BindingFlags.NonPublic |
  28.                                                         System.Reflection.BindingFlags.Static);
  29.  
  30.                                         // Create the delegate
  31.                                         if (methodInfo != null)
  32.                                         {
  33.                                                 s_GetSizeOfMainGameView = (Func<Vector2>)Delegate.CreateDelegate(typeof(Func<Vector2>), methodInfo);
  34.                                         }
  35.                                         else Debug.LogWarning("Unable to get the main game view size function");
  36.                                 }
  37.  
  38.                                 if (s_GetSizeOfMainGameView != null)
  39.                                 {
  40. #if UNITY_EDITOR_OSX
  41.                                         // There seems to be a Unity 5.4 bug that returns invalid screen size when the mouse is clicked (wtf?) on OSX
  42.                                         if (mGameSize.x == 1f && mGameSize.y == 1f) mGameSize = s_GetSizeOfMainGameView();
  43. #else
  44.                                         mGameSize = s_GetSizeOfMainGameView();
  45. #endif
  46.                                 }
  47.                                 else mGameSize = new Vector2(Screen.width, Screen.height);
  48.                                 Profiler.EndSample();
  49.                         }
  50.                         return mGameSize;
  51.                 }
  52.         }
Title: Re: Sprite jumps off screen when enabling anchor
Post by: arkon3 on November 28, 2016, 03:21:35 AM
Taking out the #ifdef fixed it for me. What did I just lose?
Title: Re: Sprite jumps off screen when enabling anchor
Post by: ArenMook on November 28, 2016, 11:10:48 AM
On Unity 4.7 -- nothing.