我正在為我創(chuàng)建的虛擬鍵盤(pán)編寫(xiě)鍵盤(pán)管理器。我正在使用 UnityAction 返回調(diào)用對(duì)象并處理鍵盤(pán)輸入。然而,不知何故,我的 UnityAction 丟失了其訂閱的事件,我得到一個(gè)“對(duì)象引用未設(shè)置到對(duì)象實(shí)例。我檢查了調(diào)用函數(shù),看看 UnityAction 到達(dá)腳本后是否不為空,事實(shí)并非如此。然而,檢查關(guān)閉中的其他地方都會(huì)返回 null。您將在我的所有調(diào)試代碼下面找到我檢查 UnityAction 不為空的位置。代碼中唯一不為 null 的地方是 ReadUserInput 函數(shù)。所有其他均為空。我在很多地方都使用過(guò)這種編碼方法,沒(méi)有出現(xiàn)任何問(wèn)題。不知道為什么這個(gè)這么粘!調(diào)用代碼: VRKeyboard.ProcessInput += AddKbdInputToInputline; VRKeyboard.ReadUserInput();VR鍵盤(pán)代碼: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_花開(kāi)花謝_0
TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊
我通過(guò)對(duì) ProcessInput 使用靜態(tài)變量解決了這個(gè)問(wèn)題...還是很奇怪,我不確定 Unity 在處理 OnPointerDown 事件時(shí)是否會(huì)忘記公共 UnityEvent 的值。
- 1 回答
- 0 關(guān)注
- 208 瀏覽
添加回答
舉報(bào)
0/150
提交
取消