我对VR的凝视有一个问题。我想做的是凝视我要选择的按钮,然后隐藏第一个游戏对象父对象,然后显示第二个游戏对象父对象。现在将显示第二个游戏对象父对象,当我尝试凝视后退按钮时,它将显示第一个游戏对象父对象并隐藏第二个游戏对象父对象。问题出现在这里,当我什么都不做并且不注视按钮时,它会自动显示我的第二个游戏对象父对象,并返回到第一个父游戏对象,并始终隐藏并显示并始终隐藏。
public float gazeTime = 2f;
private float timer;
private bool gazedAt;
public Setting setting;
private void Start()
{
}
public void Update()
{
if (gazedAt)
{
timer += Time.deltaTime;
if (timer >= gazeTime)
{
// execute pointerdown handler
ExecuteEvents.Execute(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerDownHandler);
timer = 0f;
}
else
{
return;
}
}
else
{
return;
}
}
public void PointerEnter()
{
gazedAt = true;
Debug.Log("PointerEnter");
}
public void PointerExit()
{
gazedAt = false;
Debug.Log("PointerExit");
}
//Method for going to setting
public void Setting()
{
setting.ActivateSetting();
}
//Method for going back to main menu
public void GoBack()
{
setting.GoBackToMainMenu();
}
这是我的Setting
代码设置方式
public GameObject setting;
public GameObject back;
public void ActivateSetting()
{
setting.SetActive(false);
back.SetActive(true);
}
public void GoBackToMainMenu()
{
back.SetActive(false);
setting.SetActive(true);
}
我想要的是,如果我凝视它,它只会显示游戏对象的父对象。
重置点击后,您点击一次timer
但未重置gazedAt
=>该Update
方法仍然运行计时器并再次调用click。
似乎PointerExit
根本没有调用your ,因此该按钮永远不会重置。
取而代之的是EventTrigger
我会强烈建议使用界面IPointerEnterHandler
和IPointerExitHandler
类似
public class YourClass : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
//...
public void OnPointerEnter()
{
}
public void OnPointerExit()
{
}
我实际上根本不使用Update
,而是喜欢使用协程。也不要使用复杂的调用ExecuteEvents.Execute(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerDownHandler);
而不是使用其中之一Getcomponent<Button>().onClick.Invoke();
或直接调用方法
private Button _button;
private void Awake()
{
// Get the reference only once to avoid
// having to get it over and over again
_button = GetComponent<Button>();
}
private IEnumerator Gaze()
{
// wait for given time
yield return new WaitForSeconds(gazeTime);
// call the buttons onClick event
_button.onClick.Invoke();
// or as said alternatively directly use the methods e.g.
setting.ActivateSetting();
}
public void OnPointerEnter()
{
Debug.Log("PointerEnter");
// start the coroutine
StartCoroutine(Gaze());
}
public void OnPointerExit()
{
Debug.Log("PointerExit");
// stop/interrupt the coroutine
StopCoroutine(Gaze());
}
如您所见,timer
andgazedAt
值根本不需要,因此您不能忘记将它们重置在某个地方。它还避免了重复调用该方法。
如果你不希望使用Button
在所有的,你也可以添加自己UnityEvent
喜欢
// add callbacks e.g. in the Inspector or via script
public UnityEvent onGazedClick;
// ...
onGazedClick.Invoke();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句