当类继承UI类时,序列化属性没法正常显示在在Inspector面板上
初始:
public class CircleImage : Image
{
private int segments;//片数
public float showPercent;
}
处理:
–静态字段需要使用[SerializeField],其作用是强制序列化静态字段
public class CircleImage : Image
{
[SerializeField]
private int segments;//片数
public float showPercent;
}
–Editor文件下创建脚本xxx.cs
自定义编辑器
using UnityEngine;
using UnityEditor;
//CustomEditor 自定义编辑器
//描述了用于编辑器实时运行类型的一个编辑器类。
//注意:这是一个编辑器类,如果想使用它你需要把它放到工程目录下的Assets/Editor文件夹下。
//编辑器类在UnityEditor命名空间下。所以当使用C#脚本时,你需要在脚本前面加上 "using UnityEditor"引用。
[CustomEditor(typeof(CircleImage), true)]
[CanEditMultipleObjects]
public class CircleImageEditor : UnityEditor.UI.ImageEditor
{
SerializedProperty _fillPercent;
SerializedProperty _segements;
protected override void OnEnable()
{
base.OnEnable();
// Setup the SerializedProperties.
_fillPercent = serializedObject.FindProperty("showPercent");
_segements = serializedObject.FindProperty("segments");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();绘制原本的信息//
// Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
serializedObject.Update();
// Show the custom GUI controls.--显示自定义GUI控件
EditorGUILayout.Slider(_fillPercent, 0, 1, new GUIContent("showPercent"));
EditorGUILayout.PropertyField(_segements);
// Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
serializedObject.ApplyModifiedProperties();
//下面不懂了...
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
}
}
效果: