为了账号安全,请及时绑定邮箱和手机立即绑定

UnityAction 丢失订阅事件

UnityAction 丢失订阅事件

C#
不负相思意 2023-08-20 15:22:35
我正在为我创建的虚拟键盘编写键盘管理器。我正在使用 UnityAction 返回调用对象并处理键盘输入。然而,不知何故,我的 UnityAction 丢失了其订阅的事件,我得到一个“对象引用未设置到对象实例。我检查了调用函数,看看 UnityAction 到达脚本后是否不为空,事实并非如此。然而,检查关闭中的其他地方都会返回 null。您将在我的所有调试代码下面找到我检查 UnityAction 不为空的位置。代码中唯一不为 null 的地方是 ReadUserInput 函数。所有其他均为空。我在很多地方都使用过这种编码方法,没有出现任何问题。不知道为什么这个这么粘!调用代码:    VRKeyboard.ProcessInput += AddKbdInputToInputline;    VRKeyboard.ReadUserInput();VR键盘代码:public TMPro.TMP_Text InputLine;public GameObject VRKeyboard;public UnityAction ProcessInput;private bool isdone = false;// Update is called once per framevoid Update(){    if (ProcessInput == null) Debug.Log("ProcessInput is null at update"); else Debug.Log("ProcessInput is NOT null at update");    if (isdone) CloseKeyboard();}public void ReadUserInput(){    InputLine.text = "";    VRKeyboard.SetActive(true);    if (ProcessInput == null) Debug.Log("ProcessInput is null at open"); else Debug.Log("ProcessInput is NOT null at open");}public void OnPointerDown(PointerEventData p){    Text ButtonPressed;    ButtonPressed = GetComponentInChildren<Text>(this);    switch (ButtonPressed.text)    {        case "Clear All":            InputLine.text = "";            break;        case "Backsp":            InputLine.text = InputLine.text.Substring(0, InputLine.text.Length - 1);            break;        case "Enter":            isdone = true;            break;       default:            InputLine.text += ButtonPressed.text;            break;    }    if (ProcessInput == null) Debug.Log("ProcessInput is null at pointerdown"); else Debug.Log("ProcessInput is NOT null at pointerdown");}public void OnPointerUp(PointerEventData p){}public void CloseKeyboard(){    GameObject VRKeyboard = transform.parent.parent.parent.gameObject;    VRKeyboard.SetActive(false);    if (ProcessInput == null) Debug.Log("ProcessInput is null at close");    //This line below is where I get the error:    ProcessInput(); }
查看完整描述

1 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超6个赞

我通过对 ProcessInput 使用静态变量解决了这个问题...还是很奇怪,我不确定 Unity 在处理 OnPointerDown 事件时是否会忘记公共 UnityEvent 的值。



查看完整回答
反对 回复 2023-08-20
  • 1 回答
  • 0 关注
  • 101 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信