我在場景中有兩個3d按鈕,當我注視任何按鈕時,它將調用OnPointerEnter回調並保存指針凝視的對象。Unity Vuforia Google VR - 無法將onPointerEnter改爲自身的GameObject更改材質
在遊戲手柄上按下Fire1後,我應用從資源文件夾獲取的材料。 當我凝視第二個按鈕時,我的問題開始了,並且按下Fire1按鈕將同時笨拙地更改兩個按鈕。
這是我附着到這兩個按鈕
using UnityEngine;
using UnityEngine.EventSystems;
using Vuforia;
using System.Collections;
public class TriggerMethods : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
Material _mat;
GameObject targetObject;
Renderer rend;
int i = 0;
// Update is called once per frame
void Update() {
if (Input.GetButtonDown("Fire1"))
TukarMat();
}
public void OnPointerEnter(PointerEventData eventData)
{
targetObject = ExecuteEvents.GetEventHandler<IPointerEnterHandler>(eventData.pointerEnter);
}
public void OnPointerExit(PointerEventData eventData)
{
targetObject = null;
}
public void TukarMat()
{
Debug.Log("Value i = " + i);
if (i == 0)
{
ApplyTexture(i);
i++;
}
else if (i == 1)
{
ApplyTexture(i);
i++;
}
else if (i == 2)
{
ApplyTexture(i);
i = 0;
}
}
void ApplyTexture(int i)
{
rend = targetObject.GetComponent<Renderer>();
rend.enabled = true;
switch (i)
{
case 0:
_mat = Resources.Load("Balut", typeof(Material)) as Material;
rend.sharedMaterial = _mat;
break;
case 1:
_mat = Resources.Load("Khasiat", typeof(Material)) as Material;
rend.sharedMaterial = _mat;
break;
case 2:
_mat = Resources.Load("Alma", typeof(Material)) as Material;
rend.sharedMaterial = _mat;
break;
default:
break;
}
}
的腳本我感測到的一些邏輯錯誤和試圖使另一個類爲僅管理對象的指針注視到但我得到更加困惑。
希望得到一些幫助
謝謝
非常感謝。我已經完成了OnPointerEnter和OnPointerExit回調測試,兩者都有效。 –
@MuadzirAziz and targetObject.GetComponent(); OnPointerExit後沒有返回null? –