- Home /
Creating a custom inspector utilizing a list of class instances with serialization?
I'm sorry, but I'm having trouble finding help for this. I am trying to create a line path editor with a combination of instance lists of 2 separate classes: a class defining what a point in space is, and a path class that contains a list of said points, in turn creating a list of path instances, as follows:
NOTE: These a both included in one Monobehavior class & are edited for simplicity.
Point Class:
[System.Serializable]
public class WaypointClass
{
[SerializeField]
private Vector3 _position = Vector3.zero;
public Vector3 Position
{
get { return this._position; }
set { this._position = value; }
}
}
And a Path class:
[System.Serializable]
public class PathClass
{
[SerializeField]
private string _name;
public string Name
{
get { return this._name; }
set { this._name = value; }
}
[SerializeField]
private List<WaypointClass> _waypoints = new List<WaypointClass>();
public List<WaypointClass> Waypoints
{
get { return this._waypoints; }
set { this._waypoints = value; }
}
public WaypointClass AddWaypoint ()
{
WaypointClass point = new WaypointClass();
Waypoints.Add (point);
// ... does some more needed work here.
return point;
}
public void RemoveWaypoint (WaypointClass point)
{
Waypoints.Remove (point);
// ... does some more needed work here.
}
}
[SerializeField]
private List<PathClass> _paths = new List<PathClass>();
public List<PathClass> Paths
{
get { return this._paths; }
set { this._paths = value; }
}
On the inspector side I know how setup a basic inspector using both the target method and the serializedObject method & would like to use the serializedObject method to keep them separate from prefabs Here is what I have so far:
I have serialization working for other, not mentioned areas not related to the paths themselves, but, am using targets as I don't know how to get this working with lists of class instances & their individual pieces of data.
Most specifically, how can I use serialization & iterate down the instances of waypoints to access things like the Vector2 points (others including the stop time & spawn control enums)? I can do this with the target method, but I need this to be prefab independent.
I feel like I'm missing something, could you clarify some things?
First You want to be able to go down a list of class instances and edit the properties of each instance within the inspector. Is that right?
Second Is the object with the moving platform script part of a prefab?
Will do. If I may, I have a list of lists. There is a list of instances of the path class (to make up a collection of paths), and each of those has a list of instances of the waypoint class (to define the where the path travels to). This is contained in an object/monobehaviour called the Path Designer. The inspector only shows one path (from the path list) at a time, but it allows the user to edit general settings about the path, as well as settings about each waypoint in the that path. (There is also scene editing functionality to make it easier, but I'm just focusing on the inspector for now.)
But what is pictured is a different object/monohaviour, the $$anonymous$$oving Platform. It moves about a chosen path, not only can from the list of paths from the designer, but also from a singular instance of the path class that it has itself (in case the user does not want to involve the designer). Regardless, the inspector scripts for both behave much the same way, but I have been using the target method for that up until now.
Whats important is just that I can get the individual points (Vector2's) out a list of the waypointclass instances as serialized properties, but I am not sure how to go about accessing data out of an instance of a class, let alone a list of them all in a row like that.
I am sorry this seems a bit much, but I'm keeping it as simple as I can.
Answer by Tourist · Oct 26, 2017 at 07:18 AM
I may not have understood correctly as there is no script attached regarding the editor side.
You have 2 main methods : SerializedObject.FindProperty that will give you a SerializedProperty object representing the variable you want to access in the class associated with the inspector (here MovingPlatform).
SerializedProperty.FindPropertyRelative that acts like the one before but the path given is relative to the object in the serialized property.
Assuming the field _paths at the end of the scripts attached is in MovingPlatform :
SerializedProperty pathListProperty = serializedObject.FindProperty("_paths");
SerializedProperty firstPathPorperty = pathListProperty.GetArrayElementAtIndex(0);
SerializedProperty firstPathNameProperty = firstPathProperty.FindPropertyRelative("_name");
SerializedProperty firstPathWaypointListProperty = firstPathProperty.FindPropertyRelative("_waypoints");
SerializedProperty firstPathFirstWaypointProperty = firstPathWaypointListProperty.GetArrayElementAtIndex(0);
SerializedProperty firstPathFirstWaypointPositionProperty = firstPathFirstWaypointProperty.FindPropertyRelative("_position");
Alrighty. I'm sorry for the late response. I had some issues keeping me at work. I have tested this out as quickly as I can, and have seen positive results thus far. After acquiring & modifying the data from the current waypoint instance in the loop, I call serializedObject.Apply$$anonymous$$odifiedProperties to assign the new values to that waypoint, allowing me to reuse the declared serialized properties. Would you see a downside to calling that method multiple times in a frame?
Your answer
Follow this Question
Related Questions
Custom classes, inheritance and the inpector 0 Answers
How to access one class instance in editor script? 1 Answer
How to change inspector with non-Monobehaviour objects ? 1 Answer
Replicate "Apply" and "Revert" button functionality in ScriptableObject Editor Inspector 2 Answers
Custom ordering of variables in inspector when using base and derived classes 1 Answer