前提・実現したいこと
初めまして。
Unity2018.4.36のvuforiaでARでランダムで映像を再生するscriptを書いています。
発生している問題・エラーメッセージ
ARのマーカーを認識時に映像をランダムで一つ再生するところが上手く行かず困っています。ご助力いただけないでしょうか。
該当のソースコード
/*==============================================================================
Copyright (c) 2019 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;
using UnityEngine.Video;
/// <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 DefaultTrackableEventHandler2 : MonoBehaviour, ITrackableEventHandler
{
#region PROTECTED_MEMBER_VARIABLES
protected TrackableBehaviour mTrackableBehaviour; protected TrackableBehaviour.Status m_PreviousStatus; protected TrackableBehaviour.Status m_NewStatus; [SerializeField] VideoPlayer videoPlayer; [SerializeField] VideoClip[] videoClips; private int number; #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; 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(); } 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 void SelectClipRandomly(VideoPlayer v) { v.Stop(); // 再生を停止 v.clip = videoClips[Random.Range(0, videoClips.Length)]; // 動画をランダムに選ぶ v.Prepare(); // 事前に読み込む } protected virtual void OnTrackingFound() { if (mTrackableBehaviour) { var rendererComponents = mTrackableBehaviour.GetComponentsInChildren<Renderer>(true); var colliderComponents = mTrackableBehaviour.GetComponentsInChildren<Collider>(true); var canvasComponents = mTrackableBehaviour.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; } // ARマーカーが認識されたら動画が再生されるようにします number = Random.Range(0, 2); videoPlayer.frame = 0; videoPlayer.Play(); } protected virtual void OnTrackingLost() { // ついでにARマーカーの認識が外れたら停止するようにしておきます videoPlayer.frame = 0; videoPlayer.Pause(); if (mTrackableBehaviour) { var rendererComponents = mTrackableBehaviour.GetComponentsInChildren<Renderer>(true); var colliderComponents = mTrackableBehaviour.GetComponentsInChildren<Collider>(true); var canvasComponents = mTrackableBehaviour.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; } } public override bool Equals(object obj) { return obj is DefaultTrackableEventHandler2 handler && base.Equals(obj) && number == handler.number; } public override int GetHashCode() { return base.GetHashCode(); } public override string ToString() { return base.ToString(); } #endregion // PROTECTED_METHODS
}
補足情報(FW/ツールのバージョンなど)
あなたの回答
tips
プレビュー