Unity 自定义窗口

放在Editor文件夹下;
在这里插入图片描述

#if UNITY_EDITOR

using System;
using UnityEditor;
using UnityEngine;


namespace EditorCustumTool
{
    /// 
    /// 自定义窗口
    /// 
    public class CustomWindow : EditorWindow
    {
        public enum FlagType
        {
            Flag1 = 101,
            Flag2 = 102,
            Flag3 = 103,
        }


        private string text1 = "";
        private float number1 = 0;

        private string text2 = "";
        private float number2 = 0;

        private float sliderValue;

        private float scrollBarHorizontal;
        private float scrollBarVertical;

        private int gridIndex = 0;
        private int toolbarIndex = 0;

        private FlagType flag;

        private int popupindex;

        private bool isOn = false;

        private CustomWindow()
        {
            this.titleContent = new GUIContent("自定义窗口");
        }

        [MenuItem("自定义工具/自定义窗口")]
        private static void ShowWindow()
        {
            EditorWindow.GetWindow(typeof(CustomWindow));
        }

        private void OnGUI()
        {
            EditorGUILayout.BeginVertical();

            // 标题
            GUILayout.Space(10);
            GUI.skin.label.fontSize = 15;
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            GUILayout.Label("自定义窗口");

            // 内容        
            GUI.skin.label.fontSize = 12;
            GUI.skin.label.alignment = TextAnchor.UpperLeft;


            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            GUILayout.Label("文本1", GUILayout.MaxWidth(60));
            text1 = EditorGUILayout.TextArea(text1, GUILayout.MaxHeight(20));
            GUILayout.EndHorizontal();


            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            GUILayout.Label("数值1", GUILayout.MaxWidth(60));
            number2 = EditorGUILayout.FloatField(number2, GUILayout.MaxHeight(20));
            GUILayout.EndHorizontal();


            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            text1 = EditorGUILayout.TextField("文本2", text1);
            GUILayout.EndHorizontal();


            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            number2 = EditorGUILayout.FloatField("数值2", number2);
            GUILayout.EndHorizontal();


            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            sliderValue = EditorGUILayout.Slider("滑动条", sliderValue, -5, 5);
            GUILayout.EndHorizontal();


            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            scrollBarHorizontal = GUILayout.HorizontalScrollbar(scrollBarHorizontal, 0, 0, 100, new[] { GUILayout.Width(100) });
            scrollBarVertical = GUILayout.VerticalScrollbar(scrollBarVertical, 0, 0, 100, new[] { GUILayout.Height(100) });
            GUILayout.EndHorizontal();


            // 按钮控件
            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            gridIndex = GUILayout.SelectionGrid(gridIndex, new[] { "1", "2", "3", "4", "5", "6" }, 4);
            GUILayout.EndHorizontal();


            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            toolbarIndex = GUILayout.Toolbar(toolbarIndex, new[] { "1", "2", "3", "4", "5", "6" });
            GUILayout.EndHorizontal();


            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            // 这个单选
            flag = (FlagType)EditorGUILayout.EnumPopup(new GUIContent("FlagType", null, "ToolTip"), flag);
            // 这个可以多选
            //flag = (FlagType)EditorGUILayout.EnumFlagsField(new GUIContent("FlagType", null, "ToolTip"), flag);
            GUILayout.EndHorizontal();


            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            //一个选择框,每个选择框里表示一个Int数
            popupindex = EditorGUILayout.IntPopup("IntPopup", popupindex, new[] { "a", "b", "c" }, new[] { 1, 2, 3 });
            GUILayout.EndHorizontal();


            // 展示信息
            GUILayout.Space(10);
            var noSelected = Selection.transforms == null || Selection.transforms.Length == 0;
            var message = noSelected ? "请选择一个物体" : $"当前选中了{Selection.transforms.Length}个物体";
            var messageType = noSelected ? MessageType.Error : MessageType.Info;
            EditorGUILayout.HelpBox(message, messageType);

            EditorGUILayout.Space(10);
            if (GUILayout.Button("打印选中的物体名"))
                DebugSelectedObjName();

            GUILayout.EndVertical();
        }

        private void DebugSelectedObjName()
        {
            if (Selection.transforms.Length > 0)
            {
                foreach (var trans in Selection.transforms)
                    Debug.LogError(trans.name);
            }
            else
            {
                Debug.LogError("当前没选中任何物体");
            }
        }
    }
}

#endif

效果如图所示
Unity 自定义窗口_第1张图片

你可能感兴趣的:(Unity开发,unity,游戏引擎)