Caching data for a PropertyDrawer

I’m creating a PropertyDrawer for an object that takes a lot of CPU power to get or set it. Since an instance of a PropertyDrawer is reused for multiple properties, I need a way to cache the data in a dictionary.

In order for this to work, there must be some way to uniquely identity a property. I’ve tried the following in OnGUI():

  • property.GetHashCode - doesn’t work because a new instance of SerializedProperty is created every time
  • property.name - Seemed to work at first, but not in an array. All names become “data”
  • property.CountInProperty() - Crashes Unity with “m_ByteOffset < m_Data->size ()

My code looks like this:

public class Drawer : PropertyDrawer
	{
		private Dictionary<string, Data> allData = new Dictionary<string, Data>();

		public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
		{
			Data data;
			allData.TryGetValue(property.somethingUnique, out data);

			if (data == null)
			{
				data = new Data(property);
				allData.Add(property.somethingUnique, data);
			}

			// do things with data
		}
	}

Oh, silly me. I can just use the GUIContent label text. That should be unique in almost all cases.

Edit:
property.propertyPath works better because the result will be unique even with nesting.

Now I can have infinitely nested reorderable lists!

alt text