凝视总是在 PointExit 之后执行
gaze always execute even after PointExit
我对 VR 的注视有问题。我想做的是注视我想要 select 的按钮,然后隐藏第一个游戏对象父级,然后显示第二个游戏对象父级。现在将显示第二个游戏对象父级,当我尝试注视后退按钮时,它将显示第一个游戏对象父级并隐藏第二个游戏对象父级。问题出现在这里,当我什么都不做并且不注视按钮时,它会自动显示我的第二个游戏对象父级并返回到第一个父级游戏对象并始终隐藏和显示以及隐藏和显示。
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
方法仍然 运行 计时器并再次调用点击。
似乎您的 PointerExit
根本没有被调用,因此按钮永远不会重置。
我强烈建议使用接口 IPointerEnterHandler
和 IPointerExitHandler
而不是 EventTrigger
,例如
public class YourClass : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
//...
public void OnPointerEnter()
{
}
public void OnPointerExit()
{
}
我实际上根本不会使用 Update
,而是更喜欢 Coroutine。也不要使用 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
和 gazedAt
值,因此您不会忘记在某处重置它们。也避免了方法被重复调用。
如果您根本不想使用 Button
,您也可以添加自己的 UnityEvent
,例如
// add callbacks e.g. in the Inspector or via script
public UnityEvent onGazedClick;
// ...
onGazedClick.Invoke();
我对 VR 的注视有问题。我想做的是注视我想要 select 的按钮,然后隐藏第一个游戏对象父级,然后显示第二个游戏对象父级。现在将显示第二个游戏对象父级,当我尝试注视后退按钮时,它将显示第一个游戏对象父级并隐藏第二个游戏对象父级。问题出现在这里,当我什么都不做并且不注视按钮时,它会自动显示我的第二个游戏对象父级并返回到第一个父级游戏对象并始终隐藏和显示以及隐藏和显示。
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
方法仍然 运行 计时器并再次调用点击。
似乎您的 PointerExit
根本没有被调用,因此按钮永远不会重置。
我强烈建议使用接口 IPointerEnterHandler
和 IPointerExitHandler
而不是 EventTrigger
,例如
public class YourClass : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
//...
public void OnPointerEnter()
{
}
public void OnPointerExit()
{
}
我实际上根本不会使用 Update
,而是更喜欢 Coroutine。也不要使用 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
和 gazedAt
值,因此您不会忘记在某处重置它们。也避免了方法被重复调用。
如果您根本不想使用 Button
,您也可以添加自己的 UnityEvent
,例如
// add callbacks e.g. in the Inspector or via script
public UnityEvent onGazedClick;
// ...
onGazedClick.Invoke();