using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SortOrder : MonoBehaviour
{
public enum EnumValue
{
EnumValue1,
EnumValue2,
EnumValue3,
}
public int intValue;
[SerializeField] //如果是私有的则必须序列化,editor才能访问到
private bool boolValue;
public EnumValue enumValue;
public Vector3 vec3;
public GameObject go;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
//然后使用[CustomEditor(typeof(MonoTest), true)]特性来描述要自定义的是哪个类, 第二个参数代表是否对其子类起效.
//然后将类继承父类public class MonoTestEditor : Editor.
//CustomEditor类有两个关键的属性target和serializedObject, 前者代表要自定义的类的实例化对象, 默认是Object类型,
//我们一般在OnEnable里将其转换为对应的类: _target = target as MonoTest;,
//serializedObject属性封装了对应类上所有的序列化字段和一些有用的方法,
//每个字段对应一个序列化属性SerializedProperty, 并在使用之前把两者做绑定:
//m_IntValue = serializedObject.FindProperty("intValue");.
[CustomEditor(typeof(SortOrder))]
public class Edtor_SortOrder : Editor
{
private SortOrder _target;
private SerializedProperty m_IntValue;
private SerializedProperty m_BoolValue;
private SerializedProperty m_EnumValue;
private SerializedProperty m_Vec3Value;
private SerializedProperty m_goValue;
private void OnEnable()
{
_target = target as SortOrder;
_target.intValue = 9;
m_IntValue = serializedObject.FindProperty("intValue");
m_BoolValue = serializedObject.FindProperty("boolValue");
m_EnumValue = serializedObject.FindProperty("enumValue");
m_Vec3Value = serializedObject.FindProperty("vec3");
m_goValue = serializedObject.FindProperty("go");
}
public override void OnInspectorGUI()
{
//base.OnInspectorGUI();
//GUILayout.Label("editor sort order");
//GUILayout.Button("a的设置");
//GUILayout.Width(100);
//if(GUILayout.Button(new GUIContent("b的设置"), GUILayout.Width(100), GUILayout.Width(100);))
//{
// //serializedObject.targetObject
//}
//***************
// base.OnInspectorGUI(); // 将默认的绘制关闭
serializedObject.Update();
// 使用基本的绘制
// EditorGUILayout.PropertyField(m_IntValue, new GUIContent("这是一个整型值的提示"));
EditorGUILayout.PropertyField(m_BoolValue);
// EditorGUILayout.PropertyField(m_EnumValue);
EditorGUILayout.PropertyField(m_goValue);
EditorGUILayout.PropertyField(m_Vec3Value);
m_goValue.objectReferenceValue = EditorGUILayout.ObjectField("ObjectField物体", m_goValue.objectReferenceValue, typeof(GameObject), true) as GameObject;
var content = new GUIContent { text = "整型值", tooltip = "这是一个整型值的提示" };
EditorGUILayout.IntSlider(m_IntValue, 0, 100, content); // 使用一个滑动条来代替基本的绘制, 可以规定上下界, 可以修改相关提示
var curValue = m_BoolValue.boolValue;
m_BoolValue.boolValue = EditorGUILayout.Toggle("布尔值", m_BoolValue.boolValue);
if (m_BoolValue.boolValue != curValue)
Debug.Log("value changed!" + m_BoolValue.boolValue); // 在值变化后进行一些操作
//m_BoolValue.boolValue = EditorGUILayout.Toggle("布尔值", m_BoolValue.boolValue);
//if (serializedObject.ApplyModifiedProperties())
// Debug.LogError("2value changed!" + m_BoolValue.boolValue); // 在值变化后进行一些操作
//GUI.enabled = false;//禁止对某些字段进行修改, 这时只需要在绘制字段前后对GUI开关即可,表示不可编辑
m_EnumValue.intValue = (int)(SortOrder.EnumValue)EditorGUILayout.EnumPopup("enumValue", (SortOrder.EnumValue)m_EnumValue.intValue); // 使用自定义的枚举值
//GUI.enabled = true;
serializedObject.ApplyModifiedProperties();
}
}