我正在為我創建的虛擬鍵盤編寫鍵盤管理器。我正在使用 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條經驗 獲得超7個贊
我通過對 ProcessInput 使用靜態變量解決了這個問題...還是很奇怪,我不確定 Unity 在處理 OnPointerDown 事件時是否會忘記公共 UnityEvent 的值。
- 1 回答
- 0 關注
- 188 瀏覽
添加回答
舉報
0/150
提交
取消