Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: roady on December 16, 2014, 01:20:12 PM

Title: Feature: UIWidget - maxWidth/maxHeight
Post by: roady on December 16, 2014, 01:20:12 PM
Would love to see feature for setting maxWidth/maxHeight for components. Maybe in the anchors?

For example.

I've a login view centered. I set anchors on the inputs (left/right) to resize with the different resolutions. Now the user rotates the screen to a landscape mode. If the inputs still stretches the whole way this will make the login screen look really weird. So if I set a maxWidth value it will only resize so far.

This value should of course default be 0 and ignored if not set.

thx for a excellent plugin
Title: Re: Feature: UIWidget - maxWidth/maxHeight
Post by: roady on December 18, 2014, 06:48:09 AM
Didn't want to build in maxWidth / maxHeight into NGUI because of update issues. Did a small solution.

On Camera

  1. public class CameraRotation : MonoBehaviour {
  2.  
  3.         private Camera cam;
  4.         private UIRoot mRoot;
  5.         private bool isPortrait;
  6.         public delegate void OnRotationChange(bool isPortrait, int pixelWidth, int pixelHeight);
  7.         public OnRotationChange OnUpdate;
  8.  
  9.         void Start () {
  10.                 cam = GetComponent<Camera> ();
  11.                 mRoot = NGUITools.FindInParents<UIRoot>(gameObject);
  12.                 isPortrait = (cam.pixelWidth > cam.pixelHeight); // make false value at first so first update runs event
  13.         }
  14.  
  15.        
  16.  
  17.         void Update () {
  18.                 bool n_isPortrait = (cam.pixelWidth < cam.pixelHeight);
  19.                 if (n_isPortrait != isPortrait) {
  20.                         isPortrait = n_isPortrait;
  21.  
  22.                         // update
  23.                         float ratio = (float)mRoot.activeHeight / Screen.height;
  24.                         int width = (int)Mathf.Ceil(Screen.width * ratio);
  25.                         int height = (int)Mathf.Ceil(Screen.height * ratio);
  26.  
  27.                         if(OnUpdate!=null)
  28.                                 OnUpdate(isPortrait,width,height);
  29.                 }
  30.         }
  31. }

On a widget. Example set a
UIPanel
   UIWidget - holder <- place script here
        .... my content ....

  1. public class WidgetLimitSize : MonoBehaviour {
  2.  
  3.         private UIWidget widget;
  4.         public int maxWidth = 0;
  5.         public int maxHeight = 0;
  6.  
  7.         private int aLeft = 0;
  8.         private int aRight = 0;
  9.         private int aTop = 0;
  10.         private int aBottom = 0;
  11.  
  12.  
  13.         void Awake () {
  14.                 widget = GetComponent<UIWidget> ();
  15.                 aLeft = widget.leftAnchor.absolute;
  16.                 aRight = widget.rightAnchor.absolute;
  17.                 aTop = widget.topAnchor.absolute;
  18.                 aBottom = widget.bottomAnchor.absolute;
  19.  
  20.                 CameraRotation cam = Camera.main.GetComponent<CameraRotation> ();
  21.                 cam.OnUpdate += OnUpdate;
  22.         }
  23.  
  24.         private void OnUpdate(bool isPortrait, int width, int height){
  25.                 if (maxWidth != 0) {
  26.                         widget.leftAnchor.absolute = aLeft;
  27.                         widget.rightAnchor.absolute = aRight;
  28.                 }
  29.                 if (maxHeight != 0) {
  30.                         widget.topAnchor.absolute = aTop;
  31.                         widget.bottomAnchor.absolute = aBottom;
  32.                 }
  33.  
  34.                 widget.UpdateAnchors();
  35.         }
  36.  
  37.         void LateUpdate () {
  38.                 CheckBounds ();
  39.         }
  40.  
  41.         private void CheckBounds(){
  42.                 if (maxWidth != 0 && widget.width >= maxWidth) {
  43.                         widget.width = maxWidth;
  44.                 }
  45.                 if (maxHeight != 0 && widget.height >= maxHeight) {
  46.                         widget.height = maxHeight;
  47.                 }
  48.         }
  49. }