zoukankan      html  css  js  c++  java
  • Unity3D的菜单及编辑器扩展

    一、菜单栏扩展

      先看一下我们要实现的效果:

      也就是在Unity3D的菜单栏中添加属于我们自己的菜单。同理你也可以把子菜单添加进系统菜单来定制属于自已开发习惯的功能。

      下面我们来看代码:

      

    using UnityEditor;
    public class Editor : MonoBehaviour {
        [MenuItem("我是菜单/历史信息", false, 1)]
        static void Show() {
            print("历史信息");
        }
        [MenuItem("我是菜单/当前信息", false,2)]
        static void Current() {
            print("当前信息");
        }
    
        [MenuItem("我是菜单/随便信息",true, 1)]
        static void Historyt()
        {
            print("随便信息");
        }
        [MenuItem("我是菜单/我有子菜单/子菜单1")]
        static void Menu1() {
            print("子菜单1");
        }
        [MenuItem("我是菜单/我有子菜单/子菜单2")]
        static void Menu2() {
            print("子菜单2");
        }
    }

      我遇到的坑:

        首先要导入命名空间using UnityEditor;

        [MenuItem]后的true是不可用 false是可用。

        同理后面的值也是控制子菜单的顺序的,值越小,越在上面。

    二、编辑器扩展

      先看一下结果:

      这组代码实现的就是上面的InspectorMenu脚本上的功能了。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Tool;
    
    public class InspectorMenu : MonoBehaviour {
        private Texture t;
        public Rect r;
        public string s;
        public float slider;
       
        [HideInInspector]
    
        [Range(0, 100)]
        public float health;
    
        public float t1;
        public float t2;
        public bool isOpen=true;
        public EType et = EType.up;
        public Texture T
        {
            get
            {
                return t;
            }
    
            set
            {
                t = value;
            }
        }
        [Range(0, 60)]
        public float demage;
        [Header("Weapon")]
        public string Weapon;
        public float weaponDamage;
        public string Discrip;
    
    }
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    using Tool;
    [CustomEditor(typeof(InspectorMenu))]
    public class InpectorTest : Editor {
    
        bool isSelect;
        bool isO;
        bool isOpen=true;//控制一组属性
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            InspectorMenu ins = (InspectorMenu)target;
            ins.T = EditorGUILayout.ObjectField("贴图",ins.T,typeof(Texture),true )as Texture;
            ins.r = EditorGUILayout.RectField("坐标", ins.r);
            ins.s = EditorGUILayout.TextField("备注",ins.s);
            ins.slider = EditorGUILayout.Slider("进度条",ins.slider,0f,1f);
            if (ins.health > 80) {
                GUI.color = Color.green;
            }
            if (ins.health < 20) {
                GUI.color = Color.red;
            }
            ins.health = EditorGUILayout.Slider("生命值", ins.health, 0, 100);
            GUI.color = Color.white;
            isSelect = EditorGUILayout.Toggle("是否开启", isSelect);
            GUILayout.Label("……附加信息……");
            isO = EditorGUILayout.BeginToggleGroup("是否开启", isO);
            ins.t1 = EditorGUILayout.FloatField("值1", ins.t1);
            ins.t2 = EditorGUILayout.FloatField("值2", ins.t2);
            ins.isOpen = EditorGUILayout.Toggle("开启", ins.isOpen);
            EditorGUILayout.EndToggleGroup();
            ins.et =(EType)EditorGUILayout.EnumPopup("方向", ins.et);
            //多行文本框
            EditorGUILayout.LabelField("描述");
            ins.Discrip = EditorGUILayout.TextArea(ins.Discrip);
    
            //控制伤害值来显示提示信息
            ins.demage = EditorGUILayout.Slider("伤害值", ins.demage, 0, 60f);
            if (ins.demage < 15) {
    
                EditorGUILayout.HelpBox("伤害值低",MessageType.Info);
            }
            if (ins.demage > 50) {
    
                EditorGUILayout.HelpBox("伤害值过低", MessageType.Warning);
            }
    
            //
            if (isOpen) {
    
                EditorGUI.indentLevel = 1;
                ins.Weapon = EditorGUILayout.TextField("武器名称:",ins.Weapon);
                EditorGUI.indentLevel = 2;
                ins.weaponDamage = EditorGUILayout.Slider("伤害值", ins.weaponDamage, 0, 100f);//失误一:注意菜单上的类型。
    
    
            }
            //一个人物选择按钮
            if (GUILayout.Button("人物选择")) {
                Debug.Log("您进入了人物选择系统");
            }
    
            
        }
    }
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    namespace Tool
    {
        public enum EType{
            up,
            down,
            left,
            right
        
        }
        public class EnumType : MonoBehaviour
        {
    
            // Use this for initialization
            void Start()
            {
    
            }
    
            // Update is called once per frame
            void Update()
            {
    
            }
        }
    }

      最后一个类是用枚举来实现下拉列表的。今天就到这里了,在写代码中遇到任何问题请联系我。

  • 相关阅读:
    Codeforces 271 Div 2 B. Worms
    Codeforces 271 Div 2 A Keyboard
    CSU 1333 Funny Car Racing (最短路)
    CSU 1337 搞笑版费马大定理(2013湖南省程序设计竞赛J题)
    CSU 1328 近似回文词(2013湖南省程序设计竞赛A题)
    HDU 5033 Building
    HDU 1058 Humble Numbers(离线打表)
    HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006
    HDU 5053 the Sum of Cube
    MySQL练习-employees数据库(二)
  • 原文地址:https://www.cnblogs.com/shuanglu/p/8306479.html
Copyright © 2011-2022 走看看