1

I am using Unity5 and the Gvr.I am trying to implement a menu where the user stares at a button for a 5secs then the button is to be enabled.Can anybody please help me? I have implemented the reticle and it detects the button.I am not sure how and where I should add the time trigger.

2 Answers2

0

Attach this script to your button object:

[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
Umair M
  • 10,298
  • 6
  • 42
  • 74
  • :Thank you :).If its not too much trouble,I am using the Gvr gaze on the Ui button. ie,the reticle on the button.So the cursor would be the reticle in my case.How can i modify ? I get the eroor stating "typename or namespace image could not be found". And thank you , not everybody replies with code :) – Saranya Ananthakrishnan Jul 12 '16 at 16:00
  • At start of script add `using UnityEngine.UI;`. And if your GVR gaze works fine, attaching this script to button should handle events like `OnPointerEnter `, `OnPointerExit` etc. let me know if there is anyother problem. Please mark the answer correct and vote up :) – Umair M Jul 13 '16 at 09:28
  • I'm trying to use the same script but it is not working anymore :( https://stackoverflow.com/questions/60114336/onpointerenter-is-not-being-called –  Feb 07 '20 at 13:41
0

Write a script that implements IGvrGazeResponder and at here, you have to override OnGazeTrigger() function and in that you can write:

StartCoroutine(YourControllerScript.control.LoadLevel(sceneToLoad))

to load the scene that you want

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213