Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

[UniteKorea2013] Serialization in Depth

2.984 Aufrufe

Veröffentlicht am

유나이트 코리아 2013 발표자료: 유니티 시리얼라이제이션 시스템 소개 (팀 쿠퍼)

Veröffentlicht in: Technologie, Seele & Geist
  • Als Erste(r) kommentieren

[UniteKorea2013] Serialization in Depth

  1. 1. Serialization In DepthTim CooperTuesday, 9 April 13
  2. 2. 09/04/2013 PageWho Am I?• Developer at Unity• Used to make games!• Bioshock 1 / 2• Responsible for:• Editor features• Graphics features2Tuesday, 9 April 13
  3. 3. 09/04/2013 PageTopics• Overview• How serialization works• How to serialize• Classes• Class References• ScriptableObject• Arrays3Tuesday, 9 April 13
  4. 4. 09/04/2013 PageTopics• Working with Assets• Creating an asset• Custom GUI for assets• Using an asset at runtime• Sub-assets4Tuesday, 9 April 13
  5. 5. 09/04/2013 PageOverview• 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 ;)5Tuesday, 9 April 13
  6. 6. 09/04/2013 PageOverview• When does serialization happen?• On assembly reload• Script recompilation• Enter / exit play mode• Loading and saving the project / scene6Tuesday, 9 April 13
  7. 7. 09/04/2013 PageHow 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!7Tuesday, 9 April 13
  8. 8. 09/04/2013 PageSerializing 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!8Tuesday, 9 April 13
  9. 9. 09/04/2013 PageSerializing 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 serialize9Tuesday, 9 April 13
  10. 10. 09/04/2013 PageSerializing Classes• Field serialization modifiers• [SerializeField] - Make private / protected fields serialize• [NonSerialized] - Never serialize the field10Tuesday, 9 April 13
  11. 11. 09/04/2013 PageSerializing Structs• User structs don’t serialize• A few built in ones do• Don’t use them for serialization!11Tuesday, 9 April 13
  12. 12. 09/04/2013 PageSerializing 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 data12Tuesday, 9 April 13
  13. 13. 09/04/2013 PageSerializing 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> ()13Tuesday, 9 April 13
  14. 14. 09/04/2013 PageSerializing ScriptableObject• Use them when you want data• That is referenced multiple times• Shared data• Needs Unity system callbacks14Tuesday, 9 April 13
  15. 15. 09/04/2013 PageSerializing ScriptableObject• Initialization order• Instance created• Fields deserialized into object• If they exist on the c++ side ;)• OnEnable() Called15Tuesday, 9 April 13
  16. 16. 09/04/2013 PageSerializing 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!16Tuesday, 9 April 13
  17. 17. 09/04/2013 PageHideflags• 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 ()17Tuesday, 9 April 13
  18. 18. 09/04/2013 PageSerializing Concrete Arrays• Works as expected• No object sheering• Serialized and deserialized properly• More complex objects?18Tuesday, 9 April 13
  19. 19. 09/04/2013 PageSerializing Base Class Arrays• Does not work as expected• Breaks on deserialization• Object shearing occurs• How can we serialize more complex hierarchies?19Tuesday, 9 April 13
  20. 20. 09/04/2013 PageSerializing General Array• ScriptableObject Array!• Serializes as references...• Will serialize as expected• Only need to set hideflags on ‘the most root’ object20Tuesday, 9 April 13
  21. 21. 09/04/2013 PageAsset 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 extension21Tuesday, 9 April 13
  22. 22. 09/04/2013 PageCustom 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!22Tuesday, 9 April 13
  23. 23. 09/04/2013 PageProperty 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!23Tuesday, 9 April 13
  24. 24. 09/04/2013 PageProperty Fields• How?• [CustomPropertyDrawer (typeof (MyType))]• Extend PropertyDrawer• Need a custom height?• Override GetPropertyHeight ()• Do your custom drawing• OnGUI ()24Tuesday, 9 April 13
  25. 25. 09/04/2013 PageUsing 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!25Tuesday, 9 April 13
  26. 26. 09/04/2013 PageUsing 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 :)26Tuesday, 9 April 13
  27. 27. 09/04/2013 PageUsing Sub-Assets• Use SubAssets for complex data structures• Where many elements are ScriptableObjects• Graphs• Databases• ect!27Tuesday, 9 April 13