Code Monkey home page Code Monkey logo

saltbox.terminal's Introduction

Saltbox.Terminal

openupm

A CommandLine Terminal for Unity Games

terminal

Installing from OpenUPM

You can easily install this package using OpenUPM CLI

openupm add com.saltboxgames.saltbox.terminal

Note: this method requires Node 12 and you can install OpenUPM using

npm install -g openupm-cli

Usage

Assets and scripts are available under the saltbox.terminal namespace.

The default Terminal is available under the Window dropdown. The first time you open the terminal it will create two scriptable objects Commands and TerminalSettings in your project root.

TerminalSettings

TerminalSettings is used to store information about the terminal editor window. it can't be moved, it's only used by the editor so it won't be compiled into your game.

CommandRunner

Commands is and instance of CommandRunner used to handle any terminal controls and commands you wish to write. It will be regenerated if the runner field in TerminalSettings is null.

Creating New Commands

Adding a new command can be done by implementing the ICommand interface. and adding it to the command runner. Option attributes will automatically be populated during execution.

    //Ex; clear --all
    class ClearCommand : ICommand
    {
        [Option('a', "all", HelpText="Clear All Terminals")]
        public bool clearAll { get; set; }

        public bool Invoke(ITerminalControl sender, CommandRunner runner)
        {
            if (clearAll)
            {
                runner.Clear();
                return true;
            }
            sender.Clear();
            return true;
        }
    }
    runner.AddCommand('clear', new ClearCommand());

Alternatively any commands that require Unity Assets can be created by inheriting from ScriptableCommand.

    //Ex; spawn-block -c 10
    [CreateAssetMenu(fileName = "Spawner", menuName = "Terminal/Commands/Spawner")]
    public class SpawnPrefab : ScriptableCommand
    {
        [SerializeField]
        private GameObject prefab;

        [Option('c', "count", Required = true, HelpText = "number of prefabs to spawn")]
        public int count { get; set; }

        public override string Name => "spawn-" + prefab.Name;

        public override bool Invoke(ITerminalControl sender, CommandRunner runner)
        {
            for (int i = 0; i < count; i++)
            {
                GameObject.Instantiate(prefab);
            }
            return true;
        }
    }

you can then add an instance of your command to either the Runtime or Editor Commands list in your command runner.

command-runner

Creating New Terminal

New Terminal windows can be created by implementing the ITerminalControl interface and adding a reference to your CommandRunner

    public class InGameTerminal : MonoBehaviour, ITerminalControl
    {
        public event TerminalEvent OnInputSubmit;

        public string Name => "Game";

        [SerializeField]
        private CommandRunner runner;

        [SerializeField]
        private TextMeshPro text;

        private void OnEnable()
        {
            runner.AddControl(this);
        }

        private void OnDisable()
        {
            runner.RemoveControl(this);
        }

        public void Clear()
        {
            text.text = "";
        }

        public void Write(string content, Color color)
        {
            string hex = ColorUtility.ToHtmlStringRGB(color);
            text.text += $"<{hex}>" + content + "</color>";
        }

        public void WriteLine(string content, Color color)
        {
            text.text += "\n";
            Write(content, color);
        }

        public void Submit(string content)
        {
            OnInputSubmit?.Invoke(this, new TerminalEventArgs()
            {
                content = content
            });
        }
    }

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.