This is a Unity editor plugin project.
This plugin provides a front-end interface where you can create, edit, save, select execution mode and execute code snippets.
- Dynamically execute code snippets in Edit Mode.
- Dynamically execute code snippets in Play Mode.
Although the plugin has built-in programming language backend examples for C# and Lua (xLua).
But the plugin does not impose any restrictions on the programming language backend!
You can dynamically register (inject) execution modes for any programming language through the API provided by the plugin.
Built-in execution mode examples:
- C#: InjectorCSharp.cs, ExecutionHelperCSharp.cs
- xLua (Standalone): InjectorXLua.cs, ExecutionHelperXLua.cs
- xLua (Custom): InjectorXLuaCustom.cs
The built-in execution modes are enabled by default, you can disable them in the window's menu.
The project provides a CodeExecutorRegistration
attribute to register execution modes, which has the ability to control the order of registration.
This attribute essentially uses the
InitializeOnLoadMethod
attribute provided by Unity, so you can also register execution modes directly using theInitializeOnLoadMethod
attribute, but there is no control over the order of multiple modes.
#if UNITY_EDITOR
using ChenPipi.CodeExecutor.Editor;
using UnityEditor;
public static class Example
{
[CodeExecutorRegistration]
private static void Register()
{
CodeExecutorManager.RegisterExecMode(new ExecutionMode()
{
name = "New ExecMode",
executor = ExecuteCode,
});
}
private static object[] ExecuteCode(string code)
{
// Execute code here
return new object[] { };
}
}
#endif
After successful registration, the name of the mode will appear in the drop-down list of the Execution Mode menu at the top right of the Code Executor window, and can be used to execute code snippets.
If you want to avoid directly referencing the ChenPipi.CodeExecutor.Editor
namespace in your own project code, you can implement the registration through C# reflection.
Please refer to ReflectionAPI.cs provided within the project.
- Open the Package Manager Window.
- Open the add(+) menu in the Package Manager's toolbar.
- Select Add package from git URL from the add menu.
- Enter the Git URL of this project (with
.git
suffix) in the text box and click Add button.
Learn more on https://docs.unity3d.com/Manual/upm-ui-giturl.html
Download and put the Whole Project in your project's Assets folder (or any subfolder would be ok).
Select the Window > Code Executor
option in the Unity editor menu bar.
Clicking on the New item at the top left of the window switches you to the New state, where you can edit and execute a temporary snippet.
In addition, you can save the current content as a new snippet and save it in the snippet list by clicking the Save button at the top right of the code editing area.
- Click the Execute button below the code editing area to execute the current code snippet
- Move your mouse to any item in the snippet list, click the
▶️ button on the right side of the item to execute the snippet
After selecting any item in the snippet list, the code editing area will enter the read-only state, then you can view or copy the code text.
If you need to modify the code of current snippet, click on the Edit button at the top right of the code editing area.
Switching to another snippet will enter the read-only state again.
Select any item in the snippet list, press the F2 key to rename the snippet.
If you want to duplicate the current snippet, click the Duplicate button at the top right of the code editing area.
Then you will get a copy of the current snippet.
Right-click on any snippet item in the snippet list will display a menu with the following options:
- Execute
- Edit
- Rename
- Duplicate
- Top
- Un-top
- Delete
Right-click on any category item in the snippet list will display a menu with the following options:
- Rename
- Delete
- Create New Category
- Copy To Clipboard
Right-click on an empty space in the snippet list will display a menu with the following options:
- Create New Category
- Collapse All (Categories)
- Expand All (Categories)
- Paste From Clipboard
- Press Ctrl+C to save the selected snippets in the snippet list to the system clipboard as Json format text.
- Press Ctrl+V, the program will try to parse the content from the system clipboard, and all valid snippets will be saved to the snippet list.
By this way, you can quickly swap snippets between different Unity editors.
To make it easier to reuse existing code snippets, the code editor supports a simple import syntax.
Simply add the @import("SnippetName")
statement to the code, and the plugin will replace the corresponding import statement with the code text of the target snippet before executing the code.
Here is a example~
Currently, we have a snippet called "CrazyThursday" with the following code text:
UnityEngine.Debug.LogError("[CodeExecutor] Crazy Thursday");
Then we import the "CrazyThursday" snippet in other code using the import syntax:
@import("CrazyThursday")
UnityEngine.Debug.LogError("[CodeExecutor] V Me 50");
The code will be parsed as:
UnityEngine.Debug.LogError("[CodeExecutor] Crazy Thursday");
UnityEngine.Debug.LogError("[CodeExecutor] V Me 50");
The import syntax also supports nesting.
But! Be careful! Circular references will destroy the world!
Ctrl+F
: Focus to search fieldF2
: Rename the first selected assetF5
: Reload data and settingsDelete/Backspace
: Delete selected snippetsCtrl+C
: save the selected snippets in the snippet list to the system clipboard as Json format textCtrl+V
: Try parsing the content from the system clipboard, and all valid snippets will be saved to the snippet list.Ctrl+D
: Duplicate selected snippets.
To be added...
This project is compatible with the following versions of the Unity Editor:
- 2020.2 and later
Unity Version | Tested | Note |
---|---|---|
Unity 2020.2.5f1 | ✔️ | |
Unity 2021.2.16f1 | ✔️ | |
Unity 2021.3.8f1 | ✔️ | |
Unity 2021.3.15f1 | ✔️ | |
Unity 2021.3.22f1 | ✔️ | |
Unity 2021.3.27f1 | ✔️ | |
Unity 2021.3.29f1 | ✔️ |
Package | Version | Note |
---|---|---|
None | None |
This project is licensed under the MIT license.