我的环境

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

参考资料

参考:http://www.arkaistudio.com/blog/334/unity/unity-ugui-原理篇三:recttransform

UGUI Set Anchor And Pivot的更多相关文章

  1. unity,set ugui rectTransform anchor by script

    如果想用代码实现与下面面板相同的功能 试验可知改变上面选项下面四个值也随之变化: 所以说明二者是一回事儿. 因此,只要通过代码修改RectTransform的anchorMax和anchorMin成员 ...

  2. uGUI练习(一) Anchor

    一.练习步骤 如果用过NGUI的Anchor,我们知道在2.x的版本有UIAnchor组件(下图左),3.x版本中,每个UIWidget有自带的Anchors(下图右) 而uGUI的Anchor用起来 ...

  3. UNITY的UI之Pivot与Anchor区别

    Pivot Rotations, size, and scale modifications occur around the pivot so the position of the pivot a ...

  4. uGUI VS NGUI

    前言 这篇日志的比较是根据自己掌握知识所写的,请各路大神多多指教. 引擎版本: Unity 4.6 beta 两者区别 1.uGUI的Canvas 有世界坐标和屏幕坐标 2.uGUI的Button属性 ...

  5. 【已学完】UGUI Schedule

    章节 内容 签到 Unity4.6 New UI(UGUI) 1.UGUI概述与Canvas画布介绍(一) 5月14日 2.Canvas画布介绍(二) 5月14日 3.Text控件 5月14日 4.I ...

  6. 在Unity中用UGUI制作可输入下拉框

    Unity中UGUI制作可输入下拉框 目录 Unity中UGUI制作可输入下拉框 前言 组件分析 制作流程 总结 前言 在搜索引擎以及一些网页中我们常常可以看见这样一种UI控件,看上去是一个输入框,在 ...

  7. 转:UGUI与NGUI的区别与优缺点

    1. NGUI与UGUI的区别 1) uGUI的Canvas 有世界坐标和屏幕坐标   2) uGUI的Image可以使用material     3) UGUI通过Mask来裁剪,而NGUI通过Pa ...

  8. NGUI_概述

    序言:这是张三疯第一次开始NGUI插件的学习,刚开始学习,肯定有很多漏洞,后期会及时的补上的. 希望大家可以见谅,希望大佬多多指教. 一.什么是NGUI: NGUI是严格遵循KISS原则并用C#编写的 ...

  9. U3D面试题

    直接上新.... 什么是协同程序?答:在主线程运行时同时开启另一段逻辑处理,来协助当前程序的执行.换句话说,开启协程就是开启一个可以与程序并行的逻辑.可以用来控制运动.序列以及对象的行为. Unity ...

随机推荐

  1. shell脚本示例:批量比较多个文件的内容是否相同

    bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 要比较两个文件的内容是否完全一致,可以简单地使用diff命令. ...

  2. MySQL的binlog恢复(Windows下)

    前言 在最近的工作中,由于自己粗(zuo)心(si)误update操作导致几百行的数据出现错误,在心急如焚的同时(那时候我竟然不知道除了备份之后还有binlog日志恢复)立马查资料学习binlog的恢 ...

  3. T-SQL:qualify和window 使用(十七)

    1.qualify 是一个潜在的额外筛选器 主要用于对开窗函数的数据筛选 SELECT orderid, orderdate, val, RANK() OVER(ORDER BY val DESC) ...

  4. mysqlslap 压力测试使用总结

    今天从运维同事那听说了mysql压力测试工具mysqlslap.经了解其实mysql自带就有一个叫mysqlslap的压力测试工具,还是模拟的不错的.下面举例说说.mysqlslap是从5.1.4版开 ...

  5. Java Spring cron表达式使用详解

    Java Spring cron表达式使用详解   By:授客 QQ:1033553122 语法格式 Seconds Minutes Hours DayofMonth Month DayofWeek ...

  6. 现在有两个变量,分别是a = 3, b = 4,那么我们不用第三个变量来调换a和b的值。

    现在有两个变量,分别是a = 3, b = 4,那么我们不用第三个变量来调换a和b的值. <!DOCTYPE html><html><head>    <me ...

  7. CSS3 - @keyframes

    语法 @keyframes animationname { keyframes-selector {css-styles;} } 值 描述 animationname 必需.定义动画的名称. keyf ...

  8. 山东理工大学SDUT - ACM OJ 题: Python代码 及分析

    Python基础语法学习完成,先刷基础题100道巩固 ,附 题目.代码.知识分析 题目:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index ...

  9. Android为TV端助力 遥控器的映射

    第一编写kl文件时先在盒子上输入getevent -v查看设备信息,设备信息里有vendor.product.version, 假如分别是xxxx,yyyy,zzzz,那么你的文件名就要命名为Vend ...

  10. 不能用notepad++编辑器编写python

    不能用notepad++编辑器编写python,因为notepad对空格支持不是很良好,会出现莫名其妙的错误!建议用vim或emacs. 有人这样解释:不要混合使用制表符和空格来缩进,因为这在跨越不同 ...