NGUI_Panel(clipped panels must have a uniform scale)

来源:互联网 发布:淘宝网退货地址怎么改 编辑:程序博客网 时间:2024/05/22 13:21

使用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);        }    }} 
阅读全文
0 0
原创粉丝点击