Question by
Ardenian · Feb 10, 2017 at 07:26 PM ·
inspectorserializationmonobehaviourcustom editorcustom inspector
Target field of a MonoBehaviour attribute ?
I have these classes:
public class MyClass : MonoBehaviour{
[SerializeField]
MonoBehaviour myOtherMonoBehaviour;
}
public class MyOtherClass : MonoBehaviour{
[SerializeField]
float value;
}
I would now like to write a custom inspector for MyClass which checks if myOtherMonoBehaviour is of type MyOtherClass and if so, show its field value
in the inspector of MyClass.
How do I write the check for the Type and displaying value
in the custom editor ? I know EditorGUILayout.PropertyField
for displaying a property field for a serialized object, but how do I target value
, if myOtherMonoBehaviour is of type MyOtherClass ?
Comment
Answer by Adam-Mechtley · Feb 10, 2017 at 08:30 PM
In general I would advise against it, but (without any eye toward optimization) you can do something like this:
SerializedProperty myOtherMonoBehaviour = serializedObject.FindProperty("myOtherMonoBehaviour");
EditorGUILayout.PropertyField(myOtherMonoBehaviour);
if (myOtherMonoBehaviour.objectReferenceValue != null and myOtherMonoBehaviour.objectReferenceValue is MyOtherClass)
{
SerializedObject so = new SerializedObject(myOtherMonoBehaviour.objectReferenceValue);
so.Update();
EditorGUILayout.PropertyField(so.FindProperty("value"));
so.ApplyModifiedProperties();
}