Advertisement
eugenesia

Timed Gaze Button in Unity for Google Cardboard VR

Nov 28th, 2016
2,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. /// <summary>
  2. /// Behaviors for a button that will click upon a timed gaze.
  3. /// Button is clicked after user gazes for a particular duration.
  4. /// Ref: https://www.youtube.com/watch?v=M6sL0ffosds
  5. /// </summary>
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using System.Collections;
  9.  
  10. public class LoadSceneButton : MonoBehaviour {
  11.  
  12.     // GameObject in charge of scene-level admin.
  13.     private GameObject sceneController;
  14.  
  15.     // Whether the Google Cardboard user is gazing at this button.
  16.     private bool isLookedAt = false;
  17.  
  18.     // How long the user can gaze at this before the button is clicked.
  19.     public float timerDuration = 3f;
  20.  
  21.     // Count time the player has been gazing at the button.
  22.     private float lookTimer = 0f;
  23.  
  24.     // Graphical progress indicator.
  25.     private GameObject gazeTimer;
  26.  
  27.     // Use this for initialization
  28.     void Start () {
  29.         sceneController = GameObject.Find("SceneController");
  30.         gazeTimer = GameObject.Find("GazeTimer");
  31.     }
  32.    
  33.     // Update is called once per frame
  34.     void Update () {
  35.  
  36.         // While player is looking at this button.
  37.         if (isLookedAt) {
  38.  
  39.             // Increment the gaze timer.
  40.             lookTimer += Time.deltaTime;
  41.  
  42.             // Modify graphic progress indicator to show remaining time. E.g. set the alpha layer value
  43.             // cutoff on a PNG so the part showing is proportional to remaining time.
  44.             gazeTimer.GetComponent<Renderer>().material.SetFloat("_Cutoff", lookTimer / timerDuration);
  45.  
  46.             // Gaze time exceeded limit - button is considered clicked.
  47.             if (lookTimer > timerDuration) {
  48.                 lookTimer = 0f;
  49.  
  50.                 Debug.Log("Button selected!");
  51.                 GetComponent<Button>().onClick.Invoke();
  52.             }
  53.         }
  54.  
  55.         // Not gazing at this anymore, reset everything.
  56.         else {
  57.             lookTimer = 0f;
  58.             // Reset progress indicator.
  59.             gazeTimer.GetComponent<Renderer>().material.SetFloat("_Cutoff", 0f);
  60.         }
  61.     }
  62.  
  63.     // Record whether Google Cardboard user is gazing at the button.
  64.     // gazedAt: Set it to the value passed from event trigger.
  65.     public void SetGazedAt(bool gazedAt) {
  66.         isLookedAt = gazedAt;
  67.     }
  68.  
  69.     // Call the SceneController to load the next scene in build settings.
  70.     public void LoadNextScene() {
  71.         sceneController.GetComponent<SceneController>().LoadNextScene();
  72.     }
  73.  
  74.     // Call the SceneController to load the previous scene in build settings.
  75.     public void LoadPrevScene() {
  76.         sceneController.GetComponent<SceneController>().LoadPrevScene();
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement