Unity VR在几秒钟后自动点击

时间:2016-09-20 19:06:16

标签: android unity3d virtual-reality

我正在使用GoogleVR软件包,我有这个可用的标线(从某种意义上说它使我看起来更大的对象)。我想要的行为是通过查看对象大约三秒钟来单击。该对象当前有一个事件触发器,但我怎么能等待三秒然后单击?

2 个答案:

答案 0 :(得分:0)

当你看对象时,触发器被激活了吗? 然后当你检测到触发器时,只需要制作一个等待3秒的功能,然后再做你想做的事。

如果用户开始寻找其他地方,请不要忘记停止等待。

答案 1 :(得分:0)

我刚才写了这个脚本,它可以正常使用按钮:

[RequireComponent(typeof(Button))]
public class InteractiveItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler   
{
    public Image progressImage;  // add an image as child of button and set its image type to Filled. And assign it here.
    public bool isEntered = false;

    float GazeActivationTime = 3f;
    float timeElapsed;
    Button _button;

    void Start () 
    {
        _button = GetComponent<Button>();
    }

    void fillProgress(float value)
    {
        if (progressImage != null)
        {
            progressImage.fillAmount = value;
        }
    }

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

    void OnDisable()
    {
        if (this.enabled)
        {
            isEntered = false;
            fillProgress(0);    
        }
    }

#region IPointerEnterHandler implementation

    public void OnPointerEnter (PointerEventData eventData)
    {
        if (_button.IsInteractable())
        {
            isEntered = true;
        }
    }

#endregion

#region IPointerExitHandler implementation

    public void OnPointerExit (PointerEventData eventData)
    {
        if (!_button.IsInteractable())
            return;
        try
        {
            isEntered = false;
            fillProgress(0);
        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex.Message);
        }
    }
#endregion

#region IPointerClickHandler implementation

    public void OnPointerClick (PointerEventData eventData)
    {
        isEntered = false;
        timeElapsed = 0;
        fillProgress(0);

    }
#endregion

}