前因
最近在学unity,在官方项目BossRoom的基础上改造,想加个简易聊天
简单试了下直接给在UI上写ServerRpc不太行,只能挂到管理器上
但是遇到了问题,UI调ServerRpc,必须先拿到管理器的对象,但是聊天既要在选角色界面用,又要在战斗界面,管理器还不一样。
如果聊天代码都写在state里面,还需要拿到UI对象,才能注册点击事件拿到输入信息
写出来是这样的,用了GameObject.Find
namespace Unity.BossRoom.Utils
{
public class CommonChatHelper : NetworkBehaviour
{
[Inject]
IPublisher<CommonChatMessage> m_ChatPublisher;
private TMP_InputField inputBox;
private Button sendButton;
// Start is called before the first frame update
void Start()
{
inputBox = GameObject.Find("CharacterSelectCanvas/Chat/Input").GetComponent<TMP_InputField>