Advertisement
AnthonyC

OnFocusScaler / Anthony / Next Reality

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