如何使用GVr和unity选择基于时间触发注视输入的UI按钮?

时间:2016-07-11 13:00:09

标签: unity3d

我正在使用Unity5和Gvr。我正在尝试实现一个菜单,其中用户盯着一个5秒的按钮,然后按钮将被启用。任何人都可以帮助我吗?我已经实现了十字线,它检测到了按钮。我不知道应该如何以及在哪里添加时间触发器。

2 个答案:

答案 0 :(得分:0)

将此脚本附加到您的按钮对象:

[RequireComponent(typeof(Button))]
public class InteractiveItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public Image progressImage; // add an image as child to your button object and set its image type to Filled. Assign it to this field in inspector.
    public bool isEntered = false;
    RectTransform rt;
    Button _button;
    float timeElapsed;
    Image cursor;
    // Use this for initialization
    void Awake () 
    {
        _button = GetComponent<Button>();
        rt = GetComponent<RectTransform>();

    }

    float GazeActivationTime = 3;

    void Update () 
    {
        if(isEntered)
        {
            timeElapsed += Time.deltaTime;
            progressImage.fillAmount = Mathf.Clamp(timeElapsed/GazeActivationTime,0,1);
            if(timeElapsed >= GazeActivationTime)
            {
                timeElapsed = 0;
                _button.onClick.Invoke();
                progressImage.fillAmount = 0;
                isEntered = false;
            }
        }
        else
        {
            timeElapsed = 0;
        }
    }

    #region IPointerEnterHandler implementation

    public void OnPointerEnter (PointerEventData eventData)
    {
        isEntered = true;
    }

    #endregion

    #region IPointerExitHandler implementation

    public void OnPointerExit (PointerEventData eventData)
    {
        isEntered = false;
        progressImage.fillAmount = 0;
    }
    #endregion

答案 1 :(得分:0)

编写一个实现IGvrGazeResponder的脚本,在这里,您必须覆盖OnGazeTrigger()函数,并且可以编写:

StartCoroutine(YourControllerScript.control.LoadLevel(sceneToLoad))

加载你想要的场景