【原】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

 

转载于:https://www.cnblogs.com/basecn/p/NGUI_UIRoot.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值