Wednesday, June 10, 2015

Unity3d Serializable Class Object Inspector

If you want to use ur class object inside Editor or Inspector
you need to defined as Serializable
[Serializable]
class MyClass: ScriptableObject{
   public int prop;

}

Behind the scene Unity use SerializedObject(UnityEngine.Object), that you can use to find SerializedProperty and show in OnGUI. Or if you just use it in Behaviour, Unity would do it automatically for u.

class MyBehaviour:Behaviour{
 public MyClass myObject; // Unity will show its public properties in Foldout. }

 But what If I've lot of serializable classes objects and I want to be able to switch and see its properties.


In order to use EditorGUI functions for Object class properties you need SerializedProperty.

You can create runtime ScriptableObjectTemplate class, object in memory,
 using UnityEngine;
public class ScriptableObjectTemplate:ScriptableObject{
 public MyClass value;
 }
 with only one property(named:value) of  type MyClass. Serialize by use of SerializedObject and FindProperty "value" as SerializedProperty.

__serializedProperty=SerializeVariable().FindProperty("value");

Then you can create your default drawer:
UnityDefaultPropertyDrawer drawer=new UnityDefaultPropertyDrawer();
drawer.OnGUI(rect,__serializedProperty,label)


No comments:

Post a Comment