gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:06:27 34 4
gpt4 key购买 nike

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

我希望每个视频在其父图像目标被识别时开始,但仅限于该特定视频。我需要控制每个图像目标/视频的可跟踪行为。

我该怎么做?我知道我需要在 Unity 中查看的脚本是 DefaultTrackableEventHandler.csmediaPlayerCtrl.cs(这是我用于视频管理器(视频播放器)的脚本),我从 Easy Movie Texture Assets 获得。我需​​要在这些脚本中编写什么代码才能实现它。这是 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
}

最佳答案

您必须在每次丢失跟踪时停止视频,并在找到跟踪时重新开始。我不确切知道您的视频 Assets 是如何工作的,但我会使用通用名称/功能,以便您明白这一点

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

关于c++ - Vuforia——识别图像时如何只播放一个视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50224773/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com