So to begin with, I’m writing an editor script that serves the following purpose: It will be able to access any MonoBehaviour (user defined) and access any list on that MonoBehaviour (again user defined), and will then display this list in a user friendly manner.
The script looks a little bit like this:
public class ListOrganizer : EditorWindow
{
System.Type targetType;//This is the type that we are using in our list
public System.Collections.Generic.List<Resource> list =
new System.Collections.Generic.List<Resource>();
public Resource classType = new Resource();
FieldInfo[] fieldInfos;
public ShipController sourceScript;//This is the script with the list we will be editing
public string listName = "Target List Name";//This is the name of the list variable that we want to edit
bool editing = false;
[MenuItem("Window/ListOrganizer")]
public static void Init()
{
ListOrganizer listOrganizer = (ListOrganizer)EditorWindow.GetWindow(typeof(ListOrganizer));
}
void OnGUI()
{
if(!editing)
{
sourceScript = EditorGUILayout.ObjectField(sourceScript, typeof(ShipController), true) as ShipController;
listName = EditorGUILayout.TextField(listName);
if(GUILayout.Button("Start Editing"))
{
Debug.Log(listName);
if(sourceScript != null)
{
list = (System.Collections.Generic.List<Resource>)sourceScript.GetType().GetField(listName).GetValue(sourceScript);
editing = true;
}
}
}
if(editing)
{
...//Display all the editing information
}
}
Apart from one major error, it’s working really well. However, currently this code only works for displaying List*Resource*
types and only from the MonoBehaviour ShipController. What changes do I have to make so that I can grab List*ANYTYPE*
from MonoBehaviour [ANYSCRIPT]?
Everything I’ve been able to find points me to something like what is described here. How do i convert a String into a Class reference in C# - Unity Answers
So what I would want is something like System.Type targetType = typeAsAString.GetType().
But then how do I get the type based an a string, then utilize that type throughout the rest of this script?
NOTE: Not sure why but I can’t use “<” and “>”. Edited to clear up the confusion.