Selzier

Gaze Radial Button with Slider Bar progress

Jul 5th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class NvrButtonRadialCutout : MonoBehaviour {
  6.  
  7.     // How long to look at Menu Item before taking action
  8.     public float timerDuration = 2f;
  9.  
  10.     // This value will count down from the duration
  11.     private float lookTimer = 0f;
  12.  
  13.     // My renderer so I can set _Cutoff value
  14.     private Renderer myRenderer;
  15.  
  16.     // Is player looking at me?
  17.     private bool isLookedAt = false;
  18.  
  19.     // The slider
  20.     public NvrGazeSlider mySlider;
  21.  
  22.     // MonoBehaviour Start
  23.     void Start() {
  24.         // Get my Renderer
  25.         myRenderer = GetComponent<Renderer>();
  26.         // Set cutoff
  27.         myRenderer.material.SetFloat("_Cutoff", 0f);
  28.     }
  29.    
  30.     // MonoBehaviour Update
  31.     void Update() {
  32.         // While player is looking at me
  33.         if (isLookedAt) {
  34.             // Reduce Timer
  35.             lookTimer += Time.deltaTime;
  36.  
  37.             // Set cutoff value on material to value between 0 and 1
  38.             myRenderer.material.SetFloat("_Cutoff", lookTimer / timerDuration);
  39.  
  40.             if (lookTimer > timerDuration) {
  41.                 // Reset timer
  42.                 lookTimer = 0f;    
  43.                 // Do something
  44.                 Debug.Log("BUTTON HAS BEEN SELECTED!");                
  45.                 // Disappear
  46.                 //gameObject.SetActive(false);
  47.             }    
  48.         }  else {
  49.             // Reset Timer
  50.             lookTimer = 0f;
  51.             // Reset Cutoff
  52.             myRenderer.material.SetFloat("_Cutoff", 0f);
  53.         }
  54.     }
  55.  
  56.     // Google Cardboard Gaze
  57.     public void SetGazedAt(bool gazedAt) {
  58.         if (gazedAt == true) {
  59.             mySlider.PointerEnter();
  60.         }
  61.         else {
  62.             mySlider.PointerExit();
  63.         }
  64.        
  65.         // Set the local bool to the one passed from Event Trigger
  66.         isLookedAt = gazedAt;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment