Advertisement
AnthonyC

OnFocusColorChanger / Anthony / Next Reality

Apr 25th, 2017
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using HoloToolkit.Unity.InputModule;
  4.  
  5. public class OnFocusColorChanger : MonoBehaviour, IFocusable
  6. {
  7.     public Color focusedColor;
  8.     [Space(10)]
  9.     public float fadeInTime = 1;
  10.     public float fadeOutTime = 1;
  11.  
  12.     private float progress;
  13.  
  14.     private Material material;
  15.     private Color unfocusedColor;
  16.  
  17.     private Coroutine coroutine;
  18.  
  19.     void Start()
  20.     {
  21.         material = GetComponent<Renderer>().material;
  22.         unfocusedColor = material.GetColor("_Color");
  23.     }
  24.  
  25.     public void OnFocusEnter()
  26.     {
  27.         StopCurrentCoroutineIfActive();
  28.         coroutine = StartCoroutine(CountProgress(fadeInTime));
  29.     }
  30.  
  31.     public void OnFocusExit()
  32.     {
  33.         StopCurrentCoroutineIfActive();
  34.         coroutine = StartCoroutine(CountProgress(fadeOutTime * -1));
  35.     }
  36.  
  37.     private void StopCurrentCoroutineIfActive()
  38.     {
  39.         if (progress != 0 && progress != 1)
  40.         {
  41.             StopCoroutine(coroutine);
  42.         }
  43.     }
  44.  
  45.     IEnumerator CountProgress(float time)
  46.     {
  47.         while (progress >= 0 && progress <= 1)
  48.         {
  49.             float increment = Time.deltaTime / time;
  50.             progress += increment;
  51.             material.SetColor("_Color", Color.Lerp(unfocusedColor, focusedColor, progress));
  52.             yield return null;
  53.         }
  54.         progress = Mathf.Clamp(progress, 0, 1);
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement