Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thebne/42274f74e0d7f08c56eb4a2b444d45ad to your computer and use it in GitHub Desktop.
Save thebne/42274f74e0d7f08c56eb4a2b444d45ad to your computer and use it in GitHub Desktop.
PropertyField.RegisterValueChangeCallback but before 2020
// see https://forum.unity.com/threads/uielements-developer-guide.648043/#post-6073137
void RegisterPropertyChangeCallback(PropertyField field, SerializedProperty property, FieldInfo fieldInfo, Action callback)
{
var propertyType = property.propertyType;
switch (propertyType)
{
case SerializedPropertyType.Integer:
case SerializedPropertyType.LayerMask:
case SerializedPropertyType.ArraySize:
field.RegisterCallback<ChangeEvent<int>>( (evt) => callback());
break;
case SerializedPropertyType.Boolean:
field.RegisterCallback<ChangeEvent<bool>>( (evt) => callback());
break;
case SerializedPropertyType.Float:
field.RegisterCallback<ChangeEvent<float>>( (evt) => callback());
break;
case SerializedPropertyType.String:
case SerializedPropertyType.Character:
field.RegisterCallback<ChangeEvent<string>>( (evt) => callback());
break;
case SerializedPropertyType.Color:
field.RegisterCallback<ChangeEvent<Color>>( (evt) => callback());
break;
case SerializedPropertyType.ObjectReference:
field.RegisterCallback<ChangeEvent<UnityEngine.Object>>( (evt) => callback());
break;
case SerializedPropertyType.Enum:
{
Type enumType = fieldInfo.FieldType;
if (enumType.IsDefined(typeof(FlagsAttribute), false))
{
field.RegisterCallback<ChangeEvent<Enum>>( (evt) => callback());
}
else
{
field.RegisterCallback<ChangeEvent<string>>( (evt) => callback());
}
break;
}
case SerializedPropertyType.Vector2:
field.RegisterCallback<ChangeEvent<Vector2>>( (evt) => callback());
break;
case SerializedPropertyType.Vector3:
field.RegisterCallback<ChangeEvent<Vector3>>( (evt) => callback());
break;
case SerializedPropertyType.Vector4:
field.RegisterCallback<ChangeEvent<Vector4>>( (evt) => callback());
break;
case SerializedPropertyType.Rect:
field.RegisterCallback<ChangeEvent<Rect>>( (evt) => callback());
break;
case SerializedPropertyType.AnimationCurve:
field.RegisterCallback<ChangeEvent<AnimationCurve>>( (evt) => callback());
break;
case SerializedPropertyType.Bounds:
field.RegisterCallback<ChangeEvent<Bounds>>( (evt) => callback());
break;
case SerializedPropertyType.Gradient:
field.RegisterCallback<ChangeEvent<Gradient>>( (evt) => callback());
break;
case SerializedPropertyType.Vector2Int:
field.RegisterCallback<ChangeEvent<Vector2Int>>( (evt) => callback());
break;
case SerializedPropertyType.Vector3Int:
field.RegisterCallback<ChangeEvent<Vector3Int>>( (evt) => callback());
break;
case SerializedPropertyType.RectInt:
field.RegisterCallback<ChangeEvent<RectInt>>( (evt) => callback());
break;
case SerializedPropertyType.BoundsInt:
field.RegisterCallback<ChangeEvent<BoundsInt>>( (evt) => callback());
break;
case SerializedPropertyType.Quaternion:
case SerializedPropertyType.ExposedReference:
case SerializedPropertyType.FixedBufferSize:
case SerializedPropertyType.Generic:
default:
// Not supported for now
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment