- Home /
Property Drawer and Inheritance
Hello, I am trying to create a very basic engine with a custom editor/property drawers and I seem to be messing it up royally. I am semi-familiar with property drawers and editors, as well as how they work together but this really has me stumped.
I have three classes, an Engine (with an array of waypoints), A Waypoint (with an enum to store the waypoint type), and a MoveWaypoint (an example waypoint type with custom data - extends Waypoint).
The engine has a custom inspector that just displays the array of waypoints (for now!) and is working as intended. The Waypoint has a property drawer that displays the enum for the user to choose. What I am trying to do is if the user sets the Waypoint to Move, to show the MoveWaypoint propertydrawer. I have tried a few different ways of doing this (after crawling through lots and lots of forums) but all the information is either out of date or I can't get it to work.
Currently the three scripts are just test beds for the engine, so it may seem silly to go through a lot of effort for just these three scripts. However, I am eventually going to have over two dozen scripts each with their own sets of variables and manipulations so I want to get this figured out (assuming it is possible) before I dive too deep down the rabbit hole!
I appreciate any and all help, all scripts are provided below.
LevelEngine:
using UnityEngine;
using System.Collections;
public class LevelEngine: MonoBehaviour {
public WaypointScript[] waypoints;
}
WaypointScript:
public enum MovementTypes
{
WAIT,
MOVE
};
[System.Serializable]
public class WaypointScript{
public MovementTypes moveType;
}
and MoveWaypointScript:
using UnityEngine;
[System.Serializable]
public class MoveWaypointScript: WaypointScript{
public Vector3 target;
public float moveTime;
}
Each has their own custom inspector/editor declared
EngineEditor (Works fine?):
using UnityEditor;
[CustomEditor(typeof(LevelEngine))]
public class EngineEditor : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update ();
SerializedProperty waypointsArray = serializedObject.FindProperty ("waypoints");
EditorGUILayout.PropertyField(waypointsArray);
EditorGUILayout.PropertyField(waypointsArray.FindPropertyRelative("Array.size"));
for (int i = 0; i < waypointsArray.arraySize; i++)
{
EditorGUILayout.PropertyField(waypointsArray.GetArrayElementAtIndex(i));
}
serializedObject.ApplyModifiedProperties();
}
}
WaypointEditorDrawer - Works Fine, but there is where the real problem arises -
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomPropertyDrawer(typeof(ScriptWaypoint), false)]
public class WaypointEditorDrawer : PropertyDrawer
{
ScriptWaypoint waypointScript;
float extraHeight = 17f;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
SerializedProperty movementTypes = property.FindPropertyRelative("moveType");
EditorGUILayout.PropertyField(movementTypes);
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return base.GetPropertyHeight(property, label) + (extraHeight );
}
}
And finally MovementEditorDrawer:
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomPropertyDrawer(typeof(ScriptMove))]
public class MovementEditorDrawer : PropertyDrawer {
ScriptMove moveScript;
float extraHeight = 55f;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
SerializedProperty moveTime = property.FindPropertyRelative("moveTime");
EditorGUI.PropertyField(position, moveTime);
EditorGUI.EndProperty();
}
}
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Why is my propertydrawer being automatically disabled? 1 Answer
Using CustomPropertyDrawer in OnGUI of ScriptableWizard 0 Answers
CustomPropertyDrawer: foldout issues when switching properties (simulating weak references) 0 Answers
How to prevent overlap when using nested arrays in CustomPropertyDrawers? 1 Answer