Skip to content

Commit 5f27d36

Browse files
authored
Initial Commit
1 parent 77802e2 commit 5f27d36

16 files changed

+733
-0
lines changed

Commander/Runtime.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using UnityEngine;
5+
6+
namespace Commander
7+
{
8+
public class CommandManager : MonoBehaviour
9+
{
10+
[SerializeField] private bool _showDebugLogs = true;
11+
12+
#region Lifecycle
13+
14+
private void Awake()
15+
{
16+
CommandRegistry.ShowDebugLogs = _showDebugLogs;
17+
CommandRegistry.ClearCommands();
18+
RegisterAllSceneCommands();
19+
}
20+
21+
private void OnDestroy()
22+
{
23+
CommandRegistry.ClearCommands();
24+
}
25+
26+
private void OnApplicationQuit()
27+
{
28+
CommandRegistry.ClearCommands();
29+
}
30+
31+
#endregion
32+
33+
private void RegisterAllSceneCommands()
34+
{
35+
// Find all types deriving MonoBehaviour that have at least one [ConsoleCommand]
36+
var commandTypes = AppDomain.CurrentDomain.GetAssemblies()
37+
.SelectMany(a => a.GetTypes())
38+
.Where(t => typeof(MonoBehaviour).IsAssignableFrom(t))
39+
.Where(t => t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
40+
.Any(mi => mi.GetCustomAttribute<Command>() != null))
41+
.ToList();
42+
43+
// For each such type, grab all live instances in scene
44+
foreach (var type in commandTypes)
45+
{
46+
// Use reflection to call FindObjectsByType<T>(FindObjectsSortMode) with the correct type argument
47+
var findObjectsMethod = typeof(UnityEngine.Object)
48+
.GetMethods(BindingFlags.Public | BindingFlags.Static)
49+
.FirstOrDefault(m => m.Name == "FindObjectsByType" && m.IsGenericMethod && m.GetParameters().Length == 1);
50+
51+
if (findObjectsMethod != null)
52+
{
53+
// Create a generic method with the current type
54+
var genericMethod = findObjectsMethod.MakeGenericMethod(type);
55+
56+
// Use FindObjectsSortMode.None to match default behavior
57+
var objects = genericMethod.Invoke(null, new object[] { FindObjectsSortMode.None }) as UnityEngine.Object[];
58+
foreach (var inst in objects.Cast<MonoBehaviour>())
59+
{
60+
CommandRegistry.RegisterCommand(inst);
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}

Commander/Runtime/CommandManager.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Commander/Runtime/CommandTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using UnityEngine;
2+
3+
public class CommandTest : MonoBehaviour
4+
{
5+
[Command("TestCommand")]
6+
private void TestCommandMethod(int a, string b)
7+
{
8+
Debug.Log($"Test command executed with parameters: a={a}, b={b}");
9+
}
10+
}

Commander/Runtime/CommandTest.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Commander/Runtime/Core.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Commander/Runtime/Core/Command.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/// <summary>
2+
/// Attribute to mark public and private methods as commands that can be executed.
3+
/// This attribute can be used to define commands with a specific name and description.
4+
/// </summary>
5+
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = false)]
6+
public class Command : System.Attribute
7+
{
8+
#region Properties
9+
10+
/// <summary>
11+
/// Name of the command to call from console
12+
/// </summary>
13+
public string Name { get; private set; }
14+
15+
/// <summary>
16+
/// Description of the commands action
17+
/// </summary>
18+
public string Description { get; private set; }
19+
20+
#endregion
21+
22+
#region Constructors
23+
24+
/// <summary>
25+
/// Empty command with default values - command name will be the methods name
26+
/// </summary>
27+
public Command()
28+
{
29+
Name = null;
30+
Description = null;
31+
}
32+
33+
/// <summary>
34+
/// Define a command with a name and empty description.
35+
/// </summary>
36+
/// <param name="name">Name of the command when calling</param>
37+
public Command(string name)
38+
{
39+
Name = name;
40+
Description = null;
41+
}
42+
43+
/// <summary>
44+
/// Define a command with a name and description.
45+
/// </summary>
46+
/// <param name="name">Name of the command when calling</param>
47+
/// <param name="description">Description of the command for help</param>
48+
public Command(string name, string description)
49+
{
50+
Name = name;
51+
Description = description;
52+
}
53+
54+
#endregion
55+
}

Commander/Runtime/Core/Command.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using UnityEngine;
2+
3+
public class CommandExecuter : MonoBehaviour
4+
{
5+
// Start is called once before the first execution of Update after the MonoBehaviour is created
6+
void Start()
7+
{
8+
9+
}
10+
11+
// Update is called once per frame
12+
void Update()
13+
{
14+
15+
}
16+
}

Commander/Runtime/Core/CommandExecuter.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)