1.基本结构
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
| using UnityEngine;
[CustomEditor (typeof (Player)] public class PlayerEditor: Editor { private Player _Component; private void OnEnable () { _Component = target as Player; } private void OnDisable () { _Component = null; } public override void OnInspectorGUI () { } }
|
2.绘制GUI 基本数据类型
1.标题显示
1
| EditorGUILayout.LabelField("人物相关属性");
|
2.文本
1
| _Component.Name = EditorGUILayout.TextField("玩家名称", _Component.Name);
|
3.浮点数
1
| _Component.Atk = EditorGUILayout.FloatField("玩家攻击力", _Component.Atk);
|
4.布尔
1
| _Component.isMan = EditorGUILayout.Toggle("是否为男性", _Component.isMan);
|
5.向量
1
| _Component.HeadDir = EditorGUILayout.Vector3Field("头部方向", _Component.HeadDir)
|
6.颜色
1
| _Component.Hair = EditorGUILayout.ColorField("头发颜色", _Component.Hair);
|
3.绘制GUI 对象数据类型
EditorGUILayout.ObjectField
//参数1:标题
//参数2:原始组件的值
//参数3:成员变量的类型
//参数4:是否可以将场景中的对象拖给这个成员变量
1.对象数据类型
1 2 3
| _Component.Weapon = EditorGUILayout.ObjectField("持有武器", _Component.Weapon, typeof(GameObject), true) as GameObject;
_Component.Cloth = EditorGUILayout.ObjectField("衣服材质贴图", _Component.Cloth, typeof(Texture), false) as Texture;
|
4.绘制GUI 枚举数据类型
EditorGUILayout.EnumPopup
EditorGUILayout.EnumFlagsField
1.枚举数据类型绘制
1 2 3 4 5 6 7 8 9
|
_Component.Pro = (PlayerProfression)EditorGUILayout.EnumPopup("玩家职业", _Component.Pro);
_Component.LoveColor = (PlayerLoveColor)EditorGUILayout.EnumFlagsField("玩家喜欢的颜色", _Component.LoveColor);
|
5.绘制GUI 终极数据类型绘制
1.终极数据类型绘制
1 2 3 4 5 6 7 8
| serializedObject.Update();
SerializedProperty sp = serializedObject.FindProperty("Items");
EditorGUILayout.PropertyField(sp, new GUIContent("道具信息"), true);
serializedObject.ApplyModifiedProperties();
|
6.绘制GUI 滑动条
EditorGUILayout.Slider
1.滑动条绘制
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| _Component.Atk = EditorGUILayout.Slider(new GUIContent("玩家攻击力"), _Component.Atk, 0, 100);
if (_Component.Atk > 80) { EditorGUILayout.HelpBox("攻击力太高了", MessageType.Error); }
if (_Component.Atk < 20) { EditorGUILayout.HelpBox("攻击力太低了", MessageType.Warning); }
|
7.绘制GUI按钮显示和元素排列
GUILayout.Button
1.按钮显示和元素排列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| GUILayout.Button("来个按钮"); GUILayout.Button("来个按钮");
if (GUILayout.Button("测试点击")) { Debug.Log("测试点击"); }
EditorGUILayout.BeginHorizontal();
GUILayout.Button("再来个按钮"); GUILayout.Button("再来个按钮");
EditorGUILayout.EndHorizontal();
|