UGUI 快捷键创建UGUI组件

3519444_112940_9322

 

使用NGUI的时候还有xxx快捷键创建, spirte,label,button等等. 在UGUI里面的时候好像是没有快捷键的. 不知道以后多久才能有这个功能.  在家里闲无聊的时候写了一个脚本, 可以方便的创建UGUI组件(Button,Image,Text,InputFile等等)

快捷键列表如下:

Text Shift+Alt+L
Button Shift+Alt+B
Image Shift+Alt+S
InputField Shift+Alt+I

 

代码部分:

using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.UI;

/// <summary>
/// 根据快捷键创建UGUI控件
/// 快捷键符号% Ctrl  # Shift & Alt   
/// </summary>
public class UGUIShortcutKey : Editor
{

    public const int UIlayer = 5;         //UI

    [MenuItem("Plateface/CreateUGUI Text #&L")]
    public static void CreateText()
    {
        if (Selection.gameObjects.Length > 0) 
        {
            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.text = "plateface";

            textRect.localScale = new Vector3(1, 1, 1);
            textRect.anchoredPosition = Vector2.zero;
            textRect.anchoredPosition3D = Vector3.zero;


            RectTransformZero(textRect);
        }


    }

    [MenuItem("Plateface/CreateUGUI Button #&B")]
    public static void CreateButton()
    {
        if (Selection.gameObjects.Length > 0)
        {
            Debug.Log("创建按钮");
            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>();
            buttonTx.AddComponent<Text>();

            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 > 0)
        {
            Debug.Log("创建UGUI图片");
            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()
    {
        //创建UGUI标签
        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>();
        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 = new Vector3(1, 1, 1);
        rectTransform.anchoredPosition = Vector2.zero;
        rectTransform.anchoredPosition3D = Vector3.zero;
    }
}

 

插件下载地址http://yunpan.cn/cHYb3wR7fqnUU  访问密码 a91c

posted @ 2015-09-26 21:08  盘子脸  阅读(911)  评论(1编辑  收藏  举报