每个人都有自己的知识体系。
Toggle navigation
Home
随笔
C#/.Net
树莓派 / Raspberry
皓月汉化组
Beego
Golang
OxideMod
apache
haproxy
windows
Java
Objective-C
日语/罗马音歌词/日语常识
MongoDB
python
电学
公告
Minecraft服务器-公告
NanoPi
C4D (CINEMA 4D)
生活
推流/m3u8/rtmp/rtsp
Unity3d
ffmpeg
数据结构
区块链
tarui
UnityForPSVita
About Me
Archives
Tags
安全的设置锚点,并保证位置不变
2024-03-12 17:39:56
53
0
0
akiragatsu
``` public enum AnchorPresets { TopLeft, TopCenter, TopRight, MiddleLeft, MiddleCenter, MiddleRight, BottomLeft, BottonCenter, BottomRight, BottomStretch, VertStretchLeft, VertStretchRight, VertStretchCenter, HorStretchTop, HorStretchMiddle, HorStretchBottom, StretchAll } /// <summary> /// 安全的设置锚点,并保证位置不变 /// </summary> /// <param name="source"></param> /// <param name="allign"></param> public static void SetAnchor(this RectTransform source, AnchorPresets allign) { //计算原锚点中心和组件大小 Vector2 oldAnchorCenter = (source.anchorMin + source.anchorMax) / 2; Vector2 oldAnchoredPos = source.anchoredPosition; Vector2 objSize = new Vector2(source.rect.width, source.rect.height); switch (allign) { case (AnchorPresets.TopLeft): { source.anchorMin = new Vector2(0, 1); source.anchorMax = new Vector2(0, 1); break; } case (AnchorPresets.TopCenter): { source.anchorMin = new Vector2(0.5f, 1); source.anchorMax = new Vector2(0.5f, 1); break; } case (AnchorPresets.TopRight): { source.anchorMin = new Vector2(1, 1); source.anchorMax = new Vector2(1, 1); break; } case (AnchorPresets.MiddleLeft): { source.anchorMin = new Vector2(0, 0.5f); source.anchorMax = new Vector2(0, 0.5f); break; } case (AnchorPresets.MiddleCenter): { source.anchorMin = new Vector2(0.5f, 0.5f); source.anchorMax = new Vector2(0.5f, 0.5f); break; } case (AnchorPresets.MiddleRight): { source.anchorMin = new Vector2(1, 0.5f); source.anchorMax = new Vector2(1, 0.5f); break; } case (AnchorPresets.BottomLeft): { source.anchorMin = new Vector2(0, 0); source.anchorMax = new Vector2(0, 0); break; } case (AnchorPresets.BottonCenter): { source.anchorMin = new Vector2(0.5f, 0); source.anchorMax = new Vector2(0.5f, 0); break; } case (AnchorPresets.BottomRight): { source.anchorMin = new Vector2(1, 0); source.anchorMax = new Vector2(1, 0); break; } case (AnchorPresets.HorStretchTop): { source.anchorMin = new Vector2(0, 1); source.anchorMax = new Vector2(1, 1); break; } case (AnchorPresets.HorStretchMiddle): { source.anchorMin = new Vector2(0, 0.5f); source.anchorMax = new Vector2(1, 0.5f); break; } case (AnchorPresets.HorStretchBottom): { source.anchorMin = new Vector2(0, 0); source.anchorMax = new Vector2(1, 0); break; } case (AnchorPresets.VertStretchLeft): { source.anchorMin = new Vector2(0, 0); source.anchorMax = new Vector2(0, 1); break; } case (AnchorPresets.VertStretchCenter): { source.anchorMin = new Vector2(0.5f, 0); source.anchorMax = new Vector2(0.5f, 1); break; } case (AnchorPresets.VertStretchRight): { source.anchorMin = new Vector2(1, 0); source.anchorMax = new Vector2(1, 1); break; } case (AnchorPresets.StretchAll): { source.anchorMin = new Vector2(0, 0); source.anchorMax = new Vector2(1, 1); break; } } source.sizeDelta = objSize; //调整大小 //计算偏移量 RectTransform rectTrans_Father = source.parent as RectTransform; //获取父对象 Vector2 anchorCenter = (source.anchorMin + source.anchorMax) / 2; Vector2 deltaAnchor = anchorCenter - oldAnchorCenter; Vector2 deltaMove = new Vector2(deltaAnchor.x * rectTrans_Father.rect.width, deltaAnchor.y * rectTrans_Father.rect.height); source.anchoredPosition = oldAnchoredPos - deltaMove; //调整距离锚点的位置 } ```
Pre:
怪物猎人20周年外传人气怪物投票
Next:
皓月云-历年周年纪念视频欣赏
0
likes
53
Weibo
Wechat
Tencent Weibo
QQ Zone
RenRen
Submit
Sign in
to leave a comment.
No Leanote account?
Sign up now.
0
comments
More...
Table of content
No Leanote account? Sign up now.