- Home /
Custom Editor loosing data on play
Hi! I'm trying to make a custom editor, but when I hit play, all the data gets losts, and when y stop the game, the values resets to it's defaults values.
My variables:
public GameObject [] prefabs; public int [] maxSamples; public Vector3 bufferZone;
My editor code:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
[CustomEditor(typeof(BetterInstantiate))]
public class BetterInstantiateEditor : Editor {
private List<GameObject> prefabs;
private List<int> maxSamples;
public override void OnInspectorGUI()
{
BetterInstantiate betterInstantiateScript = (BetterInstantiate)target;
betterInstantiateScript.bufferZone = EditorGUILayout.Vector3Field ("Buffer Zone:", betterInstantiateScript.bufferZone);
if (this.prefabs == null) //Aún no hemos creado los ArrayList
{
this.prefabs = new List<GameObject>();
this.maxSamples = new List<int>();
}
int numberOfPrefabs = this.prefabs.Count;
EditorGUILayout.LabelField("Number of Prefabs:", numberOfPrefabs.ToString());
if (GUILayout.Button ("Add Prefab"))
{
numberOfPrefabs ++;
this.prefabs.Add ((GameObject)null);
this.maxSamples.Add(0);
}
if (GUILayout.Button ("Remove Prefab") && numberOfPrefabs != 0)
{
numberOfPrefabs --;
this.prefabs.RemoveAt(this.prefabs.Count - 1);
this.maxSamples.RemoveAt(this.maxSamples.Count - 1);
}
for (int i = 0; i < numberOfPrefabs; i++)
{
this.prefabs [i] = ((GameObject)EditorGUILayout.ObjectField ("Prefab:", (GameObject)this.prefabs [i], typeof(GameObject), false));
this.maxSamples [i] = (EditorGUILayout.IntField("Max Sim. Objects:", (int)this.maxSamples [i]));
}
//Comprobamos que no haya dos prefabs con el mismo nombre
ArrayList names = new ArrayList();
for (int i = 0; i < numberOfPrefabs; i++)
{
if (this.prefabs [i] == null)
continue;
for (int j = 0; j < names.Count; j++)
{
if (string.Compare(((GameObject)this.prefabs [i]).ToString(), (string)names [j]) == 0)
Debug.LogError("You can't put 2 prefabs with the same name!");
}
names.Add(((GameObject)this.prefabs [i]).ToString());
}
betterInstantiateScript.prefabs = this.prefabs.ToArray();
betterInstantiateScript.maxSamples = this.maxSamples.ToArray();
}
}
I tried using Serializable and System.Serialize but it didn't work. The editor don't save nothing, the Vector3 is lost too, while in other custom editors I made this didn't happened.
Thank you.
Answer by Adam-Mechtley · Sep 08, 2016 at 12:30 PM
@miguellahoz: Hi there! You need to mark the fields you want to persist with the SerializeField attribute.
Your answer
Follow this Question
Related Questions
Questions Regarding Images in Custom Inspector/Editor 1 Answer
Working with Arrays for Custom Inspector 0 Answers
Can a GameObject's full inspector be drawn in a custom Editor Window ? 1 Answer
ReImport in c# of GameObjects only for Scene Objects, not assets? 1 Answer
UnityEventBase editor changed callback 0 Answers