Skip to content

Instantly share code, notes, and snippets.

@simonwittber
Last active February 22, 2018 09:26
Show Gist options
  • Save simonwittber/ce2461ed412d65803248a9cc2ebbd15f to your computer and use it in GitHub Desktop.
Save simonwittber/ce2461ed412d65803248a9cc2ebbd15f to your computer and use it in GitHub Desktop.
A unique ID for your Serialized classes in Unity.
[Serializable]
public class MyClass {
public int id;
}
[CustomPropertyDrawer(typeof(MyClass))]
public class MyClassDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var idProperty = property.FindPropertyRelative("id");
idProperty.intValue = property.propertyPath.GetHashCode();
}
}
@simonwittber
Copy link
Author

Note, the GetHashCode() method will probably return a unique ID. There is a very remote chance you will get a clashing ID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment