UGUI Set Anchor And Pivot

本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhaoqingqing/p/8884303.html



我的环境#

Unity 5.3.7p4

在运行时动态的设置UI元素的锚点和中心点。

设置坐标#

对于UI上的元素,使用anchorPosition,而不是localpostion,因为Recttransform可以设置锚点。

设置Anchor#

修改offsetMax不生效#

使用下面这段代码设置Anchor并不会生效,尽管他和你在属性面板看到的值是一样的。

Copy
retRoot.offsetMin = Vector2(0,0) retRoot.offsetMax = Vector2(0,0)

SetInsetAndSizeFromParentEdge#

使用SetInsetAndSizeFromParentEdge函数来进行设定。此函数不受锚点和中心的影响,其中第一个参数代表对齐方式,第二个参数为距离边界的距离,第三个参数为宽度或高度。

示例:

Copy
---设置为左上角 retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, width) retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, height)

修改Anchor不会影响Pivot#

修改Anchor之后,并不会影响Pivot

设置Pivot(中心点)#

pivot是一个0~1之间的值 示例:retRoot.pivot = Vector2(0, 0)

其中0,0为左下角

当你要做动画,设置父容器的Pivot就可以控制动画的出现方向

查看Pivot#

在Editor的工具栏将Pivot的模式设置为这个模式(Pivot Local),才能查看到正确的Pivot。

示例代码#

设置左上、左下、右上、右下四个锚点,并同时设置中心点。

Copy
---@param retRoot UnityEngine.RectTransform function TipsHelper.SetTipsAnchor(retRoot, anchor, width, height) if not retRoot then print("[error] SetAnchor失败,RectTransform不能为空") return end if anchor == Constants.Anchor.LeftTop then retRoot.pivot = Vector2(0, 1) retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, width) retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, height) elseif anchor == Constants.Anchor.LeftBottom then retRoot.pivot = Vector2(0, 0) retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, width) retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, height) elseif anchor == Constants.Anchor.RightTop then retRoot.pivot = Vector2(1, 1) retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, width) retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, height) elseif anchor == Constants.Anchor.RightBottom then retRoot.pivot = Vector2(1, 0) retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, width) retRoot:SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, height) end end

代码设置Stretch#

填充父节点:

Copy
var uiRect = uiElement.GetComponent<RectTransform>(); uiRect.localScale = Vector3.one; uiRect.localPosition = Vector3.zero; // 注意Pos.z也要设置 uiRect.sizeDelta = parent.GetComponent<RectTransform>().sizeDelta;

填充整个屏幕:

Copy
public static void StretchFull (this RectTransform rect, float paddingLeft, float paddingTop, float paddingRight, float paddingBottom) { rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.offsetMin = new Vector2 (paddingLeft, paddingBottom); rect.offsetMax = new Vector2 (-paddingRight, -paddingTop); }

参考资料#

作者:赵青青   一名在【网易游戏】做游戏开发的程序员,擅长Unity3D,游戏开发,.NET等领域。
本文版权归作者和博客园共有,欢迎转载,转载之后请务必在文章明显位置标出原文链接和作者,谢谢。
如果本文对您有帮助,请点击【推荐】您的赞赏将鼓励我继续创作!想跟我一起进步么?那就【关注】我吧。
posted @   赵青青  阅读(2294)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 易语言 —— 开山篇
· Trae初体验
历史上的今天:
2017-04-23 xLua中导出Dotween
2017-04-23 Visual Studio(VS) F12 查看DLL源代码
2016-04-23 VS设置程序集属性(文件的详细信息)
CONTENTS
点击右上角即可分享
微信分享提示