Setup
I have constructed a custom PropertyDrawer for a custom PropertyAttribute to create something similar to the RangeAttribute
, but for all data types. My PropertyDrawer requires an int to draw the inspector UI because ‘the int’ dictates the UI. In most other PropertyDrawers you can assume the data type under the attribute (i.e. float, Vector2, etc.) and have the PropertyDrawer read and write to that data type.
###Question###
However, I can’t assume the data type because I’d like this attribute to be available for all data types. Therefore, I can’t store ‘the int’ in the method mentioned above. I need a way to serialize ‘the int’ so that it persists through assembly reload and Unity closing. Got any ideas?
###Attempts###
- Creating a serializable ScriptableObject class to hold ‘the int’ and reference that in either my custom PropertyDrawer or PropertyAttribute.
- Throw around [SerializeField] and [Serializable] attributes on ‘the int’ and PropertyDrawer and PropertyAttribute classes
- Try to learn more about serialization, but PropertyDrawers or PropertyAttributes are never mentioned.
###Example###
public class MyBehavior : MonoBehaviour
{
[My]
public Vector2 data1;
[My(2)]
public SomeSerializedClass data2;
}
[AttributeUsage(AttributeTargets.Field)]
public class MyAttribute : PropertyAttribute
{
public int value; // what needs to be serialized
public MyAttribute(int startingValue = 1)
{
value = startingValue;
}
}
[CustomPropertyDrawer(typeof(MyAttribute))]
public class MyDrawer : PropertyDrawer
{
private int value; // or maybe this can be serialized instead?
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var attr = attribute as MyAttribute;
value = attr.value;
// Here I need `value` to be persistent upon Unity open/close
// and script reloading, hence the issue.
value = EditorGUI.IntSlider(position, value, 1, 2);
// ... draw `property` based on `value` ...
}
}
value
must be serialized within MyAttribute
or MyDrawer
due to value
’s independence on property
’s underlying type.
Please let me know what I need to clarify. Thanks for any help!