NGUI_Panel(clipped panels must have a uniform scale)

使用NGUI,通过更改父物体的scale来适配屏幕时,如果子panel使用softclip,子panel会出现错误:clipped panels must have a uniform scale, 出错原因是子panel的scale的x,y,z不一致,因此解决方法就是把panel物体的transform的scale的xyz换算成(1,1,1),通过改变所有子物体的,但改变后为了裁剪会出问题,因此通过修改panel 的clipping下面的offset, center, size值。
先讲一下NGUI的适配:UIRoot的ScallingStyle设置为FixedSize,
ManualHeight 设置为480, 我按800*480制作UI资源

设置完成后,NGUI会按照 screenHeight/480.0f 对UI进行高度适配,由于大部分手机厂商制作的机型宽高比不一致, 因此待NGUI完成适配后,我们需要针对宽做二次适配,屏幕适配的代码如下

public class MyWidgetScale : MonoBehaviour {

    static bool g_bInit = true;
    public static float g_fAdaptedX;
    void Awake ()
    {
        if (g_bInit)
        {
            float ration1 = 800f / 480f;
            float ration2 = Screen.width * 1.0f / Screen.height;
            g_fAdaptedX = ration2 / ration1;
            g_bInit = false;
        }

        transform.localScale = new Vector3(g_fAdaptedX, 1, 1);
    }
}

由于上边的适配导致子panel的scale不一致了,因此调整子panel的scale和裁剪区域
基本讲清楚了,下边是代码部分:
子panel没有scrollview

public class AdjustSubPanel : MonoBehaviour {

    void Start () {

        float baseScale = 1;
        if(Screen.width > Screen.height)
        {
            baseScale = transform.lossyScale.x;
        }
        else
        {
            baseScale = transform.lossyScale.y;
        }

        transform.localScale = new Vector3(baseScale / transform.lossyScale.x, baseScale / transform.lossyScale.y,
            baseScale / transform.lossyScale.z);

        UIPanel panel = transform.GetComponent<UIPanel>();
        panel.baseClipRegion = new Vector4(panel.baseClipRegion.x / transform.localScale.x, panel.baseClipRegion.y / transform.localScale.y
        , panel.baseClipRegion.z / transform.localScale.x, panel.baseClipRegion.w / transform.localScale.y);

        panel.clipOffset = new Vector2(panel.clipOffset.x / transform.localScale.x, panel.clipOffset.y / transform.localScale.y);

        for (int i = 0; i < transform.childCount; i++)
        {
            transform.GetChild(i).localScale = new Vector3(1 / transform.localScale.x, 1 / transform.localScale.y, 1 / transform.localScale.z);
        }
    }
}

子panel上挂在scrollview时

关于localScale和lossyScale的注解:
当GameObject对象A为GameObject对象B的父物体时,父物体A的各个分量放缩值x、y、z的大小应该保持1:1:1的比例,否则当子物体B的Rotation值比例不为1:1:1时,B物体将会发生变形。
设GameObject对象A为B的父物体,当A物体各个分量的放缩值保持1:1:1的比例时,子物体B的lossyScale返回值即为B物体相对世界坐标系的放缩值,关系为B.localScale = B.lossyScale/A.localScale。


public class AdjustSubPanelWithScrollView : MonoBehaviour {  

    void Start()
    {
        UIScrollView scrollView = transform.GetComponent<UIScrollView>();
        float baseScale = 1;

        if (scrollView.movement == UIScrollView.Movement.Vertical)
        {
            baseScale = transform.lossyScale.y;
        }
        else if (scrollView.movement == UIScrollView.Movement.Horizontal)
        {
            baseScale = transform.lossyScale.x;
        }
        else
        {
            Debug.LogError("Unsupported UIScrollView Movement");
            return;
        }
        Debug.Log(baseScale);
        transform.localScale = new Vector3(baseScale / transform.lossyScale.x, baseScale / transform.lossyScale.y, baseScale / transform.lossyScale.z);

        UIPanel panel = transform.GetComponent<UIPanel>();

        panel.baseClipRegion = new Vector4(panel.baseClipRegion.x / transform.localScale.x, panel.baseClipRegion.y / transform.localScale.y
            , panel.baseClipRegion.z / transform.localScale.x, panel.baseClipRegion.w / transform.localScale.y);

        panel.clipOffset = new Vector2(panel.clipOffset.x / transform.localScale.x, panel.clipOffset.y / transform.localScale.y);

        for (int i = 0; i < transform.childCount; i++)
        {
            transform.GetChild(i).localScale = new Vector3(1 / transform.localScale.x, 1 / transform.localScale.y, 1 / transform.localScale.z);
        }
    }
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值