Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MarkD · Dec 12, 2013 at 11:14 PM · editor-scriptingstuckserializablejavascript-specific

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?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hoeloe · Dec 13, 2013 at 12:31 AM 0
Share

Worth saying that Javascript =/= Java.

avatar image Statement · Dec 13, 2013 at 12:59 AM 2
Share

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.

avatar image Eric5h5 · Dec 13, 2013 at 01:07 AM 0
Share

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.

avatar image MarkD · Dec 13, 2013 at 06:27 PM 0
Share

Ok, I see, now that we got this cleared out does any one has an actual answer on the topic?

avatar image MarkD · Dec 13, 2013 at 06:30 PM 0
Share

@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.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
     
 }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarkD · Dec 12, 2013 at 11:48 PM 0
Share

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?

avatar image MarkD · Dec 12, 2013 at 11:54 PM 0
Share

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

avatar image
0

Answer by flaviusxvii · Dec 12, 2013 at 11:17 PM

[System.Serializable] for classes.

[SerializeField] for fields.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarkD · Dec 12, 2013 at 11:21 PM 0
Share

yes I tried that before, but it spits out this error Unexpected token: [

avatar image MarkD · Dec 12, 2013 at 11:25 PM 0
Share

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 ();
     
         }
         
avatar image flaviusxvii · Dec 12, 2013 at 11:26 PM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/Serializable.html

extend System.Object I guess.

avatar image flaviusxvii · Dec 12, 2013 at 11:33 PM 0
Share

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.

avatar image flaviusxvii · Dec 12, 2013 at 11:43 PM 0
Share

"Unity themselves calls it java" <-- No. Nowhere is this true. Please point out any instances so they can be notified.

avatar image
0

Answer by Eric5h5 · Dec 13, 2013 at 01:10 AM

Really short answer: you don't need it, it's automatic.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges