Attribute Reflection
A newly added feature is the ability to auto register actions in a class with reflection under the hood, which makes the action creation process much easier. Using the RuntimeDebugSystem.RegisterActionsAuto
method, the system will look for all public field, property, method, method with parameters which has a DebugAction
attribute.
Supported type for field, property
- string
- int
- float
- bool
- enum
Supported type for method with parameter
- string
- int
- float
- bool
To register
You can store the action generated somewhere for unregistering.
actions = RuntimeDebugSystem.RegisterActionsAuto(this);
To unregister
RuntimeDebugSystem.UnregisterActions(actions);
Or simply extends you component from RuntimeDebugBehaviour
Which already handles the registering and unregistering for you.
public abstract class RuntimeDebugBehaviour : MonoBehaviour
{
private BaseDebugAction[] actions;
protected virtual void Awake()
{
actions = RuntimeDebugSystem.RegisterActionsAuto(this);
}
protected virtual void OnDestroy()
{
RuntimeDebugSystem.UnregisterActions(actions);
}
}
Note
To prevent any ghost action hanging in the debug menu, it's suggested to handle your actions' unregistration if they were added from any MonoBehaviour
and maybe disabled or destroy in future, e.g.
- Register actions in the
OnAwake
and unregister actions in theOnDestroy
- Register actions in the
OnEnable
and unregister actions in theOnDisable
[Method] Example Button Action
[DebugAction(shortcutKey = "q", closePanelAfterTrigger = true)]
public void SpawnACube()
{
var bdy = GameObject.CreatePrimitive(PrimitiveType.Cube)
.AddComponent<Rigidbody>();
bdy.gameObject.AddComponent<BoxCollider>();
bdy.transform.position = new Vector3(0, 3, UnityEngine.Random.Range(-2, 2));
bdy.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
bdy.AddExplosionForce(2, Vector3.zero, 10);
}
[Method with Parameters] Example Input Action
[DebugAction]
public void LogSum(int a, int b)
{
Debug.Log(a + b);
}
[Property] Example String Input Action
[DebugAction]
public string GameOverText
{
get => gameOverText.text.text;
set => gameOverText.text.SetText(value);
}
[Field] Example Float Input Action
[DebugAction(closePanelAfterTrigger = true)]
public float obstacleVelocity = 2;