- Home /
Custom editor window resets array
Dear community!
tl:dr - This here code resets my vars hwen changing Selection.activeGameObject to something else, aaaarrr!
I wanted to do a custom window to fiddle with my arrays in editor. After 3 days of struggling , i gave up and started simple. This is the code i gave birth to:
first file, with editor code:
// Display things and do things while MapEditor in scene is selected:
var arrayOfHoles = new Array();
var groupEnabled = false;
var targetObject : GameObject;
class MapGeneratorEditorDisplay extends EditorWindow
{
var selection : boolean;
var cloneTimesX : int = 1;
var prevVar : int;
@MenuItem ("Window/MapGeneratorInfo")
static function ShowWindow ()
{
EditorWindow.GetWindow (MapGeneratorEditorDisplay);
}
function OnGUI ()
{
GUILayout.Label ("Base Settings");
var obj = Selection.activeGameObject;
if(obj)
{
if(obj.transform.name.ToString() == "MapGenerator")
{
var objScript : randomCubeGen = obj.transform.GetComponent("randomCubeGen");
if(objScript)
{
cloneTimesX = objScript.nrOfHoles;
EditorGUILayout.LabelField("Object: ",obj.transform.name.ToString());
EditorGUILayout.LabelField(" ","");
cloneTimesX = EditorGUI.IntSlider(Rect(5,40,position.width, 20), cloneTimesX, 0, 10);
objScript.nrOfHoles = cloneTimesX;
if(prevVar!=cloneTimesX)
{
objScript.test();
prevVar = cloneTimesX;
}
for(i=0; i< cloneTimesX; i++)
{
arrayOfHoles[i] = 1;
var r : Rect = EditorGUILayout.BeginVertical();
var rx : Rect = EditorGUILayout.BeginHorizontal ("Button");
EditorGUILayout.LabelField("hole: ", i.ToString());
var posX : int = objScript.holesXs[i];
var posY : int = objScript.holesYs[i];
posXType = EditorGUILayout.IntField("X:",posX);
posYType = EditorGUILayout.IntField("Y:",posY);
objScript.holesXs[i] = posXType;
objScript.holesYs[i] = posYType;
EditorGUILayout.EndHorizontal ();
EditorGUILayout.EndVertical();
}
}
}
}
else
{
selection = true;
}
this.Repaint();
}
}
Second file : the file with the variables:
var nrOfHoles : int ;
var holesXs :int[];
var holesYs :int[];
function test()
{
if(!holesXs)
{
print("made new array");
holesXs = new int[nrOfHoles];
holesYs = new int[nrOfHoles];
}
else if(holesXs)
{
var tempXs = new Array (holesXs); // copy to temp array
var tempYs = new Array (holesYs);
holesXs = new int[nrOfHoles]; // clean new array +1 to length
holesYs = new int[nrOfHoles];
//print(tempXs.length +" "+holesXs.length);
if(tempXs.length<holesXs.length)
{
for(i=0; i<tempXs.length ; i++)
{
holesXs[i] = tempXs[i];
holesYs[i] = tempYs[i];
}
}
else if(tempXs.length>holesXs.length)
{
for(i=0; i<holesXs.length ; i++)
{
holesXs[i] = tempXs[i];
holesYs[i] = tempYs[i];
}
}
}
}
First file goes wherever. Second file goes to Editor folder. Keep file name: "MapGeneratorEditorDisplay" in order to prevent errors.
Now the idea was to get a variable(multidim array) and display it in custom window to add and subtract at ease. That didnt happen, because i couldn't find any info on arrays. Docs are, well, in this case a white hole of nothing), and after a good amount of frustration i gave up and tried this approach.
All is great, but when i deselect my object with the second script, after selecting anotherone the vars are reset. But when i deselect one object and select it , the vars stay the same.
I just dont know where is the reset vars problem in here. Anyone wants to give this a try?
sorry for the formating, but pasting from monodevelop is not as simple as one would think.
Answer by Woj_Gabel_FertileSky · Feb 09, 2012 at 08:38 PM
Ok, i have figured it out. I just have to turn off this:
if(prevVar!=cloneTimesX)
{
objScript.test();
prevVar = cloneTimesX;
}
when selecting something. So just add this aditional if (second one) to first file:
...
if(objScript )
{
cloneTimesX = objScript.nrOfHoles;
if(selection == false)
{
prevVar = cloneTimesX;
selection = false;
}
...
and we are golden.
But still, its a nightmare of a code :)
Your answer
Follow this Question
Related Questions
[Simple yet problematic] Array looses all the variables when I hit "Play" 2 Answers
Array of classes on SerializedProperty 2 Answers
Working with Arrays for Custom Inspector 0 Answers
How can I make variables reset when leaving play mode? 3 Answers
How could I reset a modified serializedProperty in PlayMode runtime? 0 Answers