- Home /
JavaScript version of [Serializable ]?
The question is simple, I need to know if there is a unity script variant of [Serializable ].
I tried @script System.Serializable and @System.Serializable. The first one gave no errors but still did not save my editor scripts values when deselecting or going into play mode and the second one did not work at all.
The only info I find on google are all in C# and obviously the C# variant [Serializable ] cannot be used with a unity script approach.
I am really stuck here, I have been debugging and searching the net for hours now.
Thx ahead.
Mark
EDIT: Ok so after debugging for a while I desicovered that the information actualy does get serialized, the problem what is happening is that it does not get loaded back in. So when I deselect an object or go into play mode, the script will go back to its original state and does not reload the saved values. Any ideas with this new info?
UnityEditor.Editor class extends from UnityEngine.Object, so any public fields should be serialized by default. Any non-public fields needs `@SerializeField` added to the field. Only serializable fields will be serialized.
Java and Javascript are two different languages; you shouldn't say Java if you mean Javascript. Not even as slang or an abbreviation. And not even in the context of using Unity, since it's quite possible that you may actually be talking about Java in regards to Android usage.
Ok, I see, now that we got this cleared out does any one has an actual answer on the topic?
@Statement, yea I know, but that is the problem, it isn't doing it , even with the field added, everything gets reset, although the debugger tells me otherwise, it is very confusing.
Answer by MarkD · Dec 14, 2013 at 12:14 AM
After hourse of debugging and stumbling trough code, I finaly found the problem myself. The problem was that the booleans and float variables (inside the editor script)controlling my external scripts where the problem. Instead of putting those booleans on my external booleans I just had to create a toggle-button for the external boolenas.
At first I had a toggle controlling the editor booleans, wich on their turn would be applied to the external script and now it all refers directly to the external script so.
//this is the version that did not work because an editor script even tough tagged, will never
//serialize and always reset on play or deselect.
class GeneratorPreview extends Editor {
//apply only to this generator
var CurrentGenerator = Selection.activeGameObject;
function OnInspectorGUI() {
CurrentGenerator =GUILayout.Toggle(CurrentGenerator ,"Cylinder cast");
CurrentGenerator.GetComponent(LightShaftGenerator).CastCylinder=CurrentGenerator ;
//Altough this works, it will only work in the editor view as long
//the object is selected.
//As soon as the user selects something different or goes into play-mode
//the variable CastCylinder will reset and thus resetting the
//referenced component (LightShaftGenerator).CastCylinder.
}
}
//working version Here we access all Boolean's directly from its
//component source.
//This way none of the components will listen to anything
//from the editor script and get serialized as normally
//by its own script (in this case LightShaftGenerator).
//As long as you link the components variables directly to
//the button functions or toggles of the editor script everything
//will be linked nicely and serialized nicely.
}
class GeneratorPreview extends Editor {
//apply only to this generator
var CurrentGenerator = Selection.activeGameObject;
function OnInspectorGUI() {
CurrentGenerator.GetComponent(LightShaftGenerator).CastCylinder=GUILayout.Toggle(CurrentGenerator.GetComponent(LightShaftGenerator).CastCylinder,"Cylinder cast");
}
}
Answer by Bunny83 · Dec 12, 2013 at 11:42 PM
First all objects that are derived from UnityEngine.Object are serializable by default, even in C#.
In Unityscript (please don't call it Java since that's a totally different language) all custom classes (derived from System.Object) are also by default serializable. So you don't need this attribute at all.
However the usual syntax for attributes is like you posted in your question:
@ATTRIBUTENAME
Thing_that_gets_the_attribute
Unity just has the @script ATTRIBUTENAME version for MonoBehaviours since you don't have a class where you can put the attribute in front of.
If you read my question you will see that I am reffering to unity script and not java script. I know they are two different languages, flaviusxvii also gave me the same flame. The reason I said java is just because the docs have the selection JavaScript C# and Boo. so from there.
anyway back to topic :).
If my setup is correct, why do all my editor variables reset on play mode and deselection?
Ok this one does not throw any errors either, @System.Serializable, but still nothing gets serialized in the inspector, yet the debugger says it does.
edit: also the editor.prefs doesn't load back any values, it does serialize the once i write away, but again no get function gets called even tough it is declared.
I am going off for now, going to break my head on it in the morning, thank you very much for your time and input.
greetings
$$anonymous$$ark
Answer by flaviusxvii · Dec 12, 2013 at 11:17 PM
[System.Serializable] for classes.
[SerializeField] for fields.
yes I tried that before, but it spits out this error Unexpected token: [
Simple version of script, with all the components and classes.
#pragma strict
import System.Collections;
@CustomEditor(LightShaftGenerator)
[System.Serializable]
class GeneratorPreview extends Editor {
var SquareCast:boolean=true;
var CylinderCast:boolean=false;
var wallcast:boolean=false;
var RoofFloorCast:boolean=false;
var CylinderRadius:float=1;
function OnInspectorGUI() {
//save when changed
if (GUI.changed){
EditorUtility.SetDirty(CurrentGenerator.transform);
Debug.Log("saved");
}
// Show default inspector property editor
DrawDefaultInspector ();
}
http://docs.unity3d.com/Documentation/ScriptReference/Serializable.html
extend System.Object I guess.
http://answers.unity3d.com/questions/263467/custom-inspector-for-custom-class-that-does-not-ex.html
Follow the answers there I think.. Property Drawers seem to be what you need.
Also, Java has nothing to do with Unity. Java =/= Javascript.
"Unity themselves calls it java" <-- No. Nowhere is this true. Please point out any instances so they can be notified.
Answer by Eric5h5 · Dec 13, 2013 at 01:10 AM
Really short answer: you don't need it, it's automatic.
Your answer
Follow this Question
Related Questions
Show serializable object in a CustomEditor 0 Answers
How to expose a struct type variable from dll, which is not declared [Serializable] 1 Answer
How to hide/show serialized class object in the inspector with boolean value change? 1 Answer
Property field without Header 1 Answer
Should EditorGUILayout.PropertyField work with serializable classes? 1 Answer