25. using UnityEngine;
using Vuforia;
public class DefaultTrackableEventHandler:MonoBehaviour, ITrackableEventHandler
{
#region PROTECTED_MEMBER_VARIABLES protected TrackableBehaviour mTrackableBehaviour;
protected TrackableBehaviour.Status m_PreviousStatus;
protected TrackableBehaviour.Status m_NewStatus;
#endregion // PROTECTED_MEMBER_VARIABLES
#region UNITY_MONOBEHAVIOUR_METHODS
protected virtual void Start() {
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
protected virtual void OnDestroy() {
if (mTrackableBehaviour)
mTrackableBehaviour.UnregisterTrackableEventHandler(this);
}
#endregion // UNITY_MONOBEHAVIOUR_METHODS
解析DefaultTrackableEventHandler 2/7
25
實作ITrackableEventHandle介面
26. #region PUBLIC_METHODS
/*Implementation of the ITrackableEventHandler function called when the
tracking state changes. */
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus) {
m_PreviousStatus = previousStatus;
m_NewStatus = newStatus;
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName +
" " + mTrackableBehaviour.CurrentStatus +
" -- " + mTrackableBehaviour.CurrentStatusInfo);
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) {
OnTrackingFound();
}
else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
newStatus == TrackableBehaviour.Status.NO_POSE) {
OnTrackingLost();
}
解析DefaultTrackableEventHandler 3/7
26
27. else {
// For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
// Vuforia is starting, but tracking has not been lost or found yet
// Call OnTrackingLost() to hide the augmentations
OnTrackingLost();
}
}
#endregion // PUBLIC_METHODS
解析DefaultTrackableEventHandler 4/7
27
28. #region PROTECTED_METHODS
protected virtual void OnTrackingFound() {
Debug.Log("OnTrackingFound");
if (mTrackableBehaviour) {
var rendererComponents =
mTrackableBehaviour.GetComponentsInChildren<Renderer>(true);
var colliderComponents =
mTrackableBehaviour.GetComponentsInChildren<Collider>(true);
var canvasComponents =
mTrackableBehaviour.GetComponentsInChildren<Canvas>(true);
foreach (var component in rendererComponents) // Enable rendering:
component.enabled = true;
foreach (var component in colliderComponents) // Enable colliders:
component.enabled = true;
foreach (var component in canvasComponents) // Enable canvas':
component.enabled = true;
}
}
解析DefaultTrackableEventHandler 5/7
28
29. protected virtual void OnTrackingLost() {
Debug.Log(" OnTrackingLost ");
if (mTrackableBehaviour) {
var rendererComponents =
mTrackableBehaviour.GetComponentsInChildren<Renderer>(true);
var colliderComponents =
mTrackableBehaviour.GetComponentsInChildren<Collider>(true);
var canvasComponents =
mTrackableBehaviour.GetComponentsInChildren<Canvas>(true);
foreach (var component in rendererComponents) // Disable rendering:
component.enabled = false;
foreach (var component in colliderComponents) // Disable colliders:
component.enabled = false;
foreach (var component in canvasComponents) // Disable canvas':
component.enabled = false;
}
}
#endregion // PROTECTED_METHODS
}
解析DefaultTrackableEventHandler 6/7
29