为了防止在游戏中配置的协议常量、事件常量的值出现重复,我们添加一个测试脚本。
MsgEventTest
在Scripts文件夹内新建Test文件夹。在其中新建ITest
脚本
1 2 3 4
| public interface ITest { void Execute(); }
|
再新建MsgEventTest
脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| using System; using UnityEngine; using System.Collections.Generic; public class MsgEventTest : ITest { public void Execute() { HashSet<int> insHashSet = new(); Type type = typeof(MsgEvent); var fieldInfos = type.GetFields(); foreach (var fieldInfo in fieldInfos) { var value = fieldInfo.GetRawConstantValue(); if (value is int) { if (!insHashSet.Add((int)value)) Debug.LogError("常量字段:" + fieldInfo.Name + "和其他的常量字段数值重复"); } else Debug.LogError("常量字段:" + fieldInfo.Name + "类型错误,必须是int"); } } }
|
FieldInfo.GetRawConstantValue()
获取常量值。
TestMgr
再Scripts——Manager文件夹中新建TestMgr
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| using System.Collections.Generic;
public class TestMgr : NormalSingleton<TestMgr>, IInit { private readonly List<ITest> _editorTests = new() { new MsgEventTest() }; private readonly List<ITest> _realDeviceTests = new() {
}; public void Init() { #if UNITY_EDITOR EditorTest(); #endif RealDeviceTest(); Clear(); } private void EditorTest() { ExecuteAll(_editorTests); } private void RealDeviceTest() { ExecuteAll(_realDeviceTests); } private void ExecuteAll(List<ITest> tests) { foreach (ITest test in tests) { test.Execute(); UnityEngine.Debug.Log(test + "测试完成"); } } private void Clear() { _editorTests.Clear(); _realDeviceTests.Clear(); } }
|
注意这里的UnityEngine.Debug.Log(test + "测试完成");
,虽然test是一个接口,但是显示的是实现此接口的类型。
进入GameRoot
,然后再在其中调用TestMgr
,它作为测试用的脚本,不会添加到LifeCycleAddConfig
当中
1 2 3 4 5
| private void Start() { TestMgr.Instance.Init(); }
|