UGUI自動SetNativeSize並設置錨點到該組件四角(帶快捷鍵)

  原文鏈接關於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);
    }
}

Unity 自定義Editor菜單快捷鍵

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章