- Home /
Dynamically update the editor?
Hi,
I've been trying to figure this out for hours, been searching and testing to no avail.
What I want to do is have a script with variables which can be edited in the inspector, which affect the instantiation of prefabs at runtime in the Awake() or Start() function, but which runs and updates in the editor, changing as the variables are changed. Here is a very basic example script:
Code:
@script ExecuteInEditMode;
var cubePrefab : GameObject;
var lineLength : float;
var numberOfCubes : int;
function Start() {
var cubeStep : float = lineLength/numberOfCubes;
for(var i = 0; i < numberOfCubes; i++) {
var newCube : GameObject;
newCube = Instantiate (cubePrefab, Vector3(i * cubeStep,0,0), Quaternion.identity);
}
}
So it instantiates a number of cubes equally along a given line length, very simple. With the ExecuteInEditMode, the script runs once, but then if you change the length of the line, or the number of cubes you want, it doesn't change. Is there a way to make this work? I realise it may have something to do with editor scripts, so I've been trying to get my head around them. I tried this:
Code:
@CustomEditor (CubeLine)
class CubeLineEditor extends Editor {
function OnInspectorGUI () {
if (GUI.changed)
target.Awake();
}
}
But for one thing, it blocks out everything else in the inspector so you can no longer see the variables coming from the CubeLine.js script, and also when I did get something like this working, after you change the values, it just instantiates another set of cubes aswell as the ones already there. What I want is that, say you set the line length to 10, and number of cubes to 10, you'll see in the editor a line of ten cubes each spaced one point apart. But then if you change the length to 20 in the inspector, you'll see the line update to be 20 long, with a cube every 2 points apart etc. etc. What is the approach to such a thing?
Thank you so much, and hope this is clear. S
Answer by BerggreenDK · Oct 03, 2011 at 02:38 PM
If you need access to variables through the inspector, then add the word "public" infront of var.
Then they will be accessable through the inspector.
Answer by kinkersnick · Oct 03, 2011 at 03:44 PM
I can access the variables fine under normal circumstances, but they disappear when I use an editor script - do you mean I need to specify the variables in the normal script AND in the editor script as well? That seems rather strange... But the main question I'm asking is how to make the editor update things dynamically such as number of instantiated prefabs etc. Basically how to rerun an Awake() function when the inspector is changed: not instantiate stuff on top of what is there, but refresh it anew.
Don't reply as answer, use comments! This is no forum, its Q & A and the answers are sorted by the vote ups/accepted status!
Answer by Tseng · Oct 03, 2011 at 06:31 PM
Well, first off you need to move this code into its own method, because you have to call it on update.
function InstantiateCubes() {
for(var i = 0; i < numberOfCubes; i++) {
var newCube : GameObject;
newCube = Instantiate (cubePrefab, Vector3(i * cubeStep,0,0), Quaternion.identity);
}
}
}
And then call it every time, when a the input is changed, similar to the one you already pointed out
if(GUI.changed)
EditorUtility.SetDirty(target);
You shouldn't call Awake or Start methods directly from code. Of course, don't forget to remove the previous cubes before adding new ones. This will flag your target (the object you're editing) as dirty, which will notify unity that the Script (MonoBehaviour or ScriptableObject) has been updated.
Your answer
Follow this Question
Related Questions
Fastest way to instantiate and move multiple game objects in the editor 3 Answers
Unity Editor crashing when editing scripts referenced from GameObjects 1 Answer
Fastest way to instantiate thousands of objects at runtime? 1 Answer
Unity not recognizing Instantiate? JavaScript 1 Answer
The Script Equivilent of dragging a hierarchy of meshes over a preexisting prefab? 0 Answers