- Home /
How to customize editor to show, in an array of scriptA within scriptB, the public vars of each scriptA[n] element?
Hey y'all,
I was wondering if it was possible to customize the editor in such a way that, given an array of script (a) in script (b), I could edit each iteration of script (a)'s public variables.
To exemplify, let's say we have these two scripts:
//testScript.js
var stringVar : String;
var intVar : Int;
function Start(){
//...
}
function Update(){
//...
}
//TestScriptEditor.js
var intArray : int[]; var scriptArray : TestScript[];
function Start () {
//...
}
Normally, I'd get something like this out of that:
I was curious of how to get it to behave like this (masterful MS Paint skills):
I took a bit of time last week to learn a bit about custom editor GUIs, but don't know quite enough to handle this. I do feel like it's possible (and probably common). Of course, using straight up "TestScript[]" is most likely bad form since the editor is expecting there to be an object with the script I think, but maybe I'm wrong.
Any thoughts? I'd love to hear from the community!
Answer by Jamora · Sep 28, 2013 at 10:24 PM
You will get this functionality if you have your class derive from System.Object. In UnityScript:
//TestScript.js
#pragma strict
@SerializeField
public class TestScript {
/*implementation*/
}
The reason you don't get this with scripts derived from MonoBehaviours is that they are already attached to a GameObject (somewhere, unless the developer is breaking the rules), and it would be redundant (possibly even dangerous) to be able to modify them in two places.
That's the ticket! I forgot about $$anonymous$$onobehavior.
What's the purpose of the serialization in this situation, out of curiosity?
If you leave it out, Unity won't serialize the classes when switching from edit to play mode, so Unity will just treat them as an object.
With SerializeField, Unity knows it can serialize at least something inside the class, so it'll take a closer look what it can do. As a result, it'll show you all the types it can serialize in the inspector.
Good to know! If I wanted something like enum {one, two three} to have a dropdown selector per array item, would I need to do some script-fu with GUIEditor?
Your answer
Follow this Question
Related Questions
How to get EditorWindow client rect? 1 Answer
How can I preserve static object/data between editor and play? 2 Answers
Resizing an array of gameObjects 2 Answers
Custom Inspector Color Spacer 1 Answer
Better Unity Event UI 0 Answers