DisconnectionNotice.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace Leap.Unity
  5. {
  6. public class DisconnectionNotice : MonoBehaviour
  7. {
  8. private void Start()
  9. {
  10. this.leap_controller_ = new Controller();
  11. this.SetAlpha(0f);
  12. }
  13. private void SetAlpha(float alpha)
  14. {
  15. base.GetComponent<Image>().color = Color.Lerp(Color.clear, this.onColor, alpha);
  16. }
  17. private bool IsConnected()
  18. {
  19. return this.leap_controller_.IsConnected;
  20. }
  21. private bool IsEmbedded()
  22. {
  23. DeviceList devices = this.leap_controller_.Devices;
  24. return devices.Count != 0 && devices[0].IsEmbedded;
  25. }
  26. private void Update()
  27. {
  28. if (this.embeddedReplacementImage != null && this.IsEmbedded())
  29. {
  30. base.GetComponent<Image>().sprite = this.embeddedReplacementImage;
  31. }
  32. if (this.IsConnected())
  33. {
  34. this.frames_disconnected_ = 0;
  35. }
  36. else
  37. {
  38. this.frames_disconnected_++;
  39. }
  40. if (this.frames_disconnected_ < this.waitFrames)
  41. {
  42. this.fadedIn -= Time.deltaTime / this.fadeOutTime;
  43. }
  44. else
  45. {
  46. this.fadedIn += Time.deltaTime / this.fadeInTime;
  47. }
  48. this.fadedIn = Mathf.Clamp(this.fadedIn, 0f, 1f);
  49. this.SetAlpha(this.fade.Evaluate(this.fadedIn));
  50. }
  51. public float fadeInTime = 1f;
  52. public float fadeOutTime = 1f;
  53. public AnimationCurve fade;
  54. public int waitFrames = 10;
  55. public Sprite embeddedReplacementImage;
  56. public Color onColor = Color.white;
  57. private Controller leap_controller_;
  58. private float fadedIn;
  59. private int frames_disconnected_;
  60. }
  61. }