一、事件源生成器
开启自动生成(选择该选项)
关闭自动生成
手动生成
添加事件
using System.Collections;
using System.Collections.Generic;
using TEngine;
using UnityEngine;
namespace GameLogic
{
[EventInterface(EEventGroup.Test)]
public interface IEventTest
{
public void Test1(bool isReconnect);
public void Test2();
}
}
测试
using TEngine;
using UnityEngine;
namespace GameLogic
{
public class Test : MonoBehaviour
{
private void Start()
{
//注册事件
GameEvent.AddEventListener<bool>(IEventTest_Event.Test1, Test1);
GameEvent.AddEventListener(IEventTest_Event.Test2, Test2);
}
private void OnDestroy()
{
//移除事件
GameEvent.RemoveEventListener<bool>(IEventTest_Event.Test1, Test1);
GameEvent.RemoveEventListener(IEventTest_Event.Test2, Test2);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))//按下按键a
{
//触发事件
GameEvent.Send(IEventTest_Event.Test1,false);
}
if (Input.GetKeyDown(KeyCode.B))//按下按键b
{
//触发事件
GameEvent.Send(IEventTest_Event.Test2);
}
}
private void Test1(bool obj)
{
Debug.Log("事件1触发" + obj);
}
private void Test2()
{
Debug.Log("事件2触发");
}
}
}
二、SourceGenerator(未来版)
现在生成的代码不会直接出现在项目里,干净整洁一点。 ——六阿哥
文献资料
https://github.com/merpheus-dev/GetComponentAttribute
https://zhuanlan.zhihu.com/p/677977715
https://docs.unity3d.org.cn/6000.0/Documentation/Manual/create-source-generator.html
1、手动更新
截止到20241120,目前没有正式版
dll设置
dll打上标签RoslynAnalyzer
2、使用记录
添加事件
using TEngine;
[EventInterface(EEventGroup.GroupUI)]
public interface ILoginUI
{
public void OnRoleLogin(bool isReconnect);
public void OnRoleLoginOut();
public void OnOpen(string data);
public void OnClose(string data,bool isReconnect);
}
测试
using TEngine;
using UnityEngine;
namespace GameLogic
{
public class Test:MonoBehaviour
{
private void Start()
{
//注册事件
GameEvent.AddEventListener<bool>(ILoginUI_Event.OnRoleLogin, OnRoleLogin);
GameEvent.AddEventListener<string, bool>(ILoginUI_Event.OnClose, OnClose);
}
private void OnDestroy()
{
//移除事件
GameEvent.RemoveEventListener<string, bool>(ILoginUI_Event.OnClose, OnClose);
GameEvent.RemoveEventListener<bool>(ILoginUI_Event.OnRoleLogin, OnRoleLogin);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))//按下按键a
{
//触发事件
GameEvent.Get<ILoginUI>().OnRoleLogin(false);
}
if (Input.GetKeyDown(KeyCode.B))//按下按键b
{
//触发事件
GameEvent.Get<ILoginUI>().OnClose("HAHA",true);
}
}
private void OnRoleLogin(bool obj)
{
Debug.Log("登录"+ obj);
}
private void OnClose(string arg1, bool arg2)
{
Debug.Log("测试关闭1" + arg1 + arg2);
}
}
}