zoukankan      html  css  js  c++  java
  • 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

    如果你感兴趣,你可以把你妹妹介绍给我
  • 相关阅读:
    IDEA SpringBoot项目连接数据库报Acess denied错误解决方法
    字符流中出现的第一个字符
    数组中重复的数字
    替换空格
    数组中次数超过一半的数字
    表示数值的字符串
    正则表达式匹配
    SpringMVC中的视图和视图解析器
    IntelliJ Idea取消Could not autowire. No beans of 'xxxx' type found的错误提示
    spring中注解注入 context:component-scan 的使用说明
  • 原文地址:https://www.cnblogs.com/plateFace/p/4841299.html
Copyright © 2011-2022 走看看