-
Notifications
You must be signed in to change notification settings - Fork 14
Guide 401: Generating Custom SmartTypes
SmartData comes with various built-in SmartTypes, for basic types such as float, int and string. Creating SmartObject assets of these types is performed through the Create menu, and the SmartRef classes are used to reference them in code.
In most projects, you'll find yourself needing lots of other SmartTypes, whether for your own classes/structs, Unity types or those from other plugins.
Due to Unity's serialization limitations, it's necessary to create a new set of classes for each type you want to support. SmartData makes this simple with the SmartType generator tool. To launch it, right-click in the Project panel or Assets on the top menu, then Create > SmartData > Generate Types.
Start typing in the first Data Types field to bring up the autocomplete. This finds all valid types that match what you're typing. Note that this is case-sensitive. Click Select to fill the field. This will include the full namespace.
To generate multiple SmartTypes at once, click the green + to add more type fields. Click the red - to remove any you don't want.
This does not create a single SmartType with multiple type arguments - it creates multiple SmartTypes with a single type argument. SmartData only supports one type argument per SmartType.
The Path field denotes where the new scripts will be created. By default this is the location of the Project panel when you launched the tool.
By default, the tool will also put all created files in a subfolder named after the underlying type. There are a lot of files, so this is recommended to keep things organised.
If you're overwriting existing files, you MUST select Overwrite. Otherwise the tool will ignore any files it would have to overwrite, and display a warning.
Click Create to generate all the relevant files. The console will output various information about what files are created and any that are being overwritten.
This process will also automatically edit the .meta files of the created scripts to assign them the appropriate icons.
This may not work all the time right now. If there's an error in the console and the icons don't appear, use the tool's regenerate mode and it'll clear it up.
After recompiling, the new SmartType will now automatically appear in the Create > SmartData menu.
The tool creates new namespaces for all created SmartTypes, which must be used to reference the new types in code.
SmartData strips the namespace from the underlying type to generate the SmartType's name. This improves readability, but can cause conflicts when creating SmartTypes for identically-named types from different namespaces. This is not currently supported without workarounds.
Various types are not suitable to be used as SmartTypes. For example, UnityEditor types cannot be used at runtime. While you can manually input the names of these types into the generated (and it won't try to stop you), autocomplete won't help you out with any types it knows are unsuitable.
This is determined by strings in Advanced > Type Helper Exclude Patterns. When the tool enumerates available types, it ignores any whose names/namespaces contain the included strings.
The tool uses "{namespace}.{typename}".Contains(pattern) for pattern matching. This is case-sensitive and doesn't use any regex functionality etc.
You can remove or add to these patterns by modifying the array in the tool. You may need to do this, for example, for custom types you've created for editor functionality, or editor-only types from other plugins.