Monday, March 19, 2018

A mini debug tool in Unity

There are many ways of creating a tool to debug a game. Console commands on run-time is a common one. In-game rendered menus is another. In Unity I have been using a simple game object that has a script that exposes check-boxes which act as either toggles or buttons (they automatically get unchecked). It's sufficient for my needs. It's like having a handy dandy remote control with a bunch of options to speed up testing.

The inspector of the single debug game object in the Unity scene will look something like this:


And the script is as simple as this:

 using System.Collections;  
 using System.Collections.Generic;  
 using UnityEngine;  
 public class EPDebug : MonoBehaviour {  
      public bool toggle1;  
      public bool toggle2;  
      public bool option1;  
      public bool option2;  
      public bool option3;  
      [HideInInspector] public static EPDebug instance;  
      public void Awake() {  
           instance = this;  
      }  
      public void Start () {  
      }  
      public void Update () {  
           base.Update();  
           if (toggle1) {  
                // do stuff each frame  
                // other game objects can check this variable too  
           }  
           if (toggle2) {  
                // do stuff each frame  
                // other game objects can check this variable too  
           }  
           if (option1) {  
                // do something once  
                option1 = false;  
           }  
           if (option2) {  
                // do something once  
                option2 = false;  
           }  
           if (option3) {  
                // do something once  
                option3 = false;  
           }  
      }  
 }  

I have a toggle that has the game print out verbose information regarding game states and whatever information suits me. I use the button options for creating spontaneous game objects and invoking commands like giving some in-game currency. This debug menu can be extended with number fields to change global variables too.

No comments: