Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 即使在PointExit之后也始终执行_C#_Unity3d_Virtual Reality_Gaze Buttons - Fatal编程技术网

C# 即使在PointExit之后也始终执行

C# 即使在PointExit之后也始终执行,c#,unity3d,virtual-reality,gaze-buttons,C#,Unity3d,Virtual Reality,Gaze Buttons,我对虚拟现实的关注有问题。我想做的是凝视我想要选择的按钮,然后隐藏第一个游戏对象父对象,然后显示第二个游戏对象父对象。现在将显示第二个游戏对象父对象,当我尝试注视后退按钮时,它将显示第一个游戏对象父对象,隐藏第二个游戏对象父对象。问题出现在这里,当我尝试什么都不做并且不盯着按钮时,它会自动显示我的第二个gameobject父对象,并返回第一个父对象gameobject和隐藏并显示,以及始终隐藏并显示 public float gazeTime = 2f; private float timer;

我对虚拟现实的关注有问题。我想做的是凝视我想要选择的按钮,然后隐藏第一个游戏对象父对象,然后显示第二个游戏对象父对象。现在将显示第二个游戏对象父对象,当我尝试注视后退按钮时,它将显示第一个游戏对象父对象,隐藏第二个游戏对象父对象。问题出现在这里,当我尝试什么都不做并且不盯着按钮时,它会自动显示我的第二个gameobject父对象,并返回第一个父对象gameobject和隐藏并显示,以及始终隐藏并显示

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();
}
下面是如何设置我的
设置
代码

public GameObject setting;
public GameObject back; 

public void ActivateSetting()
{
    setting.SetActive(false);
    back.SetActive(true);
}

public void GoBackToMainMenu()
{
    back.SetActive(false);
    setting.SetActive(true);
}  

我想要的是,只有当我盯着它看的时候,它才会显示游戏对象的父对象。

一旦你重置了
计时器,就调用了click,但是你没有重置
gazedAt

=>更新方法仍然运行计时器并再次调用单击

您的
PointerExit
似乎根本没有被调用,因此该按钮永远不会重置


我强烈建议不要使用
EventTrigger
而使用接口和
ipointexthandler
之类的工具

public class YourClass : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    //...

    public void OnPointerEnter()
    {

    }

    public void OnPointerExit()
    {

    }

事实上,我根本不会使用
Update
,而是更喜欢使用一个。也不要使用复杂的调用
ExecuteEvents.Execute(gameObject,newpointereventdata(EventSystem.current),ExecuteEvents.pointerDownHandler)改为使用
Getcomponent().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());
}

Setting
GoBack
是否在某处被调用?它们在EventTrigger@derHugoHow上被调用?是否同时在
OnPointerDown
中?还有,为什么要执行
EventTrigger
?不要复杂地调用
ExecuteEvents.Execute
为什么不直接调用相应的方法呢?还要确保
gazeTime
的值仍为
2.0f
。。它在检查器中可能与脚本中不同。而不是
ExecuteEvents.Execute(gameObject,new PointerEventData(EventSystem.current),ExecuteEvents.pointerDownHandler)只需执行,例如
GetComponent.onClick.Invoke()或直接,例如
设置.ActivateSetting()问题是,如果它点击按钮,哪怕是一瞬间,它也会等待2秒钟才能激活,即使我将目光切换到另一个按钮。实际上,此代码非常有用,我会将其标记为一个答案,尽管我诚实地说,这不是我想要的。但这很有帮助不。。它根本不应该激活,因为当你退出按钮时,程序就停止了,但它不会停止,先生。嗯,好的。。如何调用
PointerEnter
PointerExit
// add callbacks e.g. in the Inspector or via script
public UnityEvent onGazedClick;

// ...

onGazedClick.Invoke();