Vuforia——识别图像时如何只播放一个视频

Vuforia -- how to only play one video when an image is recognized

我有一个 AR 应用程序,它有 4 个图像目标,每个图像目标都有一个对应的视频,当它被识别时会在其上面播放。问题在于,即使在识别图像时显示正确的动画,也会播放所有其他动画,即使它们没有显示。因此,每当我从一个图像目标移动到另一个图像目标时,都会显示视频,但它已经运行了一半。

我希望每个视频在其父图像目标被识别时启动,但只启动该特定视频。我需要控制每个图像的可跟踪行为 target/video。

我该怎么做?我知道我需要在 Unity 中查看的脚本是 DefaultTrackableEventHandler.csmediaPlayerCtrl.cs (这是一个脚本我正在使用从 Easy Movie Texture 资产 获得的视频管理器(视频播放器)。我需要在这些脚本中编写什么代码才能实现它。这是DefaultTrackableEventHandler.cs 代码:

using UnityEngine;
using Vuforia;

/// <summary>
///     A custom handler that implements the ITrackableEventHandler 
interface.
/// </summary>
public class DefaultTrackableEventHandler : MonoBehaviour, 
ITrackableEventHandler
{
#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);
}

#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;
}


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;
}

#endregion // PRIVATE_METHODS
}

您必须在每次丢失跟踪时停止视频,并在找到跟踪时重新开始。我不知道你的视频资产是如何工作的,但我会使用通用的 names/functions 这样你就明白了

protected virtual void OnTrackingLost()
{
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);
    var myVideo = GetComponentInChildren<TheVideoClassAttachedToChildOfImageTarget>

    // 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;

    // Stop video
    myVideo.Stop();
}

与 OnTrackingFound 类似

protected virtual void OnTrackingFound()
{
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);
    var myVideo = GetComponentInChildren<TheVideoClassAttachedToChildOfImageTarget>

    // 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;

    // Play the video again from the beggining
    myVideo.Play();
}
protected virtual void OnTrackingFound()
{
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);

    AudioSource[] audios = GetComponentsInChildren<AudioSource>();
    foreach (var audio in audios)
        audio.Play();


    // 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 (_videoHandler != null)
    {
        _videoHandler.Enable(true);
    }
}

OnTrackingLost:

protected virtual void OnTrackingLost()
{
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);

    AudioSource[] audios = GetComponentsInChildren<AudioSource>();

    //AudioSource[] allAudioSources = UnityEngine.Object.FindObjectsOfType<AudioSource>();

    foreach (var audio in audios)
        audio.Stop();


    // 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;

    if (_videoHandler != null)
    {
        _videoHandler.Enable(false);
    }
}