I’m trying to make a simple track editor that uses a Bezier Path to generate a 3d mesh for a friend’s racing game. The track data is an array of custom TrackDataPoint objects which store the position, tangent and other properties of each point on the track. Here is the TrackData and TrackDataPoint class code (I removed update functions of TrackData and all the “using” stuff to keep it compact here):
[System.Serializable] public class TrackDataPoint : MonoBehaviour { Vector3 position = Vector3.zero; Vector3 tangentDir = Vector3.right; float tangentWeightIn = 1.0f; float tangentWeightOut = 1.0f; } [System.Serializable] public class TrackData : MonoBehaviour { public TrackDataPoint[] points; }
In my editor class, I am able to get a custom editor displaying, get a the list of points and get each point into a SerializedProperty object, however, I cannot figure out how to get the data into the list of TrackDataPoints stored in TrackData (points). I can add a new point using the small editor interface I made, but it is not initialized, so just appears as an empty space when I try to display it.
[CustomEditor(typeof(TrackData))] public class TrackDataEditor : Editor { private SerializedObject trackData; private SerializedProperty points; void OnEnable () { trackData = new SerializedObject(target); points = trackData.FindProperty("points"); } public override void OnInspectorGUI () { trackData.Update(); //draw GUI for each point for (int i = 0; i < points.arraySize; ++i) { EditorGUILayout.BeginHorizontal(); GUILayout.Label(i + ":", GUILayout.MaxWidth(20.0f)); SerializedProperty dataPoint = points.GetArrayElementAtIndex(i); EditorGUILayout.PropertyField(dataPoint, true); EditorGUILayout.EndHorizontal(); } EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add point")) { points.array Size++; // NOW WHAT!? :) } EditorGUILayout.EndHorizontal(); trackData.ApplyModifiedProperties(); } }
How can I create a new TrackDataPoint and assign it to the empty array element from my editor script? If points happens to be a list of standard types, such as Vector3s, this happens automatically. As it is, I get new elements added to the array, but they are empty and display as a drop box for prefabs. If I remove the : MonoBehaviour from TrackDataPoint nothing shows up. Ideally I’d like to display a custom editor per point by having another editor for TrackDataPoint. Is this possible?