Hey folks, I’m posting this here in the scripting forum since I’m not looking so much for specifics on what the Unity editor will do in particular instances as I am how people are exposing particular editable data structures for easy configuration through the inspector.
So, a scenario: I want to encapsulate various features of a texture animation (similar to this tutorial on the Unify wiki). What I want to do is provide a data structure that holds an array of indices, framerate, loop flag, etc. A GameObject could then have a collection of these animation definitions for different game states (run, jump, explode, etc). The ultimate goal here, however, is to be able to add/modify animations to the GameObject’s collection through the editor’s inspector.
I’ve tried a few different approaches and haven’t found one that I’m particularly fond of, so I thought I’d see if other people had settled on a nice solution for doing something similar. The things I’ve tried:
1.) First I tried creating a public class and marking it Serializable so that the inspector will render an editor for it. The class would look something like
[System.Serializable]
public class AnimStruct {
public string name;
public int[] frames;
public int framerate;
public bool loop;
}
…and the GameObject would have…
public AnimStruct[] animations;
…which would expose this and let me modify the array through the inspector. The problem I found with this method was that certain changes to the AnimStruct class could cause the array field in the GameObject to reset back to empty and loose all previously entered data. This wouldn’t be so much trouble if my system/classes were pretty much solid and I was just using them to build up my game, but until then having erasure as a possibility is annoying. The other issue is that certain types of data (int[ ], for example) are a pain to edit through the inspector - it would be wonderful to have a custom editor for modifying and previewing each animation right there in the inspector.
2.) The issues with the previous method seemed to point to having the AnimStruct extend MonoBehavior and be a full-fledged Component. Once you do this, though, the GameObject’s array no long renders the AnimStruct fields in the inspector (event when keeping the Serializable) - it just creates a drop-target for dropping GameObjects that have an AnimStruct component attached. You could drag multiple AnimStructs onto the GameObject itself though and each would be visible to modify in the inspector. You could write a custom editor and that would be visible for each one, too. Changing the AnimStruct script would just have each of those components update without losing any data (hopefully? I didn’t get quite far enough to test this before running into issues with this method). The problem with this method, though, is it seems like Unity treats Components sort of like facets of a GameObject. There doesn’t seem to be a way to grab references of just one of multiple components in a GameObject. For example, say the GameObject controller wants to hold a reference to one of its attached AnimStruct components as a default animation. Exposing the field and dragging one AnimStruct (of the many now on the GameObject) onto it just puts a reference to the GameObject - not the component! That means dragging AnimStructA is no different that dragging AnimStructB onto the field, and our default animation field is useless.
So, my question is this: If you have complex data that can potentially be collected in an array or list, whats the best way to expose that to the inspector (with bonus points for providing a hook to eventually add a custom editor)? Thanks so much folks.
