-
Notifications
You must be signed in to change notification settings - Fork 0
Script Templates
This plugin makes it easier to create new script templates for custom classes.
Unlike the default Editor behavior that makes custom script templates accessible on the Editor level, this plugin is project based.
The script processing is entirely modular and customizable:
- Individual methods to process only a single tag, a logical group of tags, or anything else you need about the file.
- It's possible to inject your own behavior that will run before the default ones, or remove/replace the defaults.
- Every step can be replaced, even the file creation process itself.
- Create your own template in a ScriptName.cs.txt file.
- Implement a
ScriptBuilderBaseclass, add a function and attach a context menu button to it. - Fill the created script's Inspector fields for the template file and, if necessary, icon.
- In the function, create an object instance that implements
INewAssetDatathen fill it with the necessary parameters. - Either let only the default processors work, add your own extra processors, or overwrite everything with your own processors.
- Feed the object to
DoCreateNewScriptAsset.CreateFromTemplate().
Contains all the code that mimics how Unity creates its own Monobehaviour script and a few extra functions for common use cases. The class contains:
- Constants for the tags that the processors will be searching for.
- Static functions that receive the script data and are used to process it.
- The function
ExecuteAll()that contains the default execution order for the processors.
This class contains all the data for the new script and all functions to run the processors.
This class' instance is the only parameter for every processor function, since all changes to the script must be made through its properties.
Interface with the required data that DoCreateNewScriptAsset will use to kickstart the asset creation process.
This interface was created in case the user requires a more advanced approach than what NewScriptData delivers.
Ex: Custom file naming logic and default icons.
Implementation of INewAssetData specific for scripts. Uses a simple logic to validate the template file, generate the default script name and add a default .cs icon.
The base class to create context menu scripts, like the ones in the samples.
⚠ It is highly recommended that you keep the sample files as a base.
This class will create a new ScriptBuilder script, so you can register a new context menu for your custom script template.
This class is under the namespace CorePackage.ScriptTemplates instead of CorePackage.ScriptTemplates.Builders, like the others.
This is very important, as the first sub-menu item to be registered will also use the same priority number for the parent menu.
Windows, C#, .NET and Unity don't give any clear guarantees for what item is registered first in the menus. But one workaround is that parent namespaces are evaluated before their children. In conclusion, this script is the one that places the parent menu "Assets/Create Script" in the top position. If you delete this file, make sure you add another one using the same namespace, or the menu may change its position.
These classes will use a selected class in the File Explorer as a parameter.
This can be used to create auxiliary classes, like Editor and Property Drawer.