I decided that it was only slightly harder to fix the root problem than it was to explain how to work around the issue.
Here is a script that will display a sprite when the Leap hardware or software isn't connected. To use this script:
- Create an empty game object.
- Add the script below as a component.
- Drag a sprite to the SpriteRenderer added with the script. You can use the sprite textures in LeapMotion/Textures.
- Set the SpriteRenderer Flip X property.
- Set the DistanceToCamera and other properties to taste.
Connection Monitor class:
using UnityEngine;
using System.Collections;
using Leap.Unity.Attributes;
namespace Leap.Unity {
/**
* The ConnectionMonitor class monitors the connection to the Leap Motion service
* and displays a sprite in front of the camera when a connection is not
* available. You can use the PluginLeapNotice sprites in the LeapMotion/Textures
* folder or create your own.
*/
[RequireComponent(typeof(SpriteRenderer))]
public class ConnectionMonitor : MonoBehaviour {
/** The LeapServiceProvider in the scene. */
[AutoFind]
[Tooltip("The scene LeapServiceProvider.")]
public LeapServiceProvider provider;
/** The speed to fade the sprite alpha from 0 to 1. */
[Tooltip("How fast to make the connection notice sprite visible.")]
[Range(0.1f, 10.0f)]
public float fadeInTime = 1.0f;
/** The speed to fade the sprite alpha from 1 to 0. */
[Tooltip("How fast to fade out the connection notice sprite.")]
[Range(0.1f, 10.0f)]
public float fadeOutTime = 1.0f;
/** The easing curve. */
[Tooltip("The easing curve for the fade in and out effect.")]
public AnimationCurve fadeCurve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
/** How often to check the connection. */
[Tooltip("How frequently to check the connection.")]
public int monitorInterval = 2;
/** The fully on texture tint color. */
[Tooltip("A tint applied to the connection notice sprite when on.")]
public Color onColor = Color.white;
/** The distance of the notification sprite from the camera in world units. */
[Tooltip("How far to place the sprite in front of the camera.")]
public float distanceToCamera = 12.0f;
private float fadedIn = 0.0f;
private int frames_disconnected = 0;
private SpriteRenderer spriteRenderer;
private bool connected = false;
void Start() {
spriteRenderer = GetComponent<SpriteRenderer>();
SetAlpha(0.0f);
StartCoroutine(Monitor());
}
void SetAlpha(float alpha) {
spriteRenderer.color = Color.Lerp(Color.clear, onColor, alpha);
}
void Update() {
if (fadedIn > 0) {
Camera cam = Camera.main;
Vector3 pos = cam.transform.position + cam.transform.forward * distanceToCamera;
transform.position = pos;
transform.LookAt(cam.transform);
}
}
private IEnumerator Monitor() {
yield return new WaitForSecondsRealtime(monitorInterval); //Give controller time to connect at startup
while (true) { //forever
connected = provider.IsConnected();
if (connected) {
while (fadedIn > 0.0) {
fadedIn -= Time.deltaTime / fadeOutTime;
fadedIn = Mathf.Clamp(fadedIn, 0.0f, 1.0f);
SetAlpha(fadeCurve.Evaluate(fadedIn));
yield return null;
}
} else {
while (fadedIn < 1.0) {
fadedIn += Time.deltaTime / fadeOutTime;
fadedIn = Mathf.Clamp(fadedIn, 0.0f, 1.0f);
SetAlpha(fadeCurve.Evaluate(fadedIn));
yield return null;
}
}
yield return new WaitForSecondsRealtime(monitorInterval);
}
}
}
}
The original source can be found here: (currently in the fix-disconnection-notice branch)