(转)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. 最大值

转载于:https://www.cnblogs.com/backlighting/p/5061576.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值