unity

[Unity 3d] uREPL Unity Runtime 解

2019-07-09  本文已影响8人  雨落随风

一个可以在运行时的c#脚本解释器/控制台

GitHub 上的工程多如繁星,有些好的仓库,但凡不经意间错过了就很难找回,故稍作采撷,希望能帮助到有心人。

简介:

笔者今天推荐的仓库叫 uREPL - 交互式解释器。
uREPL is an in-game powerful REPL envinronment for Unity3D that supports following functions:
uREPL 是为 Unity 准备的一个在游戏中跑的强大交互式解释器环境。

功能:

使用:

属性的使用/Attribute

You can add commands by adding a [uREPL.Command] attribute to static methods.
通过在静态方法上添加 [uREPL.Command]属性可以新增一个自定义的命令。

public class CommandTest
{
    //拿到一个游戏对象索引
    static public GameObject gameObject;


    // 这个方法能在不需要提供类名的情况下执行,命令输入如下:
    // $ ShowCurrentSelectedObject() ⏎
    [uREPL.Command]
    static public string ShowCurrentSelectedObject()
    {
        return gameObject.name;
    }

    // 可以为这个方法只定义一个名称,直接通过别名来调用,命令如下:
    // $ selected ⏎
    [uREPL.Command(name = "selected")]
    static public string ShowCurrentSelectedObject2()
    {
        return gameObject.name;
    }


    // 这个将会在你输命令时弹出命令提示/描述
    [uREPL.Command(name = "selected2", description = "show the selected gameobject name.")]
    static public string ShowCurrentSelectedObject3()
    {
        return gameObject.name;
    }
}

带参数的命令

uREPL automatically convert the command format into the actual code.
uREPL自动将命令格式转换为实际代码。

public class CommandTest
{
    // '$ print "\"hoge hoge\"" ⏎'将自动改为:
    // CommandTest.Print("\"hoge hoge\"");
    [uREPL.Command(name = "print")]
    static public void Print(object obj)
    {
        Debug.Log(obj);
    }

    // 支持命令的重写
    [uREPL.Command(name = "print")]
    static public void Print(string value1, int value2, float value3)
    {
        Debug.LogFormat("string: {0}, int: {1}, float: {2}", value1, value2, value3);
    }
}

运行时

From v0.4.0, you can register and unregister commands in runtime.
从 v0.4.0 版本开始,支持在运行时注册和反注册 命令。

using UnityEngine;
public class RuntimeCommandTest : MonoBehaviour
{
    void OnEnable()
    {
        uREPL.RuntimeCommands.Register("func1", () => Debug.Log("hoge"), "output hoge");
        uREPL.RuntimeCommands.Register("func2", x => Debug.Log(x), "outout given argument");
        uREPL.RuntimeCommands.Register("func3", (x, y) => Debug.Log((int)x * (int)y), "multiply arg0 by arg1");
        uREPL.RuntimeCommands.Register("func4", (x, y, z) => Debug.Log(x + " " + y + " " + z), "output all arguments");
        uREPL.RuntimeCommands.Register("func5", (int x) => Debug.Log(x), "output int value");
        uREPL.RuntimeCommands.Register("func6", (string x, float y) => Debug.Log(x + " is " + y), "output arg0 is arg1");
        uREPL.RuntimeCommands.Register("func7", (Vector3 pos, Quaternion rot, Vector3 scale) => Debug.Log(pos + " " + rot + " " + scale), "output unity-type values.");
    }

    void OnDisable()
    {
        uREPL.RuntimeCommands.Unregister("func1");
        uREPL.RuntimeCommands.Unregister("func2");
        uREPL.RuntimeCommands.Unregister("func3");
        uREPL.RuntimeCommands.Unregister("func4");
        uREPL.RuntimeCommands.Unregister("func5");
        uREPL.RuntimeCommands.Unregister("func6");
        uREPL.RuntimeCommands.Unregister("func7");
    }
}

演示:

UREPL
UREPL-inspect

链接:

hecomi/uREPL: In-game powerful REPL environment for Unity3D.

结语:

扩展阅读:

Unity で実行時にコードやコマンドを補完つきで実行できる uREPL を作ってみた - 凹みTips

本文集持续更新ing,喜欢记得点赞关注哦!

上一篇 下一篇

猜你喜欢

热点阅读