1.Unity中,我们想自定义组件在Inspector面板中的属性显示。这时候就需要用到CustomEditor属性了。
2.实现案例:
[CustomEditor(typeof(CircleImage), true)]
[CanEditMultipleObjects]
public class CircleImageEditor : ImageEditor
{
private SerializedProperty _segements;
private SerializedProperty _showPercent;
private SerializedProperty _accurateRaycast;
protected override void OnEnable()
{
base.OnEnable();
_segements = serializedObject.FindProperty("segments");
_showPercent = serializedObject.FindProperty("showPercent");
_accurateRaycast = serializedObject.FindProperty("accurateRaycast");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
serializedObject.Update();
EditorGUILayout.Space();
EditorGUILayout.PropertyField(_segements);
EditorGUILayout.Slider(_showPercent, 0, 1, new GUIContent("showPercent"));
EditorGUILayout.PropertyField(_accurateRaycast);
serializedObject.ApplyModifiedProperties();
if (GUI.changed)
EditorUtility.SetDirty(target);
}
}
|