Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Woj_Gabel_FertileSky · Feb 08, 2012 at 01:07 PM · editorarrayvariablecustomreset

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges