- Home /
Saving custom inspector references
I have a custom inspector script that references a couple of gameobjects in the scene. Unfortunately every time I close and restart unity all of the references on my custom inspector are reset to null. Is there any way of saving these references or has anyone found a workaround to do this?
are you referring to game objects in the default references?
All of the references on the custom inspector script including gameobjects, int, float and bool are reset when I open unity.
Answer by IBDelta · Aug 06, 2011 at 02:31 PM
Continuing my tradition of answering my own questions, here's the answer. The variables must be serialised. What had confused me was several forum threads that claimed serialization didn't work when unity was restarted, this is not true. To help my fellow n00b's here are two scripts that will serialise string, int, float and gameobjects. For some reason I'm getting a warning that ObjectField is no longer supported. I've used the new system of "Allow Scene Objects", implemented in the code below, but I'm still getting this warning. Is this just unity having a bit of a weird turn over the changing systems or have I done something wrong? Either way the code compiles and runs with no real problems.
The first script should be dragged onto your object in the inspector.
SerializeTest.cs
using UnityEngine;
[System.Serializable]
public class SerializeTest : MonoBehaviour
{
[SerializeField]
public string TestString=" ";
[SerializeField]
public int TestInt=0;
[SerializeField]
public float TestFloat=0f;
[SerializeField]
public GameObject TestObject=null;
}
The second script should be put into a folder called "Editor".
SerializeTestEditor.cs
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(SerializeTest))]
public class SerializeTestEditor : Editor {
bool allowSceneObjects;
public override void OnInspectorGUI()
{
SerializeTest TargetScript = target as SerializeTest;
GUILayout.Label("Serialize test");
EditorGUILayout.Separator();
GUILayout.BeginHorizontal();
GUILayout.Label("String");
TargetScript.TestString = EditorGUILayout.TextField(TargetScript.TestString);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Int");
TargetScript.TestInt = EditorGUILayout.IntField(TargetScript.TestInt);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Float");
TargetScript.TestFloat = EditorGUILayout.FloatField(TargetScript.TestFloat);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Object");
allowSceneObjects = !EditorUtility.IsPersistent(target);
TargetScript.TestObject = EditorGUILayout.ObjectField(TargetScript.TestObject, typeof(GameObject), allowSceneObjects) as GameObject;
GUILayout.EndHorizontal();
}
}
Your answer
Follow this Question
Related Questions
How to access Custom Inspector own gameObject 1 Answer
Drawing a custom variable in the inspector y position bugs selection 0 Answers
Custom inspector variable resets automatically 1 Answer
Custom Editor (lack of) Polymorphism 6 Answers
Custom Editor for Interface/Child classes not displaying 2 Answers