当前位置:   article > 正文

Unity Editor Window_unity editorwindow

unity editorwindow

Unity Editor Window

**EditorGUILayout:**提供Unity内置类或结构体的布局组件,能够实现在Inspcctor和EditorWindow中自定义编辑器。

EditorGUILayout是对GUILayout的扩展,它主要用于Inspector和EditorWindow。

EditorGUI是Fixed排列,EditorGUILayout则是Automatic;

基本控件

所提供的控件多以Field为后缀,主要用于属性字段

1.数值输入控件
FloatFiled / IntFiled/ Vector2Field / Vector2IntField / Vector3Field / Vector3IntField / Vector4Field / RectField

float sizeMultiplier = EditorGUILayout.FloatField("Increase scale by:", sizeMultiplier);
  • 1

滑动条:FloatSlider() / IntSlider():数值的输入

cloneTimesX = EditorGUILayout.IntSlider(cloneTimesX, 1, 10);
  • 1

2.文本输入:String变量
TextArea / TextField / PasswordFiled

3.Lable
LabelField():显示只读信息

EditorGUILayout.LabelField("Time since start: ",
            EditorApplication.timeSinceStartup.ToString());
  • 1
  • 2

4.Toggle:Bool型变量

isFPSOpen = EditorGUILayout.Toggle("Is OpenFPS",isFPSOpen);
  • 1

5.PropertyFiled()
用于自定义属性面板中,定义各个字段的布局;使用该函数就会根据属性来使用unity自动的布局;

EditorGUILayout.PropertyField(m_GameObjectProp, new GUIContent("Game Object"));
  • 1

6.ObjectField()
支持unity提供的各种类型的对象

source = EditorGUILayout.ObjectField(source, typeof(Object), true)
  • 1

7.一些类类型:Color、Curve等
ColorFiled / CurveFileld / TagFiled

8.其它常用类型
PasswordFiled /

组织控件

通常以Beginxxx()和Endxxx()形式成对出现,其代码包含的控件将被统一组织;

1.Group
将控件以分组形式排列(Group相当于一个Panel),当Group移动时,组内成员也会跟随;

BeginFadeGroup(float val) / EndFadeGroup() / BeginToggleGroup() / EndToggleGroup()

BeginFoldoutHeaderGroup(bool foldout,GUIContent,) / EndFoleoutHeaderGroup()

在EditorGUI中:BeginDisableGroup(bool) /

2.Horizontal / Vertical
管理控件的水平和竖直布局方式

3.ScrollView
管理滚动视图内的控件,BeginScrollView()和EndScrollView()

4.Space()
插入一个空行

具体用法:

标签 Label

GUILayout.Label("--------Label-------");
  • 1

文本框 TextField

string myString = "";
myString = EditorGUILayout.TextField("TrackName", myString);
  • 1
  • 2

下拉列表 Popup

public string[] options = new string[]
{
            "A", "B", "C",  "D", 
};
public int m_trackIndex = 0;

m_trackIndex = EditorGUILayout.Popup("TrackName", m_trackIndex,options);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

按钮 Button

if (GUILayout.Button("搜索"))
{
    Debug.Log("------Search click-------");
    
}
  • 1
  • 2
  • 3
  • 4
  • 5

批量创建按钮

// 
for(int i = 0; i < resList.Count; i++)
{
    // 创建按钮
    if (GUILayout.Button(resList[i]))
    {
        // 根据文件名在Project下选中文件
        Selection.activeObject = AssetDatabase.LoadAssetAtPath<GameObject>(m_path+resList[i]);	}
}

// update
foreach (var filename in resList)
{
	if (GUILayout.Button(filename))
    {
		Selection.activeObject = AssetDatabase.LoadAssetAtPath<GameObject>(m_path+filename);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

进度条 Slider

float myFloat;
myFloat = EditorGUILayout.Slider("Slider", myFloat, -3, 3);
  • 1
  • 2

文本区域

public string output = "A \n B \n";
EditorGUILayout.TextArea(output);
  • 1
  • 2

水平布局

Rect r = EditorGUILayout.BeginHorizontal("Button");
if (GUI.Button(r, GUIContent.none))
	Debug.Log("Go here");
GUILayout.Label("I'm inside the button");
GUILayout.Label("So am I");
EditorGUILayout.EndHorizontal();

EditorGUILayout.BeginHorizontal();
GUILayout.Label("第一个内容");
GUILayout.Label("第二个内容");
if (GUILayout.Button("第三个按钮"))
{
	Debug.Log("GUILayout的按钮");
}
EditorGUILayout.EndHorizontal();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

垂直布局

EditorGUILayout.BeginVertical();
GUILayout.Label("第一个内容");
GUILayout.Label("第二个内容");
if (GUILayout.Button("第三个按钮"))
{
	Debug.Log("GUILayout的按钮");
}
EditorGUILayout.EndVertical();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

选择区域 Toggle Group

bool groupEnabled;

groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);
            
m_para = EditorGUILayout.TextField("参数", m_para);

EditorGUILayout.EndToggleGroup();


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

折叠区域 Fold out

 bool showFoldout;
 showFoldout = EditorGUILayout.Foldout(showFoldout, "折叠子物体:");
        if (showFoldout)
        {
            // 缩进
            EditorGUI.indentLevel++;
            EditorGUILayout.LabelField("A");
            EditorGUI.indentLevel++;
            EditorGUILayout.LabelField("B");
            EditorGUI.indentLevel--;
            EditorGUI.indentLevel--;
            EditorGUILayout.LabelField("C");
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

滚动区域 Scroll View

Vector2 scroll;scroll =EditorGUILayout.BeginScrollView(scroll);                EditorGUILayout.EndScrollView();
  • 1

分割线

// splitter
float splitterWidth = 5;
// vertical
GUILayout.Box ("",
    GUILayout.Width(splitterWidth),
    GUILayout.MaxWidth (splitterWidth),
    GUILayout.MinWidth(splitterWidth),
    GUILayout.ExpandHeight(true));
// horizontal
GUILayout.Box ("",
    GUILayout.Height(splitterWidth),
    GUILayout.Height (splitterWidth),
    GUILayout.Height(splitterWidth),
    GUILayout.ExpandWidth(true));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

空行

EditorGUILayout.Space();
// 
GUILayout.FlexibleSpace();

  • 1
  • 2
  • 3
  • 4

Inspector 面板自定义 实例

1.基础类 – 使用默认布局

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Player : MonoBehaviour
{
 
    public int id;
 
    public string playerName;
    public string backStory;
    public float health;
    public float damage;
 
    public float weaponDamage1, weaponDamage2;
 
    public string shoeName;
    public int shoeSize;
    public string shoeType;
 
    void Start()
    {
        health = ;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

2.编辑器扩展脚本 – 自定义布局

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
 
//CustomEditor(typeof()) 用于关联你要自定义的脚本
[CustomEditor(typeof(Player))]
//必须要让该类继承自Editor,且不需要导入UnityEditor程序集
public class PlayerInspector : Editor
{
 
    Player player;
    bool showWeapons;
 
    void OnEnable()
    {
        //获取当前编辑自定义Inspector的对象
        player = (Player)target;
    }
 
    //执行这一个函数来一个自定义检视面板
    public override void OnInspectorGUI()
    {
        //设置整个界面是以垂直方向来布局
        EditorGUILayout.BeginVertical();
 
        //空两行
        EditorGUILayout.Space();
        EditorGUILayout.Space();
 
        //绘制palyer的基本信息
        EditorGUILayout.LabelField("Base Info");
        player.id = EditorGUILayout.IntField("Player ID", player.id);
        player.playerName = EditorGUILayout.TextField("PlayerName", player.playerName);
 
        //空三行
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
 
        //绘制Player的背景故事
        EditorGUILayout.LabelField("Back Story");
        player.backStory = EditorGUILayout.TextArea(player.backStory, GUILayout.MinHeight());
 
        //空三行
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
 
        //使用滑块绘制 Player 生命值
        player.health = EditorGUILayout.Slider("Health", player.health, , );
 
        //根据生命值设置生命条的背景颜色
        if (player.health < )
        {
            GUI.color = Color.red;
        }
        else if (player.health > )
        {
            GUI.color = Color.green;
        }
        else
        {
            GUI.color = Color.gray;
        }
 
        //指定生命值的宽高
        Rect progressRect = GUILayoutUtility.GetRect(, );
 
        //绘制生命条
        EditorGUI.ProgressBar(progressRect, player.health / 100.0f, "Health");
 
        //用此处理,以防上面的颜色变化会影响到下面的颜色变化
        GUI.color = Color.white;
 
        //空三行
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
 
        //使用滑块绘制伤害值
        player.damage = EditorGUILayout.Slider("Damage", player.damage, , );
 
        //根据伤害值的大小设置显示的类型和提示语
        if (player.damage < )
        {
            EditorGUILayout.HelpBox("伤害太低了吧!!", MessageType.Error);
        }
        else if (player.damage > )
        {
            EditorGUILayout.HelpBox("伤害有点高啊!!", MessageType.Warning);
        }
        else
        {
            EditorGUILayout.HelpBox("伤害适中!!", MessageType.Info);
        }
 
        //空三行
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
 
        //设置内容折叠
        showWeapons = EditorGUILayout.Foldout(showWeapons, "Weapons");
        if (showWeapons)
        {
            player.weaponDamage1 = EditorGUILayout.FloatField("Weapon 1 Damage", player.weaponDamage1);
            player.weaponDamage2 = EditorGUILayout.FloatField("Weapon 2 Damage", player.weaponDamage2);
        }
 
        //空三行
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
 
        //绘制鞋子信息
        EditorGUILayout.LabelField("Shoe");
        //以水平方向绘制
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Name", GUILayout.MaxWidth());
        player.shoeName = EditorGUILayout.TextField(player.shoeName);
        EditorGUILayout.LabelField("Size", GUILayout.MaxWidth());
        player.shoeSize = EditorGUILayout.IntField(player.shoeSize);
        EditorGUILayout.LabelField("Type", GUILayout.MaxWidth());
        player.shoeType = EditorGUILayout.TextField(player.shoeType);
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.EndVertical();
    }
 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130

效果如图所示:
img

Gui 实例

拖动调节分割线

public class GUISplitter: EditorWindow {
     Vector2 posLeft;
     Vector2 posRight;
     GUIStyle styleLeftView;
     GUIStyle styleRightView;
     float splitterPos;
     Rect splitterRect;
     Vector2 dragStartPos;
     bool dragging;
     float splitterWidth = 5;
     
     // Add menu named "My Window" to the Window menu
     [MenuItem ("GUI/GUISplitter")]
     static void Init () {
            GUISplitter window = (GUISplitter)EditorWindow.GetWindow (
                typeof (GUISplitter));
         window.position = new Rect(200, 200, 200,200);
         window.splitterPos = 100;
     }
     
     void OnGUI (){
         if (styleLeftView == null)
             styleLeftView = new GUIStyle(GUI.skin.box);
         if (styleRightView == null)
             styleRightView = new GUIStyle(GUI.skin.button);
         
         GUILayout.BeginHorizontal ();
     
         // Left view
         posLeft = GUILayout.BeginScrollView (posLeft, 
             GUILayout.Width (splitterPos), 
             GUILayout.MaxWidth(splitterPos), 
             GUILayout.MinWidth(splitterPos));
             GUILayout.Box ("Left View", 
                     styleLeftView, 
                     GUILayout.ExpandWidth(true), 
                     GUILayout.ExpandHeight(true));
         GUILayout.EndScrollView ();
         
         // Splitter
         GUILayout.Box ("", 
             GUILayout.Width(splitterWidth), 
             GUILayout.MaxWidth (splitterWidth), 
             GUILayout.MinWidth(splitterWidth),
             GUILayout.ExpandHeight(true));
         splitterRect = GUILayoutUtility.GetLastRect ();
     
         // Right view
         posRight = GUILayout.BeginScrollView (posRight, 
             GUILayout.ExpandWidth(true));
             GUILayout.Box ("Right View", 
             styleRightView, 
             GUILayout.ExpandWidth(true), 
             GUILayout.ExpandHeight(true));
         GUILayout.EndScrollView ();
         
         GUILayout.EndHorizontal ();
         
         // Splitter events
         if (Event.current != null) {
             // rawType 可以在屏幕外运行
             switch (Event.current.rawType) {
                 case EventType.MouseDown:
                     if (splitterRect.Contains (Event.current.mousePosition)) {
                         Debug.Log ("Start dragging");
                         dragging = true;
                     }
                     break;
                 case EventType.MouseDrag:
                     if (dragging){
                         Debug.Log ("moving splitter");
                         splitterPos += Event.current.delta.x;
                         Repaint ();
                     }
                     break;
                 case EventType.MouseUp:
                     if (dragging){
                         Debug.Log ("Done dragging");
                         dragging = false;
                     }
                     break;
             }
         }
     }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

参考资料

Unity—Inspector面板自定义

GUI splitter control

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/92980
推荐阅读
相关标签
  

闽ICP备14008679号