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
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!
– Tseng