Unity之EditorGUILayout-Enum、Popup、EnumMaskField
一、在Inspector面板编辑显示 Enum 类型,如下图
创建 脚本 MaskT.CS
using UnityEngine;
using System.Collections;
public enum EventType
{
OnClick = 0,
Down,
Enter,
Exit,
Up,
None,
}
public class MaskT : MonoBehaviour {
public EventType eventType = EventType.None;
}
创建编辑器脚本MaskTEditor.CS
编辑 MaskT.CS
, 将 MaskTEditor.CS
放在 Editor 文件夹中
方法一:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
[CanEditMultipleObjects]
[CustomEditor(typeof(MaskT))]
public class MaskTEditor : Editor {
private MaskT _maskT;
private SerializedProperty eventType;
public void OnEnable()
{
_maskT = target as MaskT;
// 通过序列化获取MaskT 类定义的 eventType 字段,FindProperty ("字段")
eventType = serializedObject.FindProperty ("eventType");
}
public override void OnInspectorGUI ()
{
serializedObject.Update ();
EditorGUILayout.PropertyField (eventType, new GUIContent ("EventType"));
GUILayout.Space (10);
if (GUILayout.Button ("Debug"))
{
//获取枚举中包含的所有名字(字符串数组) OnClick, Down, Enter, Exit, Up, None,
string[] names = eventType.enumNames;
//选中的枚举项,取值范围 0 - 枚举长度 0 - 5
int selectIndex = eventType.enumValueIndex;
//打印选择的枚举名
Debug.Log("selectIndex :" + selectIndex + " selectName " +names[selectIndex]);
//将选择的枚举项转换为枚举类型
EventType selectEventType = (EventType)selectIndex;
Debug.Log("selectEventType :" + selectEventType);
//通过枚举值获取枚举名
string selectName = Enum.GetName(typeof(EventType), selectEventType);
Debug.Log("selectName :" + selectName);
}
GUILayout.Space (10);
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
// 保存序列化数据,否则会出现设置数据丢失情况
serializedObject.ApplyModifiedProperties ();
}
}
方法二:
改写 MaskTEditor.CS
脚本
MaskT.CS 脚本不变
using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
[CanEditMultipleObjects]
[CustomEditor(typeof(MaskT))]
public class MaskTEditor : Editor {
private MaskT _maskT;
public void OnEnable()
{
_maskT = target as MaskT;
}
public override void OnInspectorGUI ()
{
serializedObject.Update ();
GUILayout.Space (10);
//在Inspector面板显示 EventType 枚举类型
_maskT.eventType = (EventType)EditorGUILayout.EnumPopup ("EventType", _maskT.eventType, GUILayout.ExpandWidth(true));
GUILayout.Space (10);
if (GUILayout.Button ("Debug"))
{
//打印选中的枚举值
Debug.Log("eventType :" + _maskT.eventType);
}
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
// 保存序列化数据,否则会出现设置数据丢失情况
serializedObject.ApplyModifiedProperties ();
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
二、在Inspector 面板显示 Popup (下拉框)
依然使用 MaskT.CS
改写 MaskTEditor.CS
MaskTEditor.CS 代码如下
using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
[CanEditMultipleObjects]
[CustomEditor(typeof(MaskT))]
public class MaskTEditor : Editor {
private MaskT _maskT;
private SerializedProperty eventType;
private string[] eventNameArr; // 枚举名数组
private int selectIndex; // 保存枚举选中的项
public void OnEnable()
{
_maskT = target as MaskT;
eventType = serializedObject.FindProperty ("eventType");
eventNameArr = eventType.enumNames; //获取枚举中每项名
selectIndex = eventType.enumValueIndex; //获取枚举值的 int 类型值
}
public override void OnInspectorGUI ()
{
serializedObject.Update ();
GUILayout.Space (10);
//在Inspector面板显示 EventType 枚举类型
int index = selectIndex;
//首先获取枚举中的所有名字数组,通过 Popup将数组显示为下拉框,返回选中的项
selectIndex = EditorGUILayout.Popup("EventType", index, eventNameArr, GUILayout.ExpandWidth(true));
GUILayout.Space (10);
//
if (index != selectIndex)
{
//s将选择的项转换为枚举值
_maskT.eventType = (EventType)selectIndex;
//打印选中的枚举值
Debug.Log("selectIndex :" + selectIndex);
Debug.Log("selectName :" + eventNameArr[selectIndex]);
Debug.Log("selectEventType :" + (EventType)selectIndex);
Debug.Log("eventType :" + _maskT.eventType);
}
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
// 保存序列化数据,否则会出现设置数据丢失情况
serializedObject.ApplyModifiedProperties ();
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
三、在Inspector面板多选枚举,如tag、mask、layer
效果如下
改写 MaskT.CS
改写 MaskTEditor.CS
MaskT.CS 代码如下
using UnityEngine;
using System.Collections;
public enum EventType
{
OnClick = 0,
Down,
Enter,
Exit,
Up,
None,
}
public class MaskT : MonoBehaviour {
public EventType eventType = EventType.None;
void Update()
{
if (Input.GetKeyDown (KeyCode.A))
{
if (IsSelectEventType(EventType.OnClick))
{
Debug.Log("Select :" + EventType.OnClick);
}
}
}
//判断是否选择了该枚举值
public bool IsSelectEventType(EventType _eventType)
{
// 将枚举值转换为int 类型, 1 左移
int index = 1 << (int)_eventType;
// 获取所有选中的枚举值
int eventTypeResult = (int)eventType;
// 按位 与
if ((eventTypeResult & index) == index)
{
return true;
}
return false;
}
}
MaskTEditor.CS 如下
using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
[CanEditMultipleObjects]
[CustomEditor(typeof(MaskT))]
public class MaskTEditor : Editor {
private MaskT _maskT;
public void OnEnable()
{
_maskT = target as MaskT;
}
public override void OnInspectorGUI ()
{
serializedObject.Update ();
GUILayout.Space (10);
//返回值不是纯粹的 枚举值, 而是一个二进制数,
//我的枚举共 6 项,8位二进制数可以代表我的返回值
//选择 Noting 返回值为 0000 0000
//选择 Evert thing 返回值为 1111 1111
//选择 OnClick 返回值为 0000 0001
//选择 Down 返回值为 0000 0010
//同时选择OnClick, Down 返回值为 0000 0011
_maskT.eventType = (EventType)EditorGUILayout.EnumMaskField ("EventType", _maskT.eventType);
GUILayout.Space (10);
if (GUILayout.Button("Debug"))
{
// 直接打印 枚举值,得到的事错误值,
// 如同时选择 OnClick, Down 返回值为 0000 0011, 值为(3)
// 打印结果为 Enter
// Debug.Log(_maskT.eventType);
//获取选中结果
int select = (int)_maskT.eventType;
//遍历枚举中所有的项
for (int i = 0; i < 6; i ++)
{
EventType eventType = (EventType)i;
if ( _maskT.IsSelectEventType(eventType))
{
Debug.Log("selectEventType :" + eventType);
}
}
}
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
// 保存序列化数据,否则会出现设置数据丢失情况
serializedObject.ApplyModifiedProperties ();
}
}