Date post: | 02-Dec-2014 |
Category: |
Technology |
Upload: | unity-technologies-korea |
View: | 1,888 times |
Download: | 1 times |
Serialization In DepthTim Cooper
Tuesday, 9 April 13
09/04/2013 Page
Who Am I?
• Developer at Unity
• Used to make games!
• Bioshock 1 / 2
• Responsible for:
• Editor features
• Graphics features
2
Tuesday, 9 April 13
09/04/2013 Page
Topics
• Overview
• How serialization works
• How to serialize
• Classes
• Class References
• ScriptableObject
• Arrays
3
Tuesday, 9 April 13
09/04/2013 Page
Topics
• Working with Assets
• Creating an asset
• Custom GUI for assets
• Using an asset at runtime
• Sub-assets
4
Tuesday, 9 April 13
09/04/2013 Page
Overview
• Why write nicely serializable classes?
• Editor windows will survive assembly reload
• Ability to save data as custom asset files
• Easier the writing your own
• Once you know how it works ;)
5
Tuesday, 9 April 13
09/04/2013 Page
Overview
• When does serialization happen?
• On assembly reload
• Script recompilation
• Enter / exit play mode
• Loading and saving the project / scene
6
Tuesday, 9 April 13
09/04/2013 Page
How serialization works
• Assembly reload
• Pull all data out of managed (mono) land
• Create internal representation of the data on C++ side
• Destroy all memory / information in managed land
• Reload assemblies
• Reserialize the data from c++ into managed
• If your data does not make it into c++ it will go away!
7
Tuesday, 9 April 13
09/04/2013 Page
Serializing Classes
• A simple class will not automatically serialize!
• Unity does not know if the class is meant to serialize or not!
• We can fix this!
8
Tuesday, 9 April 13
09/04/2013 Page
Serializing Classes
• A class needs to be marked up to serialize
• [Serializable]
• Field serialization rules
• public - always serialize
• private / protected - serialize on assembly reload (editor)
• static - never serialize
9
Tuesday, 9 April 13
09/04/2013 Page
Serializing Classes
• Field serialization modifiers
• [SerializeField] - Make private / protected fields serialize
• [NonSerialized] - Never serialize the field
10
Tuesday, 9 April 13
09/04/2013 Page
Serializing Structs
• User structs don’t serialize
• A few built in ones do
• Don’t use them for serialization!
11
Tuesday, 9 April 13
09/04/2013 Page
Serializing Class References
• Normal class references
• Each reference serialized individually
• When you deserialize... you have different members
• Think of them as behaving like structs!
• Use them when you have:
• Data that is references only once
• Nested data
12
Tuesday, 9 April 13
09/04/2013 Page
Serializing ScriptableObject
• Serialize as reference properly!
• Multiple references to this object
• Will resolve properly on deserialization
• Supports some Unity callbacks
• OnEnable, OnDisable, OnDestroy
• Create them with CreateInstance <type> ()
13
Tuesday, 9 April 13
09/04/2013 Page
Serializing ScriptableObject
• Use them when you want data
• That is referenced multiple times
• Shared data
• Needs Unity system callbacks
14
Tuesday, 9 April 13
09/04/2013 Page
Serializing ScriptableObject
• Initialization order
• Instance created
• Fields deserialized into object
• If they exist on the c++ side ;)
• OnEnable() Called
15
Tuesday, 9 April 13
09/04/2013 Page
Serializing ScriptableObject
• To create fields properly inside a SO
• Don’t create them in constructor
• Check if they are null in OnEnable ()
• If not null... then they were deserialized
• if null... create them!
16
Tuesday, 9 April 13
09/04/2013 Page
Hideflags
• Control visibility
• Important if you are NOT saving the asset or holding a reference to it
• i.e editor only data structure referenced by a window
• HideAndDontSave
• No asset / scene root - tells unity to consider this a root object
• Will not get cleaned up on scene load (play mode)
• Destroy using Destroy ()
17
Tuesday, 9 April 13
09/04/2013 Page
Serializing Concrete Arrays
• Works as expected
• No object sheering
• Serialized and deserialized properly
• More complex objects?
18
Tuesday, 9 April 13
09/04/2013 Page
Serializing Base Class Arrays
• Does not work as expected
• Breaks on deserialization
• Object shearing occurs
• How can we serialize more complex hierarchies?
19
Tuesday, 9 April 13
09/04/2013 Page
Serializing General Array
• ScriptableObject Array!
• Serializes as references...
• Will serialize as expected
• Only need to set hideflags on ‘the most root’ object
20
Tuesday, 9 April 13
09/04/2013 Page
Asset Creation
• Design the class you want to be an asset
• Database, Character Info, ect
• Ensure that the ‘root’ is a ScriptableObject
• An asset file is a ScriptableObject
• Create the assed by calling
• AssetDatabase.CreateAsset (object, “location.asset”)
• If is mandatory to use the .asset file extension
21
Tuesday, 9 April 13
09/04/2013 Page
Custom Asset UI
• Your asset is just like a Unity asset!
• You can write custom editors
• [CustomEditor (typeof (YourType))]
• Extend from the EditorClass
• Remember to put in in an editor folder!
22
Tuesday, 9 April 13
09/04/2013 Page
Property Fields
• Delegate drawing to a separate class
• Useful for custom controls
• Can be implicitly linked to a class
• [CustomPropertyDrawer (typeof (MyType))]
• Does not need to be marked up per field
• Can be hooked up via an attribute
• Marked on a per field basis!
23
Tuesday, 9 April 13
09/04/2013 Page
Property Fields
• How?
• [CustomPropertyDrawer (typeof (MyType))]
• Extend PropertyDrawer
• Need a custom height?
• Override GetPropertyHeight ()
• Do your custom drawing
• OnGUI ()
24
Tuesday, 9 April 13
09/04/2013 Page
Using Assets
• Create a reference to the type
• Can be anywhere:
• EditorWindow
• MonoBehaviour
• Connect it to an asset
• Via code of the inspector
• Do game specific things!
25
Tuesday, 9 April 13
09/04/2013 Page
Using Sub-Assets
• ScriptableObjects
• Save each manually... they won’t serialize to file all the way down
• How?
• Add them as children of another asset (AddObjectToAsset)
• By default it will show all assets as sub assets
• Set HideFlags to HideFlags.HideInHierarchy
• Much nicer :)
26
Tuesday, 9 April 13
09/04/2013 Page
Using Sub-Assets
• Use SubAssets for complex data structures
• Where many elements are ScriptableObjects
• Graphs
• Databases
• ect!
27
Tuesday, 9 April 13