- Home /
Changes not being saved in the Inspector Editor
I'm developing a custom GUI system for Unity and I have a couple of programmed elements for now. One of then is a panel. Basically, this allow me to drag a couple of GUIZone (class that every item of my own GUI derives from) in some slots. The number of slots can be set by a int variable that basically create an array of n elements and start a loop that go from 0 to n creating an ObjectField for each of this elements.
The script I'm using is the following:
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
[CustomEditor(typeof(GUIZonePanel))]
public class GUIZonePanelEditor : Editor {
private GUIZonePanel _target;
private int _elementsInPanel;
public override void OnInspectorGUI () {
GUILayout.Label("Insert the elements you want to be in this panel");
_target = target as GUIZonePanel;
_elementsInPanel = EditorGUILayout.IntField("Number of elements", _elementsInPanel);
if(GUI.changed) {
if(_elementsInPanel > _target.panelElements.Count) {
for(int i = _target.panelElements.Count; i < _elementsInPanel; i++) {
_target.panelElements.Add( null );
}
} else {
for(int i = _target.panelElements.Count; i > _elementsInPanel; i--) {
_target.panelElements.RemoveAt(i - 1);
}
}
}
for(int i = 0; i < _elementsInPanel; i++) {
_target.panelElements[i] = (GUIZone)EditorGUILayout.ObjectField("Element " + i.ToString() ,_target.panelElements[i], typeof(GUIZone));
}
bool apply = GUILayout.Button( "Put elements in the panel");
if(apply) {
_target.applyElementsToThePanel();
}
bool _allign = GUILayout.Button("Allign with camera");
if(_allign) {
Vector3 cameraRotation = Camera.main.transform.eulerAngles;
Vector3 cameraPosition = Camera.main.transform.position;
Camera.main.transform.eulerAngles = new Vector3(90, 0, 0);
Camera.main.transform.position = new Vector3(0, 10, 0);
_target.transform.position = Vector3.zero;
_target.transform.parent = Camera.main.transform;
Camera.main.transform.eulerAngles = cameraRotation;
Camera.main.transform.position = cameraPosition;
}
if(GUI.changed) {
EditorUtility.SetDirty( _target );
}
}
}
And here's the GUIZonePanel script:
using UnityEngine;
using System.Collections.Generic;
public class GUIZonePanel : GUIZone {
public List<GUIZone> panelElements;
public void applyElementsToThePanel () {
Vector3 resultantPosition = Vector3.zero;
for(int i = 0; i < panelElements.Count; i++) {
resultantPosition += panelElements[i].transform.position;
}
transform.position = resultantPosition/panelElements.Count;
foreach(GUIZone gz in panelElements) {
gz.transform.parent = this.transform;
}
}
public void changeElementsVisibility (bool visible) {
foreach(Renderer r in GetComponentsInChildren<Renderer>()) {
r.enabled = visible;
}
}
}
The problem is that when I deselect the object that contains the GUIPanel and select it again, every change is losted. My element number get back to 0 and this happens with almost every script where I have a list or an array and I need to attach elements to this array in the Editor.
What can I make to save this changes when the GO gets deselected?
Thanks from now!
Answer by Rafes · Jul 06, 2012 at 09:45 PM
The GUI's job is to display information and allow the user to interact with it. Anything you want to store needs to be sent to the data model, which is your component script. In other words, you need to write everything you want kept to a serializable script field, such as an int, float, string, list<>, an custom serializable class you make, etc.
Regarding a naming convention, I suggest calling it something like "_uiPanelElementsBacker". The underscore is a general convention that denotes it is for internal use. A "backer" or "backing field" is a field (class member) that exists only to hold information for something more publicly accessible.
[Edit: Your code formatting could be improved. I might have missed the actual cause. Please fix it and I won't be so lazy.]
Thanks for your comment! A strange thing is that I have other script to create an AnimatedSprite, and this script has a Sprite class instance, which is a serialized class. It also contains a array that holds the frames, but seems like the changes are not being saved to this array, even the class being serializable.
*What you mean by horrible code formatting?
Can you see the list in the debug inspector? If you can see it there, then you know you are safe, since that displays all serialized fields that will be saved. If you DO see it there, and it still isn't saved, then you know there is something deeper you are doing to wiping out your list.
Your code indentation makes it hard to follow for me. I have very very lazy eyes, hah.
Hahahah! I don't know. It has some better format but it just dissapeared! Well, I could solve the problem following your hints! Thank you!
Your answer
Follow this Question
Related Questions
Show and Edit Attributes of my list of structs in EditorWindow. 0 Answers
A node in a childnode? 1 Answer
Is it possible to drag/drop/increment into editor array? 1 Answer
How can I make an array fill the contents of my GUI List? 1 Answer
How to add a reorderable list on CUSTOM EDITOR WINDOW? 0 Answers