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并不会生效,尽管他和你在属性面板看到的值是一样的。
retRoot.offsetMin = Vector2(0,0)
retRoot.offsetMax = Vector2(0,0)
SetInsetAndSizeFromParentEdge#
使用SetInsetAndSizeFromParentEdge函数来进行设定。此函数不受锚点和中心的影响,其中第一个参数代表对齐方式,第二个参数为距离边界的距离,第三个参数为宽度或高度。
示例:
---设置为左上角
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。
示例代码#
设置左上、左下、右上、右下四个锚点,并同时设置中心点。
---@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#
填充父节点:
var uiRect = uiElement.GetComponent<RectTransform>();
uiRect.localScale = Vector3.one;
uiRect.localPosition = Vector3.zero; // 注意Pos.z也要设置
uiRect.sizeDelta = parent.GetComponent<RectTransform>().sizeDelta;
填充整个屏幕:
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);
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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设置程序集属性(文件的详细信息)