Anchors to Corners 的效果會令 UGUI 的 RectTransform 依上層的縮放比例對齊.
而且是以圖片現在的位置作為參考自動進行.
如上圖效果, 對齊左上角的會完全脫離, 而依 Anchors To Corners 的則完美縮放.
把 Anchor (三角) 完美重疊到 Corner (藍點) 就會達成這完美的等比例縮放.
由於 Unity 沒有提供指令, 所以寫成指令好像比較好用.
本 script 以 Ctrl + T, 即 %t 作為 Hotkey.
當然把要放進 “Editor” 檔案夾內.
參考 : http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/?_ga=2.244486401.583712110.1615051159-1416679152.1573397245
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; namespace Kit { public class RectTransformScripts : EditorWindow { /// <summary> /// <see cref="http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/?_ga=2.244486401.583712110.1615051159-1416679152.1573397245"/> /// </summary> [MenuItem("Kit/UI Kit/Anchors to Corners %t")] private static void AnchorsToCorners() { if (Selection.gameObjects.Length > 0) { Transform[] transforms = Selection.transforms; int cnt = 0; int nameCnt = Mathf.Min(cnt, 20); string uiNames = ""; foreach (Transform transform in transforms) { if (transform is RectTransform rectTransform && rectTransform.parent is RectTransform parent) { Vector2 newAnchorsMin = new Vector2(rectTransform.anchorMin.x + rectTransform.offsetMin.x / parent.rect.width, rectTransform.anchorMin.y + rectTransform.offsetMin.y / parent.rect.height); Vector2 newAnchorsMax = new Vector2(rectTransform.anchorMax.x + rectTransform.offsetMax.x / parent.rect.width, rectTransform.anchorMax.y + rectTransform.offsetMax.y / parent.rect.height); rectTransform.anchorMin = newAnchorsMin; rectTransform.anchorMax = newAnchorsMax; rectTransform.offsetMin = rectTransform.offsetMax = Vector2.zero; cnt++; if (nameCnt > 0) { nameCnt--; uiNames += "," + rectTransform.name; } } } Debug.Log($"UI: Anchors To Corners : {cnt}\n>>{uiNames}"); } } } }