zoukankan      html  css  js  c++  java
  • (转)Unity笔记之编辑器(Foldout、HelpBox、InspectorTitlebar、Slider、MinMaxSlid ...

    1. Foldout、HelpBox
    折叠菜单,大家都知道,不具体解释了,直接代码。因为折叠菜单中必然是有内容才能看到效果,所以顺带把HelpBox(提示框)也说了。

    [code]csharpcode:

    using UnityEngine;
    using System.Collections;
    using UnityEditor; // 编辑器命名空间的引用
    
    public class Editor2 : EditorWindow // 编辑器类
    {
        private bool foldout; // 声明折叠菜单的状态
    
        [MenuItem("EditorDemo/CreateWindow")] // 在编辑器中添加一个菜单
        static void CreateWindow() // 下面这个函数必须是***静态的***
        {
            // 在这里面创建窗口
            EditorWindow.GetWindow(typeof(Editor2), false, "EditorWindow", true);
        }
    
        void OnGUI()
        {
            foldout = EditorGUILayout.Foldout(foldout, "Foldout"); // 定义折叠菜单
            if (foldout)
            {
                EditorGUILayout.HelpBox("HelpBox", MessageType.Error); // 显示一个提示框
            }
        }
    }
    
         
     
    折叠菜单的使用十分简单,一个状态值,一个标题名;要注意的是HelpBox中的MessageType,它有四种类型Error、Info、None、Warning。不同的消息类型,有不同的显示效果。
     
        
           
     
    2. InspectorTitlebar

    [code]csharpcode:

    using UnityEngine;
    using System.Collections;
    using UnityEditor; // 编辑器命名空间的引用
    
    public class Editor2 : EditorWindow // 编辑器类
    {
        private bool fold = false; // 声明折叠
        private GameObject selection; // 声明被选物体
    
        [MenuItem("EditorDemo/CreateWindow")] // 在编辑器中添加一个菜单
        static void CreateWindow() // 下面这个函数必须是***静态的***
        {
            // 在这里面创建窗口
            EditorWindow.GetWindow(typeof(Editor2), false, "EditorWindow", true);
        }
    
        void OnGUI()
        {
            if (Selection.activeGameObject)
            {
                selection = Selection.gameObjects[0];
                fold = EditorGUILayout.InspectorTitlebar(fold, selection); // 定义一个检视面板栏***Selection.objects表示当前被选中的物体***
                if (fold) // 控制折叠
                {
                    selection.transform.position = EditorGUILayout.Vector3Field("Position", selection.transform.position); // 定义一个Vector3输入区域
                }
            }
        }
    
        void OnInspectorUpdate()
        {
            this.Repaint(); // 刷新Inspector
        }
    }
    
         
     
    3. Slider、MinMaxSlider
    因为这两个比较相似,我就放在一块讲了,能有个对比。

    [code]csharpcode:

    using UnityEngine;
    using System.Collections;
    using UnityEditor; // 编辑器命名空间的引用
    
    public class Editor2 : EditorWindow // 编辑器类
    {
        private float value0 = 0f;
        private float value1 = 0f;
        private float value2 = 0f;
    
        [MenuItem("EditorDemo/CreateWindow")] // 在编辑器中添加一个菜单
        static void CreateWindow() // 下面这个函数必须是***静态的***
        {
            // 在这里面创建窗口
            EditorWindow.GetWindow(typeof(Editor2), false, "EditorWindow", true);
        }
    
        void OnGUI()
        {
            value0 = EditorGUILayout.Slider("Slider", value0, -50f, 50f); // 定义单滑块
            EditorGUILayout.MinMaxSlider(new GUIContent("MinMaxSlider"), ref value1, ref value2, -50f, 50f); // 定义双滑块
            EditorGUILayout.BeginHorizontal(); // 将下面两个值显示在同一行
            EditorGUILayout.FloatField(value1);
            EditorGUILayout.FloatField(value2);
            EditorGUILayout.EndHorizontal();
        }
    
        void OnInspectorUpdate()
        {
            this.Repaint(); // 刷新Inspector
        }
    }
    
        
    Slider中的参数包括:1. 标题;2. 值;3. 最小值;4. 最大值
    MinMaxSlider的参数包括:1. 标题(注意不是string而是GUIContent);2. 前一个滑块的值;3. 后一个滑块的值;4. 最小值;5. 最大值
  • 相关阅读:
    简单爬虫架构解析
    三种urllib实现网页下载,含cookie模拟登陆
    MySQL 从入门到删库
    Python Set
    Python dict
    Python tuple
    Python List
    死锁问题
    线程通信之生产者和消费者案例
    多线程安全和线程同步
  • 原文地址:https://www.cnblogs.com/backlighting/p/5061576.html
Copyright © 2011-2022 走看看