• 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
11
Question by adam michaels · Dec 14, 2009 at 11:28 PM ·

Edit-and-Continue Scripting - Destroys Current State.

I like the semi-Edit-and-Continue feature of Unity because it allows me to insert Debug.Log() dynamically to try and find/fix bugs, and allows me to maintain the current application state where it is. BUT, the achilles heel of this whole thing is that each time I make any small edit to any script -- it invalidates all DLL's (scripting assembly plus other DLL's that I'm using that run on Mono). Therefore, I lose nearly ALL dynamic state.

I have found that the ONLY thing that is "Saved/Restored" are Script Public Fields... which is a good starting point. So I can place some dynamic Fields here, and am then able to restore some of my dynamic state.

But I also have some dynamic state that is stored into an ArrayList of Key/Value types. I have experimented with the concept of saving the entire ArrayList into a Public String Field, on a global component - and this "sorta works". The problem is that when Script Editing occurs -- it happens without warning, so I don't have a good event handler to make the serialization into String occur JIT. I don't want to be doing this every frame, or with every change... that is too wasteful. I'd like to do it JIT.

Is there a global event I can register for that occurs BEFORE the scripts are invalidated and "stored"?? If so, then I can register for this event, and serialize my ArrayList to a public STring Field, that will then be Saved/Restored. On the other side of things, I already have the "OnEnable()" event that gets called, wherein, I can restore saved state from String back to the ArrayList.

This whole thing is a bit kludgy. I'd prefer to NOT be using this kludge, but if there is no other good alternative, then I must use it, else we can't enjoy the benefits of Edit-and-Continue.


In short, my questions are: 1. Is there an event that triggers when a Script is Edited (i.e. invalidated?), that I can register for, that occurs BEFORE Script/Component Fields are saved???

OR, even better...

  1. Is there a better way for me to Save/Restore Dynamic State of my Scripts during Script Editing run-time, for object types that are not predefined by Unity? (e.g. if you can help me get it working for ArrayList, then I'm confident I can make it work for most other types).


NOTE: I also tried making my type ISerializable, hoping that Unity might call this -- but no luck! It only seems to save/restore Public fields.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
17

Answer by Lucas Meijer 1 · Dec 15, 2009 at 12:28 PM

There is no event. Unity will serialize all your script components, reload the new assemblies, and recreate your script components from the serialized verions. This serialization does not happen with .NET's serialization functionality, but with an internal Unity one. The serialization system used can do the following:

  • CAN serialize public nonstatic fields (of serializable types)
  • CAN serialize nonpublic nonstatic fields marked with the [SerializeField] attribute.
  • CANNOT serialize static fields.
  • CANNOT serialize properties.

Your field will only serialize if it is of a type that Unity can serialize:

Serializable types are:

  • int
  • float
  • string
  • array of a serializable type
  • List of a serializable type (new in Unity2.6)
  • GameObject
  • Components
  • Texture2D
  • AnimationClip
  • All other unity things like that
  • Your custom classes, when you mark them with [Serializable]. (this is a bit of an oversight on our end, as [Serializable] is actually a .net attribute).

Headsup note: if you put one element in a list (or array) twice, when the list gets serialized, you'll get two copies of that element, instead of one copy being in the new list twice.

Trick: we don't serialize Dictionary, however you could store a List<> for keys and a List<> for values, and sew them up in a non serialized dictionary on Awake(). This doesn't solve the problem of when you want to modify the dictionary and have it "saved" back, but it is a handy trick in a lot of other cases.

For UnityScript users: Fields in c# is a script variable in UnityScript, and [SerializeField] becomes @SerializeField. [Serializable] on a class becomes @script Serializable in a UnityScript

Comment
Add comment · Show 4 · 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 Brian-Kehrer · Dec 31, 2009 at 07:26 PM 1
Share

is there a way to access [SerializeField] on the iPhone? I'd rather not make all the fields public.

avatar image Tetrad · Jun 11, 2010 at 11:21 PM 0
Share

I've pointed to this answer several times. Is this list in the documentation somewhere?

avatar image FiveOhOne · Oct 06, 2010 at 01:51 PM 1
Share

@Tetrad: I just found it in here: http://unity3d.com/support/documentation/ScriptReference/SerializeField.html

avatar image yoyo · Jan 04, 2011 at 01:42 AM 1
Share

If anyone is going to dive into Reflection-based serialization then the package at http://zeroandone.ca/unity might give you a headstart. It does deep exa$$anonymous$$ation of the scene, including exploration of Fields and Properties. It can dump to a file, but no attempt at deserialization is made (Here Be Dragons! :-)

avatar image
0

Answer by dilmer · Feb 27, 2019 at 06:21 PM

What if I wanted to serialize an "UnityAction" in a scriptable object?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Material doesn't have a color property '_Color' 4 Answers


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