Ziboo
May 10, 2011, 6:27pm
1
Hi everyone,
I'm trying to make a tool, I'm king a new to Unity, so I don't know if I'm doing all good:
My GeoPainterEditor.js is where I do everything.
And GeoPainter.js is my MonoBehevior script to store my data, and maybe in the future do some stuff.
I have to variables in GeoPainter.js:
var myGroups : Array = new Array();
var nbrGroupsCreated:int;
I'm modifing this variables from my GeoPainterEditor.
When I close the editor and reopen "nbrGroupsCreated" was saved,
but not "myGroups" array.
I know why, cause I'm creating a new array each time via: "new Array()"
But I didn't find a way to only create ONCE and ONLY ONCE this array, and not each time I restart the editor ??
Here is my code:
GeoPainterEditor.js:
@CustomEditor(GeoPainter)
class GeoPainterEditor extends Editor {
private var isPainting = false;
private var myDistance = 0.1;
private var groupSelIndex : int = 0;
function OnInspectorGUI ()
{
EditorGUILayout.BeginHorizontal();
GUILayout.Label("Add/Remove Groups");
if(target.myGroups.length != 0 && GUILayout.Button ("-")) {
var index = (groupSelIndex -1);
var go = target.myGroups[index];
target.myGroups.RemoveAt(index);
DestroyImmediate(go);
}
if(GUILayout.Button ("+")) {
go = new GameObject ("GeoPainter_Group_" + target.nbrGroupsCreated.ToString());
target.nbrGroupsCreated = target.nbrGroupsCreated + 1;
target.myGroups.Add(go);
}
EditorGUILayout.EndHorizontal();
var nbrTemp = 1;
for(var obj in target.myGroups)
{
EditorGUILayout.BeginHorizontal();
GUILayout.Label("" + nbrTemp + ": ");
obj = EditorGUILayout.ObjectField(obj, GameObject);
nbrTemp++;
EditorGUILayout.EndHorizontal();
}
if(target.myGroups.length > 0)
{
EditorGUILayout.BeginHorizontal();
GUILayout.Label("Selected Group: ");
groupSelIndex = EditorGUILayout.IntSlider(groupSelIndex, 1, target.myGroups.length);
EditorGUILayout.EndHorizontal();
}
if(GUILayout.Button("Start Painting"))
{
}
myDistance = EditorGUILayout.FloatField("Distance",myDistance);
if(GUI.changed)
{
}
}
@MenuItem ("Azylum Tool/Add GeoPainter To Selection")
static function CreateMenu () {
// Get existing open window or if none, make a new one:
if(!GameObject.Find("GeoPainterSys"))
{
go = new GameObject ("GeoPainterSys");
go.AddComponent("GeoPainter");
Selection.activeGameObject = go;
}
}
}
GeoPainter.js
class GeoPainter extends MonoBehaviour
{
var myGroups : Array = new Array();
var nbrGroupsCreated:int;
}
Creating a new Array each time is not your problem. The Array will be created when the object is first instantiated. This is before deserialization. Unity uses serialization to save and load data in your scene and project.
The problem you have is that Array class contains 'object' types (not to be confused with UnityEngine.Object) so Unity does not know how to serialize those objects. Unity can only serialize value types like int, string, vector3, as well as many built in UnityEngine classes such as MonoBehaviour, GameObject, etc and custom classes you've written that are marked with the attribute [Serializable]
[Serializable]
public class MySerializableClass
{
}
In your case all you are storing in the array is the GameObject type. Replace the Array in your GeoPainter class with a built in array like this GameObject[].
class GeoPainter extends MonoBehaviour
{
var myGroups : GameObject[];
var nbrGroupsCreated:int;
}
Ziboo
May 10, 2011, 9:25pm
3
Thanks for your answer Antony,
But I have an error:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name)
UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
GeoPainterEditor.OnInspectorGUI () (at Assets/_AzylumTool/Editor/GeoPainterEditor.js:12)
UnityEditor.InspectorWindow.OnGUI () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/Mono/Inspector/InspectorWindow.cs:364)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
At the line:
if(target.myGroups.length > 0 && GUILayout.Button ("-")) {
in my OnInspectorGUI
Ziboo
May 10, 2011, 10:27pm
4
Thanks for your answers.
For C#, I'm from flash, AS3, so javascript is a bit simpler for me.
But it still doesn't fix the issue Antony:
I have something, but my array is still not saving:
GeoPainter.js
class GeoPainter extends MonoBehaviour
{
private var myGroupsBuiltIn : GameObject[];
var nbrGroupsCreated : int;
private var myGroups:Array;
}
GeoPainterEditor.js
@CustomEditor(GeoPainter)
class GeoPainterEditor extends Editor
{
...
function OnInspectorGUI ()
{
if(target.myGroups == null)
{
target.myGroups = new Array(target.myGroupsBuiltIn);
target.myGroups.RemoveAt(0);
return;
}
...
...
...
target.myGroupsBuiltIn = target.myGroups.ToBuiltin(GameObject);
}
}
I have to make a "target.myGroups.RemoveAt(0)" cause it displayed a "none gameObject"...
Maybe because when you create it "private var myGroupsBuiltIn : GameObject[]" it creates it with 1 gameObject inside ?
I'm really kind of lost, and stuck...