This is really anoying me, working on some tools to help streamline stuff and its resisting all efforts.
Every time I create a grid of nodes it wipes them out if a script compiles, I hit play or i reload the editor.
EditorUtility.SetDirty(target); Should be saveing the changes but for some reason it is not.
Have also tryed all types of [SerializeField] and [System.Serializable] but nothing changes.
This is the customEditor im trying to get to work.
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
[CustomEditor(typeof(PathFinderTwo))]
public class EditorPathFinder : Editor
{
public override void OnInspectorGUI()
{
PathFinderTwo script = (PathFinderTwo)target;
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Make NodeGrid"))
{
Place_Grid(script);
EditorUtility.SetDirty(target);
EditorUtility.SetDirty(this);
}
EditorGUILayout.EndHorizontal();
// Show default inspector property editor
DrawDefaultInspector();
}
public void Place_Grid(PathFinderTwo script)
{
Debug.Log("******************************");
script.nodeList = new PathFinderTwo.Node[script.gridSizeX / script.gridSpaceing, script.gridSizeZ / script.gridSpaceing]; // Make the array
for (int x = 0; x < script.gridSizeX / script.gridSpaceing; x++)
{
for (int z = 0; z < script.gridSizeZ / script.gridSpaceing; z++)
{
EditorUtility.DisplayProgressBar("Working", "# " + x + " Of " + script.gridSizeX, x / script.gridSizeX);
//Create a new node
PathFinderTwo.Node node = new PathFinderTwo.Node();
//Get the posistion of the new node
Vector3 nodeposistion = new Vector3(x * script.gridSpaceing, 0, z * script.gridSpaceing);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(new Vector3(nodeposistion.x,200, nodeposistion.z), -Vector3.up, out hit))
{
nodeposistion.y = hit.point.y + 0.5F;
}
node.nodePosistion = nodeposistion;
script.nodeList[x, z] = node;
Debug.Log("Node: "+x + "," + z);
}
}
EditorUtility.ClearProgressBar();
script.numberOfNodes = script.nodeList.Length;
EditorUtility.SetDirty(target);
}
}
This is part of the script it changes.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PathFinderTwo : MonoBehaviour {
//public float costForEachNode = 1;
public bool showIcons;
public int gridSizeX;
public int gridSizeZ;
public int gridSpaceing; //Distance of nodes from each other.
public int numberOfNodes;
[System.Serializable]
public struct Node
{
public Vector3 nodePosistion;
// public Node connection; //For parenting and returning path
public int connectionKey;
public bool open; //Is this node passable
}
[SerializeField]
public Node[,] nodeList;
void Start()
{
}
void OnDrawGizmos()
{
if (showIcons == true)
{
foreach (Node node in nodeList)
{
Gizmos.DrawIcon(node.nodePosistion, "NavNode.psd");
}
}
}
}