Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - bjennings76

Pages: [1]
1
NGUI 3 Documentation / Re: UIPanel
« on: October 18, 2014, 12:08:40 PM »
I struggled for a long time to get the UIPanel to freely resize, but to also respect the restrictions of the UIRoot. I haven't had much luck. It will only work one of two ways:
  • No Constraints - Anchored elements stick to the sides of the panel, even beyond the UIRoot's constraints.
  • Constraints on UIPanel - The anchored elements respect the root UIPanel, but don't slide in when the UIRoot's view is smaller than the UIPanel's constraints.
I feel like there's a setting I'm missing somewhere. I was able to fix it using a custom component, though, which toggles a UIPanel's constraints depending on the UIRoot's view ratio. I'll post it here in case someone else runs into the same problem:

  1. using UnityEngine;
  2.  
  3. [ExecuteInEditMode, RequireComponent(typeof(UIPanel))]
  4. public class UIConstrain : MonoBehaviour
  5. {
  6.     private UIPanel Panel { get { return _panel ?? (_panel = GetComponent<UIPanel>()); } }
  7.     private UIPanel _panel;
  8.  
  9.     private UIRoot Root { get { return _root ?? (_root = GetComponentInParent<UIRoot>()); } }
  10.     private UIRoot _root;
  11.  
  12.     private static float Ratio { get { return Screen.width*1f/Screen.height; } }
  13.     private float MaxRatio { get { return Root.manualWidth*1f/Root.manualHeight; } }
  14.  
  15.     private void Start() { UpdateAnchors(); }
  16.  
  17. #if UNITY_EDITOR
  18.     private void Update() { if (!Application.isPlaying) { UpdateAnchors(); } }
  19. #endif
  20.  
  21.     private void UpdateAnchors()
  22.     {
  23.         if (!Root || !Panel) { return; }
  24.  
  25.         // Ratio is less than max ratio, so release panel constraints.
  26.         if (Ratio < MaxRatio && Panel.clipping == UIDrawCall.Clipping.ConstrainButDontClip) { Panel.clipping = UIDrawCall.Clipping.None; }
  27.  
  28.         // Ratio is more than max ratio, so constrain panel.
  29.         else if (Ratio > MaxRatio && Panel.clipping == UIDrawCall.Clipping.None)
  30.         {
  31.             Panel.clipping = UIDrawCall.Clipping.ConstrainButDontClip;
  32.             Panel.baseClipRegion = new Vector4(Panel.baseClipRegion.x, Panel.baseClipRegion.y, Root.manualWidth, Root.manualHeight);
  33.         }
  34.     }
  35. }
  36.  

2
I've had this same problem when removing buttons from a UIGrid and then repositioning the remaining buttons.

Each button will slide back to its original position on mouse over, leaving a hole in the list and acting just plain weird. This is because UIButtonOffset's 'mPos' variable used to tween doesn't get reset when the UIGrid repositions it's children. That position only gets set once at in UIButtonOffset's Start() function.

I see two solutions to the problem:
  • The saved position is local, so you could nest the button into an empty game object that is in turn inside the UIGrid. The grid will reposition that game object pulling the button along with it which will never be the wiser and it's saved position is still valid.

  • You could modify NGUI directly to expose a 'ResetStartPosition()' function in UIButtonOffset that UIGrid would use during it's 'Reposition()' function when a transform it repositions has that component.

For me, I didn't need the UIButtonOffset effect, so I've just removed the UIButtonOffset component and my problem is solved.

Pages: [1]