- Home /
Question by
m52nd-Architect · Mar 23, 2018 at 07:51 AM ·
editorinspectorarrayscustom editor
Why is my unity editor script not saving changes to the actual script?
I have written an editor script to modify a "Structure" script and for some reason when i change values it resets them to the default state when I hit play. have tried checking for overwriting of values and arrays and cannot find anything to give me a hint as to why. I have isolated he bug, however, to these two C# files and their interactions between each other.
EDITOR SCRIPT
[CustomEditor(typeof(Structure))]
public class StructureEditor : Editor {
private int constructionResourcesNeeded;
private bool showConstructionResources, showGhostNodes, showReferences = true, showTypeSpecificInfo, showSpecificReferences, showFloorVariations;
private string[] resourceCostOptions = new string[] {
"[0] Metal", "[1] Plastic", "[2] Food", "[3] Medicine" , "[4] Volume", "[5] Heat", "[6] Fuel", "[7] Water"
};
public override void OnInspectorGUI() {
Structure structure = (Structure)target;
showReferences = EditorGUILayout.Foldout(showReferences, "References");
if (showReferences) {
structure._controller = (GameController)EditorGUILayout.ObjectField("Game Controller", structure._controller, typeof(GameController), true);
structure._cameraRay = (CameraRay)EditorGUILayout.ObjectField("Camera Ray", structure._cameraRay, typeof(CameraRay), true);
structure.boundingBox = (Collider)EditorGUILayout.ObjectField("Bounding Box", structure.boundingBox, typeof(Collider), true);
EditorGUILayout.Space();
if (GUILayout.Button("Initialize Component")) {
structure.Initialize();
structure.approvedConnectionTypes[0] = true;
}
}
EditorGUILayout.Space();
structure.moduleName = EditorGUILayout.TextField("Module Name", structure.moduleName);
structure.buidLevelIndex = EditorGUILayout.IntSlider("Floor Level", structure.buidLevelIndex, 1, 3);
structure.UpdateFloorVariations();
EditorGUILayout.Space();
string[] options = Exodus.Data.StructureTypes;
structure.structureType = EditorGUILayout.Popup("Type", structure.structureType, options);
EditorGUILayout.Space();
showFloorVariations = EditorGUILayout.Foldout(showFloorVariations, "Exterior Model Variations");
if (showFloorVariations) {
structure.FloorVariations[0] = (GameObject)EditorGUILayout.ObjectField("1st Floor Ext", structure.FloorVariations[0], typeof(GameObject), true);
structure.FloorVariations[1] = (GameObject)EditorGUILayout.ObjectField("2nd Floor Ext", structure.FloorVariations[1], typeof(GameObject), true);
structure.FloorVariations[2] = (GameObject)EditorGUILayout.ObjectField("3rd Floor Ext", structure.FloorVariations[2], typeof(GameObject), true);
}
EditorGUILayout.Space();
EditorGUILayout.LabelField("Connection Types (" + structure.approvedConnectionTypes.Length + ")");
EditorGUI.indentLevel++;
//make sure the structure list of conenction matches the length of the actual connection types
//if (structure.approvedConnectionTypes.Length != Data.StructureTypes.Length) {
// structure.approvedConnectionTypes = new bool[Data.StructureTypes.Length];
//}
for (int i = 0; i < Data.StructureTypes.Length; i++) {
EditorGUILayout.BeginHorizontal();
structure.approvedConnectionTypes[i] = EditorGUILayout.Toggle(structure.approvedConnectionTypes[i]);
EditorGUILayout.LabelField("[" + i + "] " + Format.IntToStructureType(i));
EditorGUILayout.EndHorizontal();
}
EditorGUI.indentLevel--;
showConstructionResources = EditorGUILayout.Foldout(showConstructionResources, "Construction Resources");
if (showConstructionResources) {
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Add")) {
addConstructionResource(structure);
}
if (GUILayout.Button("Clear")) {
structure.constructionCost.Clear();
structure.constructionResource.Clear();
}
EditorGUILayout.EndHorizontal();
for (int i = 0; i < structure.constructionResource.Count; i++) {
EditorGUILayout.BeginHorizontal();
structure.constructionResource[i] = EditorGUILayout.Popup(structure.constructionResource[i], resourceCostOptions);
structure.constructionCost[i] = EditorGUILayout.IntField("Cost", structure.constructionCost[i]);
if (GUILayout.Button("X")) {
structure.constructionCost.RemoveAt(i);
structure.constructionResource.RemoveAt(i);
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
}
}
EditorGUILayout.Space();
#region Ghost Nodes
//showGhostNodes = EditorGUILayout.Foldout(showGhostNodes, "Ghost Nodes
//showGhostNodes = false;
//if (showGhostNodes) {
// EditorGUILayout.BeginHorizontal();
// if (GUILayout.Button("Add")) {
// structure.ghostNodes.Add(new GameObject());
// GameObject tempGhostNode = structure.ghostNodes[structure.ghostNodes.Count - 1];
// tempGhostNode.transform.parent = structure.transform;
// tempGhostNode.name = "Ghost Node " + structure.ghostNodes.IndexOf(tempGhostNode);
// tempGhostNode.transform.SetPositionAndRotation(structure.transform.position, structure.transform.rotation);
// tempGhostNode.AddComponent<NodeVisualizer>();
// }
// if (GUILayout.Button("Clear")) {
// foreach (GameObject g in structure.ghostNodes) {
// DestroyImmediate(g);
// }
// structure.ghostNodes.Clear();
// }
// EditorGUILayout.EndHorizontal();
// for (int i = 0; i < structure.ghostNodes.Count; i++) {
// EditorGUILayout.BeginHorizontal();
// structure.ghostNodes[i] = (GameObject)EditorGUILayout.ObjectField("Ghost Node [" + i + "]", structure.ghostNodes[i], typeof(GameObject), true);
// if (GUILayout.Button("X")) {
// DestroyImmediate(structure.ghostNodes[i]);
// structure.ghostNodes.RemoveAt(i);
// }
// EditorGUILayout.EndHorizontal();
// EditorGUILayout.Space();
// }
//}
//EditorGUILayout.Space();
#endregion
showTypeSpecificInfo = EditorGUILayout.Foldout(showTypeSpecificInfo, "Type Specific Information (" + Format.IntToStructureType(structure.structureType) + ")");
if (showTypeSpecificInfo) {
EditorGUI.indentLevel++;
showSpecificReferences = EditorGUILayout.Foldout(showSpecificReferences, "Specific References");
if (showSpecificReferences) {
EditorGUI.indentLevel++;
#region SpecificReferences
//switch (structure.structureType) {
// case 0:
// Debug.LogWarning("No functionality suported with an structureType: Undefined!");
// break;
// case 1: // "WallSegment"
// structure.snapNode = (Transform)EditorGUILayout.ObjectField("Parent Snap Node", structure.snapNode, typeof(Transform), true);
// break;
// case 2:
// // temp = "BoxHab";
// structure.snapNode = (Transform)EditorGUILayout.ObjectField("Parent Snap Node", structure.snapNode, typeof(Transform), true);
// break;
// case 3:
// //temp = "Connector";
// structure.snapNode = (Transform)EditorGUILayout.ObjectField("Parent Snap Node", structure.snapNode, typeof(Transform), true);
// break;
// case 4:
// // temp = "ConnectorModule";
// structure.snapNode = (Transform)EditorGUILayout.ObjectField("Parent Snap Node", structure.snapNode, typeof(Transform), true);
// break;
// default:
// //temp = "Undefined";
// break;
//}
#endregion
structure.snapNode = (Transform)EditorGUILayout.ObjectField("Parent Snap Node", structure.snapNode, typeof(Transform), true);
EditorGUI.indentLevel--;
}
}
EditorGUI.indentLevel--;
}
void addConstructionResource(Structure _structure) {
_structure.constructionResource.Add(0);
_structure.constructionCost.Add(0);
}
}
STRUCTURE SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Structure : MonoBehaviour {
[Header("References")]
public GameController _controller;
public CameraRay _cameraRay;
public string moduleName;
public Transform snapNode;
private Structure parentStructure;
private Structure[] connections;
public Collider boundingBox;
public GameObject[] FloorVariations = new GameObject[3];
[Header("Data")]
public int structureType = -1, buidLevelIndex = 0;
public bool hasConnection, isConnected, underConstruction, needsRepair;
[SerializeField] public bool[] approvedConnectionTypes = new bool[10];
[Header("Costs & Resources")]
public List<int> constructionResource;
public List<int> constructionCost;
//public List<int> resourceMod;
//public List<int> modAmount;
public List<GameObject> ghostNodes;
private void Start() {
Initialize();
//grab all needed references even if it means searching for them in the scene
if (_controller == null || _cameraRay == null) {
Debug.LogWarning("UnassignedReferenceException : one or more of the reference variables was not set to an instance of an object. " +
"Automatically searching and assigning proper references.");
_controller = GameObject.FindObjectOfType<GameController>();
_cameraRay = GameObject.FindObjectOfType<CameraRay>();
}
//assign a module name if none was provided
if (moduleName == "" || moduleName == null) {
moduleName = "Unnamed Module";
}
//process navigation and AI information here
#region AI stuff;
#endregion;
////basic functionality per structure type to be carried out
//switch (structureType) {
// case 0:
// Debug.LogWarning("No functionality suported with an structureType: Undefined!");
// break;
// case 1: // "WallSegment"
// break;
// case 2:
// // temp = "BoxHab";
// break;
// case 3:
// //temp = "Connector";
// break;
// case 4:
// // temp = "ConnectorModule";
// break;
// default:
// //temp = "Undefined";
// break;
//}
//process resources and modifiers
_controller.modifyResource(0, 0, false);
//modify model and meshes
if (FloorVariations[0] != null) {
foreach (GameObject variation in FloorVariations) {
variation.SetActive(false);
}
FloorVariations[buidLevelIndex].SetActive(true);
}
}
//gat the type of structure from an integer
public void snapTo(Transform snapPoint) {
transform.SetPositionAndRotation(snapPoint.position, snapPoint.rotation);
}
public void snapToAndParent(Transform snapPoint) {
snapTo(snapPoint);
transform.parent = snapPoint.parent;
}
public void Initialize() {
_controller = GameObject.FindObjectOfType<GameController>();
_cameraRay = GameObject.FindObjectOfType<CameraRay>();
//approvedConnectionTypes = new bool[Exodus.Data.StructureTypes.Length];
if (_controller != null || _cameraRay != null) {
Debug.Log("References applied.");
}
else {
Debug.LogWarning("An error occured while trying to assign references.");
}
}
public void UpdateFloorVariations() {
if (FloorVariations[0] != null) {
foreach (GameObject variation in FloorVariations) {
variation.SetActive(false);
}
FloorVariations[buidLevelIndex-1].SetActive(true);
}
}
}
Comment