So, I am writing a custom inspector (c#) and have come into a theory to handle an organizational need for the project. What I can’t figure out is how to handle it in the inspector.
The theory is to create a self-referencing grouping class. Basically, I want to make a list of a custom class. Within this custom class is another list variable of that same class. A plausibly endless nest.
Here is an example snippet (untested of course) to help display what I mean:
//main class that Unity would use
public class Designer : Monobehavior {
public List<Organizer> folder = new List<Organizer>(); //The list that would display in inspector
}
//Organizer class used to create groups and sub-groups to organize data
public class Organizer {
public List<Organizer> file = new List<Organizer>(); //Allow for creation of sub-groups
public string name; //what the user names this group (or sub-group)
public int id; //a unique ID assigned to this group only
public Data someData;
}
//Just for the sake of complete example, throw in a Data class
public class Data {
public List<string> phrases = new List<string>(); //quotes of some kind, say.. from an author
}
Now, in this example the users working with this script should be able to create lists upon lists upon lists. ID’s should be auto-assigned. For the sake of argument, lets assume that ID’s are based only on the current list, and are assigned at time of creation by the custom inspector to be the highest_id+1. eg - if 3 items were created in a list, and the second item was removed, the next item created would still have an id of 3 (because 0, 1, 2 would of been the first 3. removing 1 means 2 is still the highest ID. 2+1 = 3).
Lets pretend the user is a librarian. Here’s how grouping would work:
- Create a Folder (most outer layer). Name it “Fantasy”. No data is assigned to this group, it’s strictly for organizational purposes.
- Create a file (sub-layer of “Fantasy”). Name it “A”. Again, no data is assigned here, it’s only an organizational layer.
- Create a file (sub-layer of “A”). Name it “Richard Adams”. Again, no data is assigned.
- Create a file (sub-layer of “Richard Adams”). Name it “Cinema”. No data assigned.
- Create a file (sub-layer of “Cinema”). Name it “Watership Down”. Data is assigned to this group, and we may not need additional layers.
So, in this example, the Librarian would later be able to see the main group (fantasy) and know that within that group they will find an Alphabetized group of authors that are fantasy based. They would then be able to pick the letter of the authors last name, the author they want, and then decide if they want book quotes from movies that had theatrical releases, or not. Then find the book they want, and see the list of phrases.
Potentially, they could create another group that shows what phrases made it into the movie, or whatever.
So… the problem?
The custom class is designed to edit one group at a time. For example, they could select the group “Fantasy” and be provided with options like adding a sub-group, selecting a subgroup to edit, adding data to this group, or more etc. This is done by displaying a custom GUI using the various variables of the selected group.
What I need to do is find a way to open groups, and possibly “go up one level”. If all the data displayed is based on the currently available group, how do I access the nth group in the nest and how can I determine what the level above it is?
Example snippet (again untested, and is just the example of the display code) of what the display of a group might look like:
viewFolder = EditorGUILayout.Popup(viewFolder, script.folder.ToArray());//creates a dropdown list of folders (assume viewFolder is an int declared prior to the OnInspectorGUI() function. Also assume that 'script' has been predeclared as the script the custom inspector is editing)
GUILayout.BeginVertical(EditorStyles.textField); //create a visible boxed work space that will function as our group editor area
//This is where the group variables are going to be displayed/edited
GUILayout.BeginHorizontal();
if (GUILayout.Button("Add Subgroup", EditorStyles.miniButton, GUILayout.Width(100))) {
script.folder[viewFolder].file.Add(); //make a subgroup to this group. Assume there is a similar button to make exterior groups prior to this area in the editor
}
GUILayout.EndHorizontal();
GUILayout.BeginVertical();
for (int i=0;i<script.folder[viewFolder].files.Count;i++) {
//list the subgroups and allow the user to select one
//this would be horizontal layouts and such
}
GUILayout.EndVertical();
GUILayout.EndVertical(); //End the group editor area
In this example, I didn’t fill in the for loop, just commented what kind of things would go there. I also didn’t include the ability to edit other group variables. I’m not overly concerned with those specifics.
What I’m trying to theorize is a feasible manner to allow this kind of functionality to work. viewFolder wouldn’t work as a dropdown list because the group editor box should be populated with data from whatever the currently selected group is. That could be the nth subgroup of the folder list.
I hope I properly explained my question. I’m not at all certain how to proceed. I have no specific code beyond the examples listed because I haven’t written this yet. Trying to wrap my head around how exactly to do it.
Thanks in advance for your help!