Hello, I want to fill up an List with values from my Editor Window. I know I have to use Serialization somehow, but I can’t figure it out. After I filled the list up, I need to use it in a Monobehaviour gameobject script, but the list is empty. Also, how can I access that list when I do actually fill it up correctly in my gameobject script? Should I just make a static list so any script could access it?
Sorry, I can’t make out what you’re asking. Can you explain in more detail what you’re trying to do? What is MyClass? What class is this list on, and how is it declared? Where exactly are you trying to use it?
Sorry for describing it so abstractly. This is what I’m trying to do.
I have a script, that is attached to a game object.
public class GameObjectScript : MonoBehaviour {
public static List<Information> informationList = new List<Information>();
void Start () {
}
// Update is called once per frame
void Update () {
// Do something with informationList
}
}
Here is my Information class, which is used to set up some variables.
public class Information {
public int variable1;
public int variable2;
}
And finally, here is my custom Unity editor tool.
public class CustomWindowTool : EditorWindow {
...
void OnGUI() {
GameObjectScript.informationList.add (new Information ());
GameObjectScript.informationList[0].variable1 = EditorGUILayout.IntField ("Var1: ", GameObjectScript.informationList[0].variable1);
GameObjectScript.informationList[0].variable2 = EditorGUILayout.IntField ("Var2: ", GameObjectScript.informationList[0].variable2);
}
}
What I’m trying to do here is to setup some variables before my game starts and then use those variables to do something in my GameObjectScript, but the list is always empty when I start the game.
Ah, I see.
The way this is normally done is not to use a custom editor, but instead to make the list an ordinary (non-static) public property, whose type is marked with System.Serializable. Then you can throw an instance of that script onto any object in the scene, and edit it directly with the standard Unity inspector. All the serialization and so forth is handled for you.
Then of course you will need a way to locate that object from other scripts. This is often done with a static property of that script type, which you set in Awake. For added safety, you can make this property private, and expose a public accessor.
To actually edit and save static data, if there’s some reason you have to do it that way… well, I’m not sure. I suspect you’d have to write out an AssetBundle, but I’ve never tried it.
Hello, @clad
I’d suggest you to use EditorUtility.SetDirty();
You can find out more here: Unity - Scripting API: EditorUtility.SetDirty
TL; DR:
BaseScript myBaseScript = (BaseScript)target;
if (GUI.changed)
EditorUtility.SetDirty(myBaseScript);
This will let you save all the value changes in an editor extension!
If you are having difficult time with creating editor extensions, feel free to check out CiON:
Thank you for the suggestion, but I have one problem. My editor class extends “EditorWindow” and not “Editor”, that’s why I can’t use the “target” variable. Is there any workaround?
I see, when I was coding my asset CiON I used this:
http://docs.unity3d.com/ScriptReference/SerializeField.html
TL;DR:
[SerializeField]
public int ThisWillBeSaved;