This document discusses various tips and tricks for hacking Unity through serialization, assets, runtime, and reflection. It provides code examples and demos for serializing polymorphic types by creating custom ScriptableObjects, creating default inspectors for any object type, reusing importer settings across assets, accessing private editor APIs at runtime, and more advanced techniques for optimizing games developed in Unity.
The Hack Spectrum: Tips, Tricks, and Hacks for Unity
1. The Hack Spectrum:
Tips, Tricks, and Hacks for Unity
Ryan Hipple
Senior Engineer
Schell Games
@roboryantron
2. The World of Lexica
● Transformational game
● Tablets in the classroom
● More than 50 people
● 2 ½ years
3. The Hack Spectrum:
Tips, Tricks, and Hacks for Unity
Serialization
Assets
Runtime
Reflection
4. The Hack Spectrum:
Tips, Tricks, and Hacks for Unity
Serialization
Assets
Runtime
Reflection
5. Serialization
Polymorphic Serialization
● Polymorphism is awesome
● Unity does not serialize it well (fields or arrays)
Name
Name Name Name
Intelligence Ferocity Deliciousness
18. Assets
Default Object Inspector
● No inspectors for directories
● No inspectors for scenes
● No inspectors for unknown extensions
● No inspectors for asset roots
19. Assets
Default Object Inspector
● Create Custom Editor for “Object”
● Draw differently for each type
○ If statements?
● Better Option - Copy Unity’s Inspector
pattern
20. 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
26. 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!
27. Assets
Custom Metadata
● AssetImporter.userData - string
● Save key value pairs or json
● Use AssetModificationProcessor to update
● Display in inspector
29. The Hack Spectrum:
Tips, Tricks, and Hacks for Unity
Serialization
Assets
Runtime
Reflection
30. 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
31. 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
32. Runtime
Runtime Level Listing
● At runtime, don’t know what scenes exist
● EditorBuildSettings has a list
● Lets steal it
33. Runtime
Runtime Level Listing
● AssetModificationProcessor for
EditorBuildSettings.asset
● Copy EditorBuildSettings.scenes to a
ScriptableObject in Resources
● Add any custom data in the list
35. The Hack Spectrum:
Tips, Tricks, and Hacks for Unity
Serialization
Assets
Runtime
Reflection
36. 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
37. Reflection
Developer Debug Options
● API Design - If it is public, they will call it
● Attach to your API with reflection
● Debugging
● Testing
38. Reflection
Call Editor Code from Game
● If building DLLs, can’t use UnityEditor in
game code
● SelectGameObjectInHierarchy
● Call your own editor code
● EDITOR ONLY!
41. Reflection
Use Private APIs
● Unity exposes a lot out of the box
● Much more to explore
● Browse with ILSpy or Reflector
42. 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