TimeSpan的自定义PropertyDrawer,发布设置SerializedProperty longValue我已经遵循了可以找到的所有资源,但是无论我做什么,我的SerializedProperty似乎都没有价值。
UTimeSpan.cs:
[System.Serializable]
public class UTimeSpan
{
public UTimeSpan() { }
public UTimeSpan(double ticks)
{
Ticks = ticks;
}
[SerializeField]
public long Ticks;
private TimeSpan timeSpan;
public static implicit operator TimeSpan(UTimeSpan uts)
=> uts.timeSpan;
public static implicit operator UTimeSpan(TimeSpan ts)
=> new UTimeSpan(ts.Ticks);
}
TimeSpanPropertyDrawer.cs:
[CustomPropertyDrawer(typeof(UTimeSpan))]
public class TimeSpawnPropertyDrawer : PropertyDrawer
{
public override void OnGUI(UnityEngine.Rect position, SerializedProperty property, UnityEngine.GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.PrefixLabel(position, label);
SerializedProperty ticks = property.FindPropertyRelative("Ticks");
TimeSpan ts = new TimeSpan(ticks.longValue);
int days = (int)ts.TotalDays;
days = EditorGUI.IntField(new Rect(
position.x, position.y,
50, position.height
), days);
ts = new TimeSpan(days, 0, 0);
ticks.serializedObject.Update();
ticks.longValue = ts.Ticks;
ticks.serializedObject.ApplyModifiedProperties();
EditorGUI.LabelField(new Rect(
position.x + 110, position.y,
100, position.height
), ts.ToString());
EditorGUI.EndProperty();
}
}
但是Ticks始终为0。给我的印象是,手动设置SerializedProperty只需执行以下步骤:
我在这里想念什么吗?预先感谢!
这不是序列化问题,但有错字。
ts = new TimeSpan(days, 0, 0, 0);
PS:UTimeSpan应该是一个结构,因为它非常简单(正确的是)。另外,timeSpan可以只是一个属性,也可以(甚至更好)内联到现有的隐式运算符中:]
TimeSpan timeSpan => new TimeSpan( this.Ticks );