我正在使用Unity和Vuforia,并希望在AR对象上制作一个动画,该动画在找到目标后开始,并在丢失目标时重置,以便在再次找到目标时从头开始动画。
这是DeafaultTrackableHandler的代码:
public class DefaultTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
{
// Begin sound
public AudioSource soundtarget;
public AudioClip cliptarget;
private AudioSource[] allAudioSources;
// Function to stop all sounds
void StopAllAudio(){
allAudioSources = FindObjectsOfType (typeof(AudioSource)) as AudioSource[];
foreach (AudioSource audioS in allAudioSources) {
audioS.Stop ();
}
}
// Function to play sound
void playSound(string ss){
cliptarget = (AudioClip)Resources.Load (ss);
soundtarget.clip = cliptarget;
soundtarget.loop = false;
soundtarget.playOnAwake = false;
soundtarget.Play ();
}
// -------- end sound -----------
#region PRIVATE_MEMBER_VARIABLES
protected TrackableBehaviour mTrackableBehaviour;
#endregion // PRIVATE_MEMBER_VARIABLES
#region UNTIY_MONOBEHAVIOUR_METHODS
protected virtual void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
mTrackableBehaviour.RegisterTrackableEventHandler(this);
soundtarget = (AudioSource)gameObject.AddComponent<AudioSource> ();
}
#endregion // UNTIY_MONOBEHAVIOUR_METHODS
#region PUBLIC_METHODS
/// <summary>
/// Implementation of the ITrackableEventHandler function called when the
/// tracking state changes.
/// </summary>
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
OnTrackingFound();
}
else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
newStatus == TrackableBehaviour.Status.NOT_FOUND)
{
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
OnTrackingLost();
}
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
#region PRIVATE_METHODS
protected virtual void OnTrackingFound()
{
var rendererComponents = GetComponentsInChildren<Renderer>(true);
var colliderComponents = GetComponentsInChildren<Collider>(true);
var canvasComponents = GetComponentsInChildren<Canvas>(true);
// Enable rendering:
foreach (var component in rendererComponents)
component.enabled = true;
// Enable colliders:
foreach (var component in colliderComponents)
component.enabled = true;
// Enable canvas':
foreach (var component in canvasComponents)
component.enabled = true;
if(mTrackableBehaviour.TrackableName == "MERCADO")
playSound("sounds/audio");
}
protected virtual void OnTrackingLost()
{
var rendererComponents = GetComponentsInChildren<Renderer>(true);
var colliderComponents = GetComponentsInChildren<Collider>(true);
var canvasComponents = GetComponentsInChildren<Canvas>(true);
// Disable rendering:
foreach (var component in rendererComponents)
component.enabled = false;
// Disable colliders:
foreach (var component in colliderComponents)
component.enabled = false;
// Disable canvas':
foreach (var component in canvasComponents)
component.enabled = false;
StopAllAudio ();
}
#endregion // PRIVATE_METHODS
}
使用此代码,碰巧当我检测到目标时,动画就开始了,而当目标丢失时,动画就不是从头开始的。
非常感谢您的帮助。 ☺