The Hack Spectrum: Tips, Tricks, and Hacks for Unity

5,714 views
5,578 views

Published on

As engineers, we often strive for order and predictability. However, as game developers we are ready to battle obstacles in unconventional ways; anything that we can do to make a great game. This session explores the spectrum of unconventional solutions in Unity, from clever tricks to dirty hacks, and covers some of the techniques we have used at Schell Games to overcome otherwise insurmountable challenges. In the realm of clever solutions, this session will touch on polymorphic array serialization, custom inspectors for directories, universal importer settings, editor scripting and other techniques. Closer to the "hack" end of the spectrum, there are things like using reflection to access private variables, stealing parts of the editor UI, and use editor APIs from game code.

Published in: Engineering, Software
2 Comments
18 Likes
Statistics
Notes
No Downloads
Views
Total views
5,714
On SlideShare
0
From Embeds
0
Number of Embeds
670
Actions
Shares
0
Downloads
0
Comments
2
Likes
18
Embeds 0
No embeds

No notes for slide

The Hack Spectrum: Tips, Tricks, and Hacks for Unity

  1. 1. The Hack Spectrum: Tips, Tricks, and Hacks for Unity Ryan Hipple Senior Engineer Schell Games @roboryantron
  2. 2. The World of Lexica ● Transformational game ● Tablets in the classroom ● More than 50 people ● 2 ½ years
  3. 3. The Hack Spectrum: Tips, Tricks, and Hacks for Unity Serialization Assets Runtime Reflection
  4. 4. The Hack Spectrum: Tips, Tricks, and Hacks for Unity Serialization Assets Runtime Reflection
  5. 5. Serialization Polymorphic Serialization ● Polymorphism is awesome ● Unity does not serialize it well (fields or arrays) Name Name Name Name Intelligence Ferocity Deliciousness
  6. 6. Serialization Polymorphic Serialization ● ScriptableObject instead of [Serializable] ● One problem : lots of files
  7. 7. Serialization Polymorphic Serialization ● Want to make it one file... ● AssetDatabase.AddObjectToAsset
  8. 8. Serialization Polymorphic Serialization ● Want to make it seem like a normal file? ● hideFlags = HideFlags.HideInHierarchy
  9. 9. Serialization Polymorphic Serialization ● Streamline with custom inspectors ● CreateInstance on Add ● Destroy on Remove ● Dropdown with available types
  10. 10. Serialization Polymorphic Serialization Demo
  11. 11. Serialization Serializable Long ● 64 bit integer ● Great for IDs ● Used by other software ● Unity does not serialize the type ‘long’
  12. 12. Serialization Serializable Long ● Break into two ints for serialization ● Custom Inspector ● Implicit casting
  13. 13. The Hack Spectrum: Tips, Tricks, and Hacks for Unity Serialization Assets Runtime Reflection
  14. 14. Assets Auto Scriptable Objects ● Not like assigning MonoBehaviours ● Can only create ScriptableObjects from code
  15. 15. Assets Auto Scriptable Objects ● Custom editor for MonoScript (inspector) ● Check for ScriptableObject inheritance ● Button to CreateInstance
  16. 16. Assets Auto Scriptable Objects Demo
  17. 17. Assets Default Object Inspector ● No inspectors for directories ● No inspectors for scenes ● No inspectors for unknown extensions ● No inspectors for asset roots
  18. 18. Assets Default Object Inspector ● Create Custom Editor for “Object” ● Draw differently for each type ○ If statements? ● Better Option - Copy Unity’s Inspector pattern
  19. 19. Assets Default Object Inspector ● Custom Editor for Object ● OnEnable, finds all “ObjectInspector” types ○ Assembly.GetTypes ● Any ObjectInspector where “IsValid” returns true is added to a list ● Draw Valid Inspectors in OnInspectorGUI
  20. 20. Assets Default Object Inspector ● Simple to Create ● Extend ObjectInspector ● Override IsValid ○ example: return extension == (".unity"); ● Override OnInspectorGUI
  21. 21. Assets Default Object Inspector Demo scene folder and file encoding
  22. 22. Assets Reusing Importer Settings ● Often want same importer settings ● Per project ● Per directory ● Should be automatic
  23. 23. Assets Reusing Importer Settings ● Two-Part Solution ○ AssetPostprocessor script ○ Postprocessor settings scriptable objects ● Each directory has a settings object ● Override options for each importer option ● Applies to new assets automatically
  24. 24. Assets Reusing Importer Settings Demo
  25. 25. Assets Custom Metadata ● Data about a file - not in the file ● Loading full file takes long ● New objects can get deleted, renamed ● Unity stores in meta file ● You can too!
  26. 26. Assets Custom Metadata ● AssetImporter.userData - string ● Save key value pairs or json ● Use AssetModificationProcessor to update ● Display in inspector
  27. 27. Assets Custom Metadata Demo
  28. 28. The Hack Spectrum: Tips, Tricks, and Hacks for Unity Serialization Assets Runtime Reflection
  29. 29. Runtime References, Resources, and Memory ● Useful to load a list of other assets ● Object references in prefabs or SOs? ○ Loads all assets at once ● Paths for Resources.Load? ○ Loads only when needed ○ Typo prone
  30. 30. Runtime References, Resources, and Memory ● Asset Path Attribute for string fields ● Property Drawer for the attribute ○ Draw as an object field ○ Save path to string
  31. 31. Runtime Runtime Level Listing ● At runtime, don’t know what scenes exist ● EditorBuildSettings has a list ● Lets steal it
  32. 32. Runtime Runtime Level Listing ● AssetModificationProcessor for EditorBuildSettings.asset ● Copy EditorBuildSettings.scenes to a ScriptableObject in Resources ● Add any custom data in the list
  33. 33. Runtime Runtime Level Listing Demo
  34. 34. The Hack Spectrum: Tips, Tricks, and Hacks for Unity Serialization Assets Runtime Reflection
  35. 35. Reflection Reflection Primer ● Information about all Assemblies and Types ● Get a list of all types ● List all functions in a type - Public or Private! ● Call functions, set fields
  36. 36. Reflection Developer Debug Options ● API Design - If it is public, they will call it ● Attach to your API with reflection ● Debugging ● Testing
  37. 37. Reflection Call Editor Code from Game ● If building DLLs, can’t use UnityEditor in game code ● SelectGameObjectInHierarchy ● Call your own editor code ● EDITOR ONLY!
  38. 38. Reflection Call Editor Code from Game
  39. 39. Reflection Call Editor Code from Game Demo calling editor code from runtime
  40. 40. Reflection Use Private APIs ● Unity exposes a lot out of the box ● Much more to explore ● Browse with ILSpy or Reflector
  41. 41. Reflection Use Private APIs ● Open any editor window; issue commands ● Take control of the window layout ● “Borrow” editor assets ● Tons of private calls waiting to be explored
  42. 42. Reflection Use Private APIs
  43. 43. Reflection Use Private APIs
  44. 44. Reflection Use Private APIs
  45. 45. Reflection Use Private APIs Demo window layout
  46. 46. The Hack Spectrum: Tips, Tricks, and Hacks for Unity Ryan Hipple Senior Engineer Schell Games @roboryantron

×