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:
@CustomEditor(Graph)
class GraphEditor extends Editor
{
function OnInspectorGUI()
{
GUILayout.Label("Graph Editor");
target.length = EditorGUILayout.IntField ("Length", target.length );
target.width = EditorGUILayout.IntField ("Width", target.width );
target.offset = EditorGUILayout.IntField ("Offset", target.offset );
if( GUILayout.Button( "Build Graph" ) )
{
target.Generate();
}
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel ("graphNodes size: ");
if( target.graphNodes )
{
EditorGUILayout.IntField ( target.graphNodes.length );
}
EditorGUILayout.EndHorizontal();
if (GUI.changed)
{
EditorUtility.SetDirty (target);
}
}
}
below is an example of what happens when Generate() is called:
var length = 10;
var width = 10;
var offset = 0;
var graphNodes : Array;
function Generate()
{
//Clean up
if( graphNodes )
{
graphNodes.Clear();
}
//Create a list of nodes
graphNodes = new GraphNode[ length * width ];
//Instantiate GraphNode and fill list
var prefab : GraphNode = Resources.Load( "GraphNode", GraphNode );
for( var i = 0; i < graphNodes.length; ++i )
{
graphNodes[ i ] = Instantiate( prefab, Vector3.zero, Quaternion.identity );
}
//Build graph( not needed for the example )
}
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.