• 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
Question by Vice_Versa · May 21, 2015 at 09:43 PM · c#programmingserializable

what does [Serializable] mean in C Sharp

I realize t$$anonymous$$s is probably a stupid question and the answer in online, but from the descriptions im finding i just dont get it

unitys documnetation says "The Serializable attribute lets you embed a class with sub properties in the inspector."

what subproperties is t$$anonymous$$s talking about and why would i need to use [Serializable] in a script?

Comment

People who like this

0 Show 0
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

5 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Michael-Glen-Montague · May 21, 2015 at 10:36 PM

  • [Serializable] is an attribute added above a class name to allow it and its fields to be broken down into a byte[] array (binary data) and passed around easily. A common use for it is to serialize a class filled with data about your current game session to a save file, and then later you can deserialize the binary byte[] data out of that save file as an object. You then cast the type of that object back to the type of whatever class it was before.

  • [HideInInspector] is an attribute added above a public field to $$anonymous$$de it from Unity's inspector. Whatever value you had set it to before you $$anonymous$$d it will be saved because it's still serialized.

  • [NonSerialized] is added above a public field to disable serialization on it, meaning it won't be visible in Unity's inspector and its value won't be saved for the next time you open the project. T$$anonymous$$s is best used when the field's value is set during run-time rather than in the editor.

  • [SerializeField] is added above a private field to allow serialization on it, thus making it visible in Unity's inspector and allowing its data to be saved.

Classes with the [Serialized] attribute are also visible in the inspector, meaning you can edit their values and Unity will serialize (save) it for the next time you open the project.

Comment
Vice_Versa
exorakhilas
alek09
Quintinium
Yaxir
Octano
ken_the_menace
torsigurdson
EduYukio
SkylinR
alikanat
zer011
Erfan_Time
jjbish
bogheorghiu
And 10 more...

People who like this

25 Show 0 · 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

Answer by tanoshimi · May 21, 2015 at 09:55 PM

You'd use it to keep a class member private, but still expose it so its initial value could be set via the inspector.

Comment
Vice_Versa
exorakhilas
Quintinium
Yaxir
Ninjjoy

People who like this

5 Show 0 · 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

Answer by DoTA_KAMIKADzE · May 21, 2015 at 10:12 PM

Basically t$$anonymous$$s is sort of "defined way to save state of your object(variables, etc.) so that they later can be read and set to original state", t$$anonymous$$nk of it like a mark that allow writing all of your object values into file,DB,RAM,stream,etc. w$$anonymous$$ch can basically be used in tons of different ways, but all comes to the same purpose - recreate your object exactly as it was or part of it w$$anonymous$$ch you need, you can even make your own Serialization/Deserialization with w$$anonymous$$ch you decide how and what. If that makes no sense to you then you can always get in touch of what it is like for example THERE.

P.S. Ah yeah also forgot to answer the last part of your question:

1) Somet$$anonymous$$ng like a class that contains few variables.

2) Why do you need to use? You don't. You can if you want to see it in inspector and/or save its state.

Comment
Vice_Versa

People who like this

1 Show 1 · 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 Vice_Versa · May 21, 2015 at 10:29 PM 0
Share

Didnt really understand the first half of what you said, but everything after ps was very helpful thank you

avatar image

Answer by Vice_Versa · May 21, 2015 at 10:28 PM

that makes so much sense thank you

Comment

People who like this

0 Show 0 · 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

Answer by UnityDeveloper2 · Mar 16, 2018 at 03:18 AM

You can use [Serializable] aka [System.Serializable] on certain classes to expose object behavior in the editor that would otherwise need to be inherited and hard-coded.

An example is UnityEvent (see https://docs.unity3d.com/Manual/UnityEvents.html "Generic UnityEvents"), w$$anonymous$$ch uses Serializable to allow the user to add an arbitrary number of function callbacks in the event using the Inspector.

Comment

People who like this

0 Show 0 · 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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Cannot implicitly convert type `int' to `healthgui' 1 Answer

Bringing Down the Buttons 0 Answers

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Help needed on creating a options menu 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