Search Unity

Storing arrays through extended editors

Discussion in 'Scripting' started by Valhadon, Nov 24, 2010.

  1. Valhadon

    Valhadon

    Joined:
    Nov 21, 2009
    Posts:
    6
    I am trying to build a pathfinding system for the game I am working on. I would like to have the graph built prior to the play button being pressed by using a custom editor.The basic idea is that the user chooses the graphs length, width and offset and presses a button to generate the graph.

    below is a code example of my custom editor:
    Code (csharp):
    1.  
    2. @CustomEditor(Graph)
    3.  
    4. class GraphEditor extends Editor
    5. {
    6.     function OnInspectorGUI()
    7.     {
    8.         GUILayout.Label("Graph Editor");
    9.        
    10.         target.length = EditorGUILayout.IntField ("Length", target.length );
    11.         target.width = EditorGUILayout.IntField ("Width", target.width );
    12.         target.offset = EditorGUILayout.IntField ("Offset", target.offset );
    13.        
    14.         if( GUILayout.Button( "Build Graph" ) )
    15.         {
    16.             target.Generate();
    17.         }
    18.        
    19.         EditorGUILayout.BeginHorizontal();
    20.         EditorGUILayout.PrefixLabel ("graphNodes size: ");
    21.         if( target.graphNodes )
    22.         {
    23.             EditorGUILayout.IntField ( target.graphNodes.length );
    24.         }
    25.         EditorGUILayout.EndHorizontal();
    26.        
    27.         if (GUI.changed)
    28.         {
    29.                    EditorUtility.SetDirty (target);
    30.         }
    31.     }
    32. }
    33.  
    below is an example of what happens when Generate() is called:
    Code (csharp):
    1.  
    2. var length = 10;
    3. var width = 10;
    4. var offset = 0;
    5. var graphNodes : Array;
    6.  
    7. function Generate()
    8. {
    9.     //Clean up
    10.     if( graphNodes )
    11.     {
    12.        graphNodes.Clear();
    13.     }
    14.    
    15.     //Create a list of nodes
    16.     graphNodes = new GraphNode[ length * width ];
    17.    
    18.     //Instantiate GraphNode and fill list
    19.     var prefab : GraphNode = Resources.Load( "GraphNode", GraphNode );
    20.     for( var i = 0; i < graphNodes.length; ++i )
    21.     {
    22.         graphNodes[ i ] = Instantiate( prefab, Vector3.zero, Quaternion.identity );
    23.     }
    24.    
    25.     //Build graph( not needed for the example )
    26. }
    27.  
    28.  
    Everything works perfectly, the graph builds and shows all its connections and has a length of ( length * width ) until I press the play button then the arrays length becomes 0. It seems that length, width and offset get set to the taget.script but the array gets cleared somehow.

    Does the custom editor have problems with dynamic arrays or is there something I could do differently to fix this?
    I have looked around a lot and haven't been able to find a solution. Any help would be greatly appreciated.

    Thank you.
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Does your monobehaviour code potentially instantiate an array?

    I know that this functionality works fine, even with more complex stuff
     
  3. Valhadon

    Valhadon

    Joined:
    Nov 21, 2009
    Posts:
    6
    The second example is my monobehaviour script, when the Generate button is clicked in my custom editor, it calls the generate function of my monobehaviour script( target.Generate(); ) which then creates a new array( graphNodes = new GraphNode[ length * width ]; ). But when the play button is pressed this array is somehow cleared, while the other data members of this script are not.
     
  4. Valhadon

    Valhadon

    Joined:
    Nov 21, 2009
    Posts:
    6
    Can someone please help me with this!
     
  5. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    Arrays changed in edit mode aren't persistent. Dunno why.

    I had to use

    @HideInInspector
    var variable : Type[];

    to get the functionality I needed.
     
  6. Valhadon

    Valhadon

    Joined:
    Nov 21, 2009
    Posts:
    6
    Is there any other way to get it to work?
     
  7. rom

    rom

    Joined:
    Jul 2, 2006
    Posts:
    265
    Like ArrayList, I don't think an Array is a persistent type, use GraphNode[].
    Code (csharp):
    1.  
    2. var length = 10;
    3. var width = 10;
    4. var offset = 0;
    5. var graphNodes : GraphNode[];
    6.  
    perhaps make your GraphNode class a [System.Serializable] System.Object.
    or a ScriptableObject, stored as an Assetdatabase
     
    Last edited: Dec 2, 2010