ngui中pivot的C#脚本控制改变

2015年01月15日 16:52 0 点赞 0 评论 更新于 2025-11-21 14:29

在NGUI(Next-Gen UI)中,我们可以通过C#脚本方便地控制UISprite组件的pivot属性,从而改变其轴心点位置。以下是几个示例代码,展示了如何将UISprite的轴心点分别设置为左、中、右:

设置轴心点为左

go.GetComponent<UISprite>().pivot = UIWidget.Pivot.Left;

设置轴心点为中

go.GetComponent<UISprite>().pivot = UIWidget.Pivot.Center;

设置轴心点为右

go.GetComponent<UISprite>().pivot = UIWidget.Pivot.Right;

在上述代码中,go 代表一个 GameObject 对象,通过 GetComponent<UISprite>() 方法获取该对象上的 UISprite 组件,然后将其 pivot 属性设置为不同的值,即可改变 UISprite 的轴心点位置。这样的操作在UI布局和动画效果实现中非常有用。