yasirkula / UnityIngameDebugConsole

A uGUI based console to see debug messages and execute commands during gameplay in Unity
MIT License
2.11k stars 221 forks source link

Improve autocomplete #22

Open bobroberts177 opened 4 years ago

bobroberts177 commented 4 years ago

Here's an example for testing out these features:

using System;
using System.Collections.Generic;
using UnityEngine;
using IngameDebugConsole;

public class ConsoleTest : MonoBehaviour {
  [ConsoleMethod("cube", "Creates a cube at specified position")]
  public static void CreateCubeAt(Vector3 position) {
    GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = position;
  }

  [ConsoleMethod("cub3", "Creates a cube at specified position")]
  public static void CreateCub3At(Vector3 position) {
    GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = position;
  }

  [ConsoleMethod("destroy", "Destroy a GameObject")]
  public static void DestroyGameObject(GameObject go) {
    GameObject.Destroy(go);
  }

  [ConsoleTypeParse(typeof(PrimitiveType))]
  public static bool ParsePrimitiveType(string input, out object output) {
    foreach(PrimitiveType primitiveType in (PrimitiveType[])Enum.GetValues(typeof(PrimitiveType))) {
      if (input == primitiveType.ToString()) {
        output = primitiveType;
        return true;
      }
    }
    output = null;
    return false;
  }

  [ConsoleTypeSuggest(typeof(PrimitiveType))]
  public static bool SuggestPrimitiveType(string input, out List<string> suggestions) {
    suggestions = new List<string>();
    foreach(PrimitiveType primitiveType in (PrimitiveType[])Enum.GetValues(typeof(PrimitiveType))) {
      string primitiveString = primitiveType.ToString();
      if (primitiveString.StartsWith(input))
        suggestions.Add(primitiveType.ToString());
    }
    return true;
  }

  [ConsoleMethod("create_primitive", "Create a primitive")]
  public static void CreateEntity(PrimitiveType primitiveType, Vector3 position) {
    GameObject.CreatePrimitive(primitiveType).transform.position = position;
  }
}
yasirkula commented 4 years ago

Thank you for the PR! In the future, I may change the autocomplete behaviour as you suggested, so I'll keep this PR open. But I'm not planning to change it now.