目光总是甚至PointExit后执行

问题描述 投票:1回答:1

我有一个关于我的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);
}  

我要的是,它只会显示游戏对象父,如果我凝望着它。

c# unity3d virtual-reality gaze-buttons
1个回答
1
投票

调用的点击,一旦你重置timer但你没有重置gazedAt

=>在Update方法也仍然运行计时器,并再次呼的一下。

看来你的PointerExit完全不叫,所以按钮不会被重置。


取而代之的是EventTrigger的我会强烈建议使用接口IPointerEnterHandlerIPointerExitHandler

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());
}

正如你可以看到有没有必要在所有的timergazedAt值,所以你不能忘记的地方重新设置。这也避免了该方法被反复调用。


如果你不希望在所有使用Button你也可以添加自己喜欢UnityEvent

// add callbacks e.g. in the Inspector or via script
public UnityEvent onGazedClick;

// ...

onGazedClick.Invoke();
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.