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 <em>= (GUIZone)EditorGUILayout.ObjectField("Element " + i.ToString() ,_target.panelElements*, typeof(GUIZone));*</em>
}
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 panelElements;
public void applyElementsToThePanel () {
Vector3 resultantPosition = Vector3.zero;
for(int i = 0; i < panelElements.Count; i++) {
resultantPosition += panelElements*.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()) {
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!