项目作者: Elringus

项目描述 :
Development console for Unity game engine
高级语言: C#
项目地址: git://github.com/Elringus/UnityConsole.git
创建时间: 2018-08-06T16:30:14Z
项目社区:https://github.com/Elringus/UnityConsole

开源协议:MIT License

下载


Description

Allows executing static C# methods via an on-demand console IMGUI.

Installation

Use UPM to install the package via the following Git URL: https://github.com/elringus/unity-console.git#package.

Alternatively, manually copy Assets/UnityConsole folder from the repository to the Unity project.

Minimum supported Unity version: 2019.4.

How to Use

Register commands by adding ConsoleCommand attribute to static C# methods. The attribute has an optional string argument, allowing to assign an alias (shortcut).

  1. [UnityConsole.ConsoleCommand("hello")]
  2. public static void PrintHelloWorld () => Debug.Log("Hello World!");
  3. [UnityConsole.ConsoleCommand]
  4. public static void Add (int arg1, int arg2) => Debug.Log(arg1 + arg2);

Enable the console at runtime with:

  1. UnityConsole.ConsoleGUI.Initialize()

Toggle console GUI with ~ key. The key can be overridden via ConsoleGUI.ToggleKey public static property. It’s also possible to toggle console with a multi-(3 or more) touch on touch screen devices.

In the console, type either method name or alias of a registered command and press Enter key to invoke the method. Method arguments are separated with a single whitespace. To specify string arguments with whitespace, wrap them in double or single quotes.

Use Up and Down to navigate over previously executed commands.

To disable the console at runtime:

  1. UnityConsole.ConsoleGUI.Destroy()

Preprocessors

It’s possible to inject delegates to modify the console input before it’s send for execution, eg:

  1. using UnityConsole;
  2. using UnityEngine;
  3. public class TestPreprocessor : MonoBehaviour
  4. {
  5. private void OnEnable ()
  6. {
  7. InputPreprocessor.AddPreprocessor(PreprocessInput);
  8. }
  9. private void OnDisable ()
  10. {
  11. InputPreprocessor.RemovePreprocessor(PreprocessInput);
  12. }
  13. private string PreprocessInput (string input)
  14. {
  15. if (input != null && input.StartsWith("@"))
  16. {
  17. Debug.Log(input);
  18. return null;
  19. }
  20. return input;
  21. }
  22. }

— will intercept commands starting with @ and instead log the input. Notice, that when a preprocessor delegate returns null it will stop the default console behaviour.