AlicizaX/Client/Assets/TimerGenericTest.cs

123 lines
3.2 KiB
C#
Raw Normal View History

2026-04-24 20:50:35 +08:00
using AlicizaX;
using AlicizaX.Timer.Runtime;
using UnityEngine;
public sealed class TimerGenericTest : MonoBehaviour
{
private sealed class IntBox
{
public int Value;
}
private sealed class PlayerData
{
public int Id;
public string Name;
public int Score;
}
private sealed class Enemy
{
public string Name;
public int Health;
}
private ITimerService _timerService;
private void Start()
{
_timerService = AppServices.Require<ITimerService>();
Debug.Log("=== Timer Generic Test Started ===");
TestNoArgs();
TestIntArg();
TestStringArg();
TestPlayerDataArg();
TestClassArg();
TestLoopWithArg();
}
private void TestNoArgs()
{
Debug.Log("[Test 1] No args timer (1s)");
_timerService.AddTimer(OnNoArgsCallback, 1f);
}
private static void OnNoArgsCallback()
{
Debug.Log("[Test 1] Callback executed!");
}
private void TestIntArg()
{
Debug.Log("[Test 2] Boxed int reference timer (2s)");
_timerService.AddTimer(OnIntCallback, new IntBox { Value = 42 }, 2f);
}
private static void OnIntCallback(IntBox value)
{
Debug.Log(Utility.Text.Format("[Test 2] Int callback executed! Value: {0}", value.Value));
}
private void TestStringArg()
{
Debug.Log("[Test 3] String arg timer (3s)");
_timerService.AddTimer(OnStringCallback, "Hello Timer!", 3f);
}
private static void OnStringCallback(string message)
{
Debug.Log(Utility.Text.Format("[Test 3] String callback executed! Message: {0}", message));
}
private void TestPlayerDataArg()
{
Debug.Log("[Test 4] PlayerData class arg timer (4s)");
_timerService.AddTimer(OnPlayerDataCallback, new PlayerData
{
Id = 123,
Name = "Alice",
Score = 9999
}, 4f);
}
private static void OnPlayerDataCallback(PlayerData data)
{
Debug.Log(Utility.Text.Format("[Test 4] Player callback executed! Player: {0} (ID: {1}, Score: {2})", data.Name, data.Id, data.Score));
}
private void TestClassArg()
{
Debug.Log("[Test 5] Class arg timer (5s)");
_timerService.AddTimer(OnClassCallback, new Enemy
{
Name = "Goblin",
Health = 100
}, 5f);
}
private static void OnClassCallback(Enemy enemy)
{
Debug.Log(Utility.Text.Format("[Test 5] Class callback executed! Enemy: {0}, Health: {1}", enemy.Name, enemy.Health));
}
private void TestLoopWithArg()
{
Debug.Log("[Test 6] Loop timer with reference arg (0.5s interval)");
IntBox counter = new IntBox { Value = 1 };
int timerId = 0;
timerId = _timerService.AddTimer(count =>
{
Debug.Log(Utility.Text.Format("[Test 6] Loop callback #{0}", count.Value));
if (count.Value >= 3)
{
_timerService.RemoveTimer(timerId);
Debug.Log("[Test 6] Loop timer removed");
return;
}
count.Value++;
}, counter, 0.5f, isLoop: true);
}
}