Unity Vuforia多目标视频自动播放问题

时间:2019-10-14 08:13:32

标签: unity3d vuforia

我在这里遇到Unity问题。我试图在线搜索解决方案,对其进行了测试,但是没有用。现在,我正在使用Unity和Vuforia开发AR应用程序。我正在使用带有3D对象和视频的多个图像目标。

问题在于视频将自动播放而无需扫描目标。我没有选择“清醒时播放”,但是它只会显示白色四边形(因为我将视频附加到四边形对象上)。

我还尝试在DefaultTrackableEventHandler脚本上添加quad.Play()和quad.Stop(),因为有些人设法使其正常工作。

以下是脚本:

/*==============================================================================
Copyright (c) 2017 PTC Inc. All Rights Reserved.

Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Protected under copyright and other laws.
==============================================================================*/

using UnityEngine;
using Vuforia;

/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
///
/// Changes made to this file could be overwritten when upgrading the Vuforia version.
/// When implementing custom event handler behavior, consider inheriting from this class instead.
/// </summary>
public class DefaultTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
{

    public UnityEngine.Video.VideoPlayer quad;

    #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

    #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)
    {
        m_PreviousStatus = previousStatus;
        m_NewStatus = 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.NO_POSE)
        {
            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 PROTECTED_METHODS

    protected virtual void OnTrackingFound()
    {
        quad.Play();
        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()
    {
        quad.Stop();
        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 // PROTECTED_METHODS
}

希望你们能有所帮助。谢谢:)

1 个答案:

答案 0 :(得分:0)

您需要分别在OnTrackingFound()和OnTrackingLost()中添加代码以播放和暂停视频播放器。

  

对于OnTrackingFound()

if (mTrackableBehaviour.gameObject.GetComponentInChildren<VideoPlayer>() != null)
   {
            mTrackableBehaviour.gameObject.GetComponentInChildren<VideoPlayer>().Play();
   }
  

对于OnTrackingLost()

if (mTrackableBehaviour.gameObject.GetComponentInChildren<VideoPlayer>() != null)
    {
            mTrackableBehaviour.gameObject.GetComponentInChildren<VideoPlayer>().Pause();
    }
  

完成脚本。用此代码替换脚本。


using UnityEngine;

using Vuforia;

using UnityEngine.Video;      //added

/// <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.NO_POSE)

        {

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

        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;

        // added

        if (mTrackableBehaviour.gameObject.GetComponentInChildren<VideoPlayer>() != null)

        {

            mTrackableBehaviour.gameObject.GetComponentInChildren<VideoPlayer>().Play();

        }

    }



    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;

        // added

        if (mTrackableBehaviour.gameObject.GetComponentInChildren<VideoPlayer>() != null)

        {

            mTrackableBehaviour.gameObject.GetComponentInChildren<VideoPlayer>().Pause();

        }

    }

    #endregion // PRIVATE_METHODS

}