- Home /
Problem is not reproducible or outdated
Multi-dimensional Array Not Persisting
Ok. I am working with a custom editor script which involves a 2 dimensional array. Basically, the custom editor is used to display the values in the 2D array as a table. That works fine. I also have it set up to be able to edit the values of the array via the table in the custom editor. That seems to work fine, too. The problem is, the changes I make to the array don't seem to have any lasting effect. They hold as long as I continue to work in the editor, but if I exit and restart Unity, or even if I just hit Play from the editor, the changes that I made to the array are lost. In fact, the array itself is resets to null, and the array has to be initialized again.
Anyone have any idea why this is happening? I'd love to post my scripts, but I can't seem to figure out how to attach anything. I tried inserting it as a code sample, but it wasn't reading the formatting correctly...
Ok. Further experimentation reveals it is not just multi-dimensional arrays that fail to retain their values...nothing does. I cut and pasted the code from this link to test. http://unity3d.com/support/documentation/ScriptReference/Editor.html Executing the code samples on that page has the same problem. As long as the editor has focus, the values set on $$anonymous$$yPlayer are persistent, but as soon as it loses focus (Play is pressed, different scene is loaded, etc), the values are lost. Even when I change the values, the editor does not seem to register anything has changed because it never prompts me to save the scene if I attempt to leave it.
I was wondering if v4.0 has provided support for this issue. It certainly does not seem to work for me. But I wonder if there is a "trick" to getting it to work.
Thanks
Answer by syclamoth · Sep 19, 2011 at 04:30 AM
To begin with, Unity does not serialise multi-dimensional arrays. At all. I've had to instead serialise a list of [System.Serializable] objects with arrays inside of them, and then use a special 'getter' to glue them back together when I needed them- a solution which, I'd be the first to admit, was just short of a truly disgusting hack. On the other hand, it worked, which is important.
This is a problem I have had to deal with at least once. My solution to it was to have a GameObject in the scene which contained all the data, and my editor window just referred to that for things I wanted to save between sessions (or, for that matter as Peter G said, longer than about 5 minutes!). The problem as far as I can tell is that Unity does not serialise anything that is not on a gameObject or Asset- which does not include custom editor windows. For what I was doing this was adequate because I wanted to have a GameObject at runtime that I could query for the information I set up in the custom editor, but for editor-only utilities it's kind of a waste to do it this way.
Answer by Peter G · Sep 19, 2011 at 02:39 AM
I've found that Unity creates a new instance of your editor script much more frequently than you would like. How frequently depends on what kind of custom editor you have. With a custom inspector for example, there's a new instance of your editor script every time you leave the object then come back. Obviously a new instance means that all the values are reset to their initial state.
I haven't found a good solution to this yet. I haven't found anyway of telling Unity to use the same instance of your editor script again. It will create a new one every time and that makes it nearly impossible to save values by storing them in your editor script. If you are really determined, I guess you could write the values to some sort of text file then read them back. The only other alternative I've found is to store the values in a script on an object then read values off of that. It's really not pretty or clean, but it will work.
This is late, but anyway. I'm using a static init for inspectors to load all textures once... Once every compilation. But if you are creating some data to visualize, you shouldn't I guess. Inspector should draw only stuff that is on an object. And some nice stuff, like custom buttons.