Tanoda
ConnectionMonitor.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using System.Collections;
12
13namespace Leap.Unity {
20 [RequireComponent(typeof(SpriteRenderer))]
21 public class ConnectionMonitor : MonoBehaviour {
23 [Tooltip("The scene LeapServiceProvider.")]
26 [Tooltip("How fast to make the connection notice sprite visible.")]
27 [Range(0.1f, 10.0f)]
28 public float fadeInTime = 1.0f;
30 [Tooltip("How fast to fade out the connection notice sprite.")]
31 [Range(0.1f, 10.0f)]
32 public float fadeOutTime = 1.0f;
34 [Tooltip("The easing curve for the fade in and out effect.")]
35 public AnimationCurve fadeCurve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
37 [Tooltip("How frequently to check the connection.")]
38 public int monitorInterval = 2;
40 [Tooltip("A tint applied to the connection notice sprite when on.")]
41 public Color onColor = Color.white;
43 [Tooltip("How far to place the sprite in front of the camera.")]
44 public float distanceToCamera = 12.0f;
45
46 private float fadedIn = 0.0f;
47 private SpriteRenderer spriteRenderer;
48 private bool connected = false;
49
50 void Start() {
51 spriteRenderer = GetComponent<SpriteRenderer>();
52 SetAlpha(0.0f);
53 StartCoroutine(Monitor());
54 }
55
56 void SetAlpha(float alpha) {
57 spriteRenderer.color = Color.Lerp(Color.clear, onColor, alpha);
58 }
59
60 void Update() {
61 if (fadedIn > 0) {
62 Camera cam = Camera.main;
63 Vector3 pos = cam.transform.position + cam.transform.forward * distanceToCamera;
64 transform.position = pos;
65 transform.LookAt(cam.transform);
66 }
67 }
68
69 private IEnumerator Monitor() {
70 yield return new WaitForSecondsRealtime(monitorInterval); //Give controller time to connect at startup
71 while (true) { //forever
72 connected = provider.IsConnected();
73 if (connected) {
74 while (fadedIn > 0.0) {
75 fadedIn -= Time.deltaTime / fadeOutTime;
76 fadedIn = Mathf.Clamp(fadedIn, 0.0f, 1.0f);
77 SetAlpha(fadeCurve.Evaluate(fadedIn));
78 yield return null;
79 }
80 } else {
81 while (fadedIn < 1.0) {
82 fadedIn += Time.deltaTime / fadeOutTime;
83 fadedIn = Mathf.Clamp(fadedIn, 0.0f, 1.0f);
84 SetAlpha(fadeCurve.Evaluate(fadedIn));
85 yield return null;
86 }
87 }
88 yield return new WaitForSecondsRealtime(monitorInterval);
89 }
90 }
91 }
92}
UnityEngine.Color Color
Definition: TestScript.cs:32
LeapServiceProvider provider
The LeapServiceProvider provides tracked Leap Hand data and images from the device via the Leap servi...
bool IsConnected()
Returns true if the Leap Motion hardware is plugged in and this application is connected to the Leap ...