原文链接关于UGUI自动设置锚点到控件四个角的问题,我在原文的基础上加了一个SetNativeSize的快捷键,这样在拼接UI的时候就快多了。
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class AnchorAutoFit : MonoBehaviour
{
[MenuItem("UGUI/SetNativeSize &s")]
static void SetImgNative()
{
Image img = Selection.activeGameObject.GetComponent<Image>();
if (img == null) return;
img.SetNativeSize();
}
[MenuItem("UGUI/Anchors to Corners &a")]
static void AnchorsToCorners()
{
RectTransform rect = Selection.activeTransform as RectTransform;
RectTransform pt = Selection.activeTransform.parent as RectTransform;
if (rect == null || pt == null) return;
Vector2 newAnchorsMin = new Vector2(rect.anchorMin.x + rect.offsetMin.x / pt.rect.width,
rect.anchorMin.y + rect.offsetMin.y / pt.rect.height);
Vector2 newAnchorsMax = new Vector2(rect.anchorMax.x + rect.offsetMax.x / pt.rect.width,
rect.anchorMax.y + rect.offsetMax.y / pt.rect.height);
rect.anchorMin = newAnchorsMin;
rect.anchorMax = newAnchorsMax;
rect.offsetMin = rect.offsetMax = new Vector2(0, 0);
}
[MenuItem("UGUI/Corners to Anchors &d")]
static void CornersToAnchors()
{
RectTransform rect = Selection.activeTransform as RectTransform;
if (rect == null) return;
rect.offsetMin = rect.offsetMax = new Vector2(0, 0);
}
}