使用快捷键创建UGUI控件

using UnityEngine;
using UnityEditor;
using UnityEngine.UI;

public class UGUIShortcutKey : Editor
{
    public const int UIlayer = 5;
    [MenuItem("Component/UI/Shortcut/Create Text #&L")]
    public static void CreateText()
    {
        if (Selection.gameObjects.Length == 1)
        {
            GameObject obj = Selection.gameObjects[0];
            GameObject text = new GameObject();
            RectTransform textRect = text.AddComponent<RectTransform>();
            Text textTx = text.AddComponent<Text>();
            text.transform.SetParent(obj.transform);
            text.name = "Text";
            text.layer = UIlayer;
            //设置字体
            textTx.font = AssetDatabase.LoadAssetAtPath<Font>("Assets/GameMain/Localization/ChineseSimplified/Fonts/MainFont.ttf");
            textTx.text = "plateface";
            textTx.alignment = TextAnchor.MiddleCenter;
            textRect.localScale = Vector3.one;
            textRect.anchoredPosition = Vector2.zero;
            textRect.anchoredPosition3D = Vector3.zero;
            textRect.sizeDelta = new Vector2(textTx.preferredWidth, textTx.preferredHeight);
            RectTransformZero(textRect);
        }
    }

    [MenuItem("Plateface/CreateUGUI Button #&B")]
    public static void CreateButton()
    {
        if (Selection.gameObjects.Length == 1)
        {
            GameObject obj = Selection.gameObjects[0];
            if (obj == null) return;

            GameObject button = new GameObject();
            GameObject buttonTx = new GameObject();

            RectTransform buttonRect = button.AddComponent<RectTransform>();
            RectTransform buttonTxRect = buttonTx.AddComponent<RectTransform>();

            button.AddComponent<Image>();
            Text texBtn = buttonTx.AddComponent<Text>();
            texBtn.font = AssetDatabase.LoadAssetAtPath<Font>("Assets/GameMain/Localization/ChineseSimplified/Fonts/MainFont.ttf");
            texBtn.text = "Button";
            buttonTxRect.sizeDelta = new Vector2(texBtn.preferredWidth, texBtn.preferredHeight);
            button.transform.SetParent(obj.transform);
            buttonTx.transform.SetParent(button.transform);
            button.name = "Button";
            buttonTx.name = "Text";

            button.layer = UIlayer;
            buttonTx.layer = UIlayer;

            RectTransformZero(buttonRect);
            RectTransformZero(buttonTxRect);
        }
    }

    [MenuItem("Plateface/CreateUGUI Image #&S")]
    public static void CreateImage()
    {
        if (Selection.gameObjects.Length == 1)
        {
            GameObject obj = Selection.gameObjects[0];
            //RectTransform selectionObjRect = Selection.gameObjects[0].GetComponent<RectTransform>();

            GameObject image = new GameObject();
            RectTransform imageRect = image.AddComponent<RectTransform>();
            image.AddComponent<Image>();
            image.transform.SetParent(obj.transform);
            image.name = "Image";
            image.layer = 5;

            RectTransformZero(imageRect);
        }

    }

    [MenuItem("Plateface/CreateUGUI InputField #&I")]
    public static void CreateInputField()
    {
        if (Selection.gameObjects.Length == 1)
        {
            GameObject obj = Selection.gameObjects[0];

            GameObject inputField = new GameObject();
            RectTransform rectTransform = inputField.AddComponent<RectTransform>();
            Image image = inputField.AddComponent<Image>();
            //image.sprite = Resources.Load<Sprite>("UnityPlugins/UGUIShortcutKeyTexture/background1");
            inputField.AddComponent<InputField>();
            rectTransform.localScale = new Vector3(1, 1, 1);
            inputField.layer = UIlayer;

            inputField.transform.SetParent(obj.transform);
            inputField.name = "InputField";

            GameObject placeholder = new GameObject();
            Text placeholderTx = placeholder.AddComponent<Text>();
            placeholderTx.font = AssetDatabase.LoadAssetAtPath<Font>("Assets/GameMain/Localization/ChineseSimplified/Fonts/MainFont.ttf");
            placeholderTx.text = "Enter text...";
            placeholder.transform.SetParent(inputField.transform);
            placeholder.name = "Placeholder";
            placeholder.layer = UIlayer;
            placeholderTx.color = Color.black;

            GameObject text = new GameObject();
            Text textTx = text.AddComponent<Text>();
            text.transform.SetParent(inputField.transform);
            text.name = "Text";
            text.layer = UIlayer;

            textTx.color = Color.black;

            RectTransformZero(rectTransform);
        }
    }

    public static void RectTransformZero(RectTransform rectTransform)
    {
        rectTransform.localScale = Vector3.one;
        rectTransform.anchoredPosition = Vector2.zero;
        rectTransform.anchoredPosition3D = Vector3.zero;
    }
}

 

转载于:https://www.cnblogs.com/Yellow0-0River/p/8624935.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值