- Home /
Saving its values through custom Editor
This is my first time trying to create an Editor script to edit an prefab properties and values through the Editor. I am having issues saving the prefabs array. I am able to add/remove objects to the array, but once I click on the PLAY button, the array is destroyed.
This is my Editor script:
#pragma strict
@CustomEditor(MyController)
class MyControllerEditor extends Editor { function OnInspectorGUI() { var my : MyController = target as MyController;
GUILayout.BeginHorizontal("Button");
if ( GUILayout.Button("Add Object to Array") )
{
my.someArray.Push(new Object());
}
if ( GUILayout.Button("Remove Object from Array") )
{
my.someArray.RemoveAt(0);
}
if ( GUILayout.Button("Remove All from Array") )
{
my.someArray = new Array();
}
GUILayout.EndHorizontal();
my.randomVariable = EditorGUILayout.IntField("Hello", my.randomVariable);
}
}
This script is attached to the prefab:
#pragma strict
var someArray : Array = new Array(); var randomVariable : Int = 0;
class someObject() { var prop1 : String = "a value"; var prop10 : String = "a longer value"; }
Am I doing this the proper way? How come the array is being destroyed when I PLAY and STOP the game in the Preview? Side note: setting the 'randomVariable' in the Editor works fine.
Answer by Waz · Aug 11, 2011 at 10:40 PM
I'm not certain, but I suspect the problem is Unity not serializing an Array, since that won't work in the default inspector either. If you use a normal built-in array, it should work:
my.someArray += [new Object()];
etc.
var someArray = new Object[0];
Yah -- first make sure everything you need shows up in the normal Inspector. A trick for testing is to also call DrawDefaultInspector
in your CustomEditor -- changes from custom should be immediately reflected.
Awesome! The DrawDefaultInspector worked like a debug tool for me. And the my.someArray += [new Object()];
worked like a charm!
Thanks again!
Answer by karl_ · Aug 11, 2011 at 06:05 PM
I think you're looking for EditorUtility.SetDirty()
http://unity3d.com/support/documentation/ScriptReference/EditorUtility.SetDirty.html
I have looked at that and have tried this but it doesn't work:
EdtiorUtility.SetDirty(target);
Am I using incorrectly?
SetDirty just tags that an asset needs saving. It is not used for non-prefab changes like this.
Your answer
Follow this Question
Related Questions
Extending "GameObject/Create Other" with own prefab 1 Answer
Custom Data structs & Inspector 1 Answer
How Mark Prefab Dirty? 1 Answer
Unity editor and inspector header 0 Answers
How does Unity retain UnityEngine.Object references? 2 Answers