unity 让ui对齐_UNITY3D使用NGUI制作自适应UI的总结

这篇博客介绍了如何在UNITY3D中使用NGUI组件创建自适应UI,通过`EF_C_NGUI_Clipping_Panel_Uniform`类展示了如何实现UI面板的自动对齐和裁剪范围的调整,确保UI在不同屏幕尺寸下保持一致显示效果。
摘要由CSDN通过智能技术生成

//  clipping panel auto uniform & change clipping range

public class EF_C_NGUI_Clipping_Panel_Uniform : MonoBehaviour

{

public Transform ParentAnchor           = null;

private bool m_IsInit                   = false;

void Update()

{

if( m_IsInit == false )

{

m_IsInit                        = true;

this.enabled                  = false;

//  make uniform

MakeUniform();

}

}

//  make uniform

void MakeUniform()

{

if( ParentAnchor == null )

return;

//  create parent obj

GameObject objParent                = new GameObject("Panel_Clipping_Parent");

GameObject objParent_Root           = new GameObject("Panel_Clipping_Parent_ROOT");

Transform parentObj                 = objParent.transform;

Transform parentObj_Root            = objParent_Root.transform;

if( parentObj == null )

return;

//  root parent

{

parentObj_Root.parent           = transform;

parentObj_Root.localPosition    = Vector3.zero;

parentObj_Root.localRotation    = Quaternion.identity;

parentObj_Root.localScale       = new Vector3(1,1,1);

parentObj_Root.parent           = transform.parent;

}

//  set parent obj data

{

//  parentObj.parent                    = transform;

parentObj.parent                = parentObj_Root;

parentObj.localPosition         = Vector3.zero;

parentObj.localRotation         = Quaternion.identity;

parentObj.localScale            = new Vector3(1,1,1);

}

//  change panel parent

transform.parent                    = parentObj;

//  get scale

Vector3 parentScale                 = ParentAnchor.localScale;

Vector3 objScale                    = parentObj.transform.localScale;

//  change clip range for current screen

UIPanel panelObj                    = gameObject.GetComponent();

if( panelObj != null )

{

Vector4 oldClipRange            = panelObj.clipRange;

panelObj.clipRange              = new Vector4( oldClipRange.x, oldClipRange.y

, oldClipRange.z * ( parentScale.x / objScale.x )

, oldClipRange.w * ( parentScale.y / objScale.y ) );

}

//  make uniform

objScale.x                          = 1.0f / parentScale.x;

objScale.y                          = 1.0f / parentScale.y;

objScale.z                          = 1.0f / parentScale.z;

parentObj.transform.localScale      = objScale;

}

}

posted on 2013-11-12 13:14 Updraft Studio 阅读(8765) 评论(0)  编辑 收藏 引用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值