【问题标题】:Unity VR autoclick after few seconds几秒钟后 Unity VR 自动点击
【发布时间】:2016-09-20 19:06:15
【问题描述】:

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

【问题讨论】:

    标签: android unity3d virtual-reality


    【解决方案1】:

    当您查看对象时,触发器已激活,对吗? 然后,当您检测到触发器启动时,只需执行一个等待 3 秒的函数,然后执行您想要的操作。

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

    【讨论】:

      【解决方案2】:

      我不久前编写了这个脚本,它可以很好地与按钮配合使用:

      [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
      
      }
      

      【讨论】:

        猜你喜欢
        • 2015-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-28
        • 2013-01-06
        • 1970-01-01
        相关资源
        最近更新 更多