zoukankan      html  css  js  c++  java
  • 【原】NGUI中的UIRoot脚本功能

    UIRoot是NGUI控件的根节点,使用是根据屏幕尺寸自动(或手动)调节节点下子控件的大小。

    这个组件声明了在编辑模式下运行:[ExecuteInEditMode],在Inspector编辑修改属性值时,可以直接影响控件。

    UIRoot有两个开放属性:

    Automatic: bool

    ManualHeight: float

    在Inspector中,如果Automatic为true,则自动调节;如果是false,则可以手工调节ManualHeight。

    调整时可以直接看到控制尺寸的变化。

    //----------------------------------------------
    //            NGUI: Next-Gen UI kit
    // Copyright © 2011-2012 Tasharen Entertainment
    //----------------------------------------------
    
    using UnityEngine;
    
    /// <summary>
    /// This is a script used to keep the game object scaled to 2/(Screen.height).
    /// If you use it, be sure to NOT use UIOrthoCamera at the same time.
    /// </summary>
    
    [ExecuteInEditMode]
    [AddComponentMenu("NGUI/UI/Root")]
    public class UIRoot : MonoBehaviour
    {
        public bool automatic = true;
        public int manualHeight = 800;
    
        Transform mTrans;
    
        void Start ()
        {
            mTrans = transform;
    
            UIOrthoCamera oc = GetComponentInChildren<UIOrthoCamera>();
            
            if (oc != null)
            {
                Debug.LogWarning("UIRoot should not be active at the same time as UIOrthoCamera. Disabling UIOrthoCamera.", oc);
                Camera cam = oc.gameObject.GetComponent<Camera>();
                oc.enabled = false;
                if (cam != null) cam.orthographicSize = 1f;
            }
        }
    
        void Update ()
        {
            manualHeight = Mathf.Max(2, automatic ? Screen.height : manualHeight);
    
            float size = 2f / manualHeight;
            Vector3 ls = mTrans.localScale;
    
            if (!Mathf.Approximately(ls.x, size) ||
                !Mathf.Approximately(ls.y, size) ||
                !Mathf.Approximately(ls.z, size))
            {
                mTrans.localScale = new Vector3(size, size, size);
            }
        }
    }
    View Code
  • 相关阅读:
    Java再学习——栈(stack)和堆(heap)
    Java再学习——深究static关键字
    毕业设计进度:3月4日
    毕业设计进度:3月3日
    可视化组件:echarts柱状图添加点击事件
    毕业设计进度:2月29日
    毕业设计进度:2月28日
    毕业设计进度:2月27日
    毕业设计进度:2月26日
    毕业设计进度:2月25日
  • 原文地址:https://www.cnblogs.com/basecn/p/NGUI_UIRoot.html
Copyright © 2011-2022 走看看