Giter Site home page Giter Site logo

ichenpipi / unity-code-executor Goto Github PK

View Code? Open in Web Editor NEW
30.0 2.0 3.0 536 KB

[Unity Editor Tool] A front-end interface where you can create, edit, save and dynamically execute code snippets.

License: MIT License

C# 100.00%
csharp executor lua snippets unity unity-editor xlua dynamic-execution debug game-development

unity-code-executor's Introduction

Code Executor

中文文档

Introduction

What is this project

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.

Intro

What can it do

  1. Dynamically execute code snippets in Edit Mode.
  2. Dynamically execute code snippets in Play Mode.

Supported programming languages

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:

The built-in execution modes are enabled by default, you can disable them in the window's menu.

Built-in Execution Modes

How to register execution modes

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 the InitializeOnLoadMethod 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.

Execution Modes

Register by reflection

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.

Installation

Install from Package Manager (Recommend)

  1. Open the Package Manager Window.
  2. Open the add(+) menu in the Package Manager's toolbar.
  3. Select Add package from git URL from the add menu.
  4. 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

Manually install

Download and put the Whole Project in your project's Assets folder (or any subfolder would be ok).

Usage

Open the window

Select the Window > Code Executor option in the Unity editor menu bar.

Create Snippet

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.

New

Execute Snippet

  1. Click the Execute button below the code editing area to execute the current code snippet
  2. Move your mouse to any item in the snippet list, click the ▶️ button on the right side of the item to execute the snippet

Execute

Edit 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.

Edit

Rename Snippet

Select any item in the snippet list, press the F2 key to rename the snippet.

Rename

Duplicate 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.

Duplicate

Snippet Menu

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

Snippet Menu

Category Menu

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

Category Menu

Context Menu

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

Context Menu

Copy/Paste Snippets

  • 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.

Reference Snippets

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!

Shortcut keys

  • Ctrl+F: Focus to search field
  • F2: Rename the first selected asset
  • F5: Reload data and settings
  • Delete/Backspace: Delete selected snippets
  • Ctrl+C: save the selected snippets in the snippet list to the system clipboard as Json format text
  • Ctrl+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.

Screenshots/Animations

To be added...

Compatibility

This project is compatible with the following versions of the Unity Editor:

  • 2020.2 and later

Testing details

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 ✔️

Dependencies

Package Version Note
None None

License

This project is licensed under the MIT license.

unity-code-executor's People

Contributors

ichenpipi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

unity-code-executor's Issues

还未处理需要引入命名空间的情况

比如下面这个代码,需要用到 System.Diagnostics 命名空间

                string resPath = "";
                StackFrame[] stackFrames = new StackTrace(true).GetFrames();
                if (stackFrames != null && stackFrames.Length > 0)
                {
                    string scriptPath = stackFrames[0].GetFileName()?.Replace("\\", "/");
                    if (!string.IsNullOrEmpty(scriptPath))
                    {
                        int projectPathLength = Application.dataPath.Length - "Assets".Length;
                        int lastSlashIndex = scriptPath.LastIndexOf("/");
                        resPath = scriptPath.Substring(projectPathLength, lastSlashIndex - projectPathLength) + "/Res/";
                        Debug.Log($"res path = {resPath}");
                    }
                }

否则就会报错了:

The type or namespace name `StackFrame' could not be found. Are you missing `System.Diagnostics' using directive?
UnityEngine.Debug:LogError (object)
ChenPipi.CodeExecutor.Example.InjectHelperCSharp:CompileCode (string) (at E:/Unity/Github/unity-code-executor/Editor/Scripts/Examples/InjectHelperCSharp.cs:198)
以下省略...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.