unity+高通vuforia开发增强现实(AR)教程(三) (勘误)

按照原作者的想法,是可以实现手机观察到播放封面,但没法点击播放。针对这个问题,我翻看了官方论坛,因为最新的unity包缺少了一个C#文件。把缺少的文件加上,可以实现播放,以下为详细说明:

The objective here is to show how to replicate the essence of the Vuforia-VideoPlayback sample scene using the Vuforia prefabs and the drag and drop approach of Unity:

  • Create a new Unity project
  • Import the Vuforia video playback unity package
  • Create a new scene
  • Drag the ARCamera prefab into the Unity scene
  • Under the DataSetLoadBehaviour in the Inspector tick “Load Data Set StonesAndChips”, and the 'Activate' checkbox below this
  • From '/Qualcomm AugmentedReality/Prefabs' drag the ImageTarget prefab into the scene
    • For the Image Target select “StonesAndChips” as the dataset and the Image Target should change to the Stones texture
  • From Vuforia Video Playback/Prefabs drag the Video prefab to be the child of the Image Target
    • In the Inspector under “Video Playback Behaviour (Script)” set the path to 'VuforiaSizzleReel_1.m4v'
  • Drag the TrackableEventHandler from Scripts to the Image Target (this plays the video)
  • Remove the DefaultTrackableEventHandler script from the Image Target as it is not needed.
  • Autoplay works already, however tapping the video does not yet work. In order to fix this, simply create a VideoPlaybackController script, fill it with the code below, and then attach this to the ARCamera.

       大概意思就是(我英语不好,懂意思就好= =#,英语好的还是读上文吧):

  • 创建一个新的Unity工程
  • 引入Vuforia video playback的unity包
  • 创建一个新的场景
  • 拖ARCamera进场景(ARCamera搜索下很好找到)
  • 点击场景中的ARCamera,右边Inspector栏目,DataSetLoadBehaviour(script)中“Load Data Set StonesAndChips”和 'Activate'都打上勾
  • 从'/Qualcomm AugmentedReality/Prefabs'目录中Image Target拖入场景,右边Data Set选择“StonesAndChips”
  • 从“Vuforia Video Playback/Prefabs”目录中Video拖入场景,右边“Video Playback Behaviour (Script)”中path栏输入'VuforiaSizzleReel_1.m4v'(测试是播放这个视频),或者也可以使用
    http://oneshot.qualcomm.com/webAR/content/strawberryfields_H264_AAC.mp4
    这个网络视频进行测试

  • 把TrackableEventHandler这个C#文件放到Image Target中,主要是用来播放视频的
  • 从Image Target中移除DefaultTrackableEventHandler这个文件的使用,因为这个文件根本不需要,移除的话其实把它的打勾去掉也可以
  • 自动播放要不要打上勾随自己需要, unity+高通vuforia开发增强现实(AR)教程(三) (勘误)_第1张图片  创建VideoPlaybackController的C#文件放入ARCamera中,unity+高通vuforia开发增强现实(AR)教程(三) (勘误)_第2张图片VideoPlaybackController中的代码如下:


/*==============================================================================
Copyright (c) 2012-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
 
This  Vuforia(TM) sample application in source code form ("Sample Code") for the
Vuforia Software Development Kit and/or Vuforia Extension for Unity
(collectively, the "Vuforia SDK") may in all cases only be used in conjunction
with use of the Vuforia SDK, and is subject in all respects to all of the terms
and conditions of the Vuforia SDK License Agreement, which may be found at
<a title="https://developer.vuforia.com/legal/license" href="https://developer.vuforia.com/legal/license">https://developer.vuforia.com/legal/license</a>.
 
By retaining or using the Sample Code in any manner, you confirm your agreement
to all the terms and conditions of the Vuforia SDK License Agreement.  If you do
not agree to all the terms and conditions of the Vuforia SDK License Agreement,
then you may not retain or use any of the Sample Code in any manner.
==============================================================================*/
 
using UnityEngine;
using System.Collections;
 
/// <summary>
/// This class contains the logic to handle taps on VideoPlaybackBehaviour game objects
/// and starts playing the according video. It also pauses other videos when a new one is
/// started.
/// </summary>
public class VideoPlaybackController : MonoBehaviour
{
    #region PRIVATE_MEMBER_VARIABLES
 
    private Vector2 mTouchStartPos;
    private bool mTouchMoved = false;
    private float mTimeElapsed = 0.0f;
 
    private bool mTapped = false;
    private float mTimeElapsedSinceTap = 0.0f;
 
    private bool mWentToFullScreen = false;
 
    #endregion // PRIVATE_MEMBER_VARIABLES
 
 
 
    #region UNITY_MONOBEHAVIOUR_METHODS
 
    void Update()
    {
        // Determine the number of taps
        // Note: Input.tapCount doesn't work on Android
 
        if (Input.touchCount > 0)
        {
            Touch touch = Input.touches[0];
            if (touch.phase == TouchPhase.Began)
            {
                mTouchStartPos = touch.position;
                mTouchMoved = false;
                mTimeElapsed = 0.0f;
            }
            else
            {
                mTimeElapsed += Time.deltaTime;
            }
 
            if (touch.phase == TouchPhase.Moved)
            {
                if (Vector2.Distance(mTouchStartPos, touch.position) > 40)
                {
                    // Touch moved too far
                    mTouchMoved = true;
                }
            }
            else if (touch.phase == TouchPhase.Ended)
            {
                if (!mTouchMoved && mTimeElapsed < 1.0)
                {
                    if (mTapped)
                    {
                        // Second tap
                        HandleDoubleTap();
                        mTapped = false;
                    }
                    else
                    {
                        // Wait to see if this is a double tap
                        mTapped = true;
                        mTimeElapsedSinceTap = 0.0f;
                    }
                }
            }
        }
 
        if (mTapped)
        {
            if (mTimeElapsedSinceTap >= 0.5f)
            {
                // Not a double tap
                HandleTap();
                mTapped = false;
            }
            else
            {
                mTimeElapsedSinceTap += Time.deltaTime;
            }
        }
 
        // special handling in play mode:
        if (QCARRuntimeUtilities.IsPlayMode())
        {
            if (Input.GetMouseButtonUp(0))
            {
                if (PickVideo(Input.mousePosition) != null)
                    Debug.LogWarning("Playing videos is currently not supported in Play Mode.");
            }
        }
    }
 
    #endregion // UNITY_MONOBEHAVIOUR_METHODS
 
 
 
    #region PRIVATE_METHODS
 
    /// <summary>
    /// Handle single tap event
    /// </summary>
    private void HandleTap()
    {
        // Find out which video was tapped, if any
        VideoPlaybackBehaviour video = PickVideo(mTouchStartPos);
 
        if (video != null)
        {
            if (video.VideoPlayer.IsPlayableOnTexture())
            {
                // This video is playable on a texture, toggle playing/paused
 
                VideoPlayerHelper.MediaState state = video.VideoPlayer.GetStatus();
                if (state == VideoPlayerHelper.MediaState.PAUSED ||
                    state == VideoPlayerHelper.MediaState.READY ||
                    state == VideoPlayerHelper.MediaState.STOPPED)
                {
                    // Pause other videos before playing this one
                    PauseOtherVideos(video);
 
                    // Play this video on texture where it left off
                    video.VideoPlayer.Play(false, video.VideoPlayer.GetCurrentPosition());
                }
                else if (state == VideoPlayerHelper.MediaState.REACHED_END)
                {
                    // Pause other videos before playing this one
                    PauseOtherVideos(video);
 
                    // Play this video from the beginning
                    video.VideoPlayer.Play(false, 0);
                }
                else if (state == VideoPlayerHelper.MediaState.PLAYING)
                {
                    // Video is already playing, pause it
                    video.VideoPlayer.Pause();
                }
            }
            else
            {
                // Display the busy icon
                video.ShowBusyIcon();
                 
                // This video cannot be played on a texture, play it full screen
                video.VideoPlayer.Play(true, 0);
                mWentToFullScreen = true;
            }
        }
    }
 
 
    /// <summary>
    /// Handle double tap event
    /// </summary>
    private void HandleDoubleTap()
    {
        // Find out which video was tapped, if any
        VideoPlaybackBehaviour video = PickVideo(mTouchStartPos);
 
        if (video != null)
        {
            if (video.VideoPlayer.IsPlayableFullscreen())
            {
                // Pause the video if it is currently playing
                video.VideoPlayer.Pause();
 
                // Seek the video to the beginning();
                video.VideoPlayer.SeekTo(0.0f);
 
                // Display the busy icon
                video.ShowBusyIcon();
 
                // Play the video full screen
                video.VideoPlayer.Play(true, 0);
                mWentToFullScreen = true;
            }
        }
    }
 
 
    /// <summary>
    /// Find the video object under the screen point
    /// </summary>
    private VideoPlaybackBehaviour PickVideo(Vector3 screenPoint)
    {
        VideoPlaybackBehaviour[] videos = (VideoPlaybackBehaviour[])
                FindObjectsOfType(typeof(VideoPlaybackBehaviour));
 
        Ray ray = Camera.main.ScreenPointToRay(screenPoint);
        RaycastHit hit = new RaycastHit();
 
        foreach (VideoPlaybackBehaviour video in videos)
        {
            if (video.collider.Raycast(ray, out hit, 10000))
            {
                return video;
            }
        }
 
        return null;
    }
 
 
    /// <summary>
    /// Pause all videos except this one
    /// </summary>
    private void PauseOtherVideos(VideoPlaybackBehaviour currentVideo)
    {
        VideoPlaybackBehaviour[] videos = (VideoPlaybackBehaviour[])
                FindObjectsOfType(typeof(VideoPlaybackBehaviour));
 
        foreach (VideoPlaybackBehaviour video in videos)
        {
            if (video != currentVideo)
            {
                if (video.CurrentState == VideoPlayerHelper.MediaState.PLAYING)
                {
                    video.VideoPlayer.Pause();
                }
            }
        }
    }
 
    #endregion // PRIVATE_METHODS
 
 
 
    #region PUBLIC_METHODS
 
    /// <summary>
    /// One-time check for the Instructional Screen
    /// </summary>
    public bool CheckWentToFullScreen()
    {
        bool result = mWentToFullScreen;
        mWentToFullScreen = false;
        return result;
    }
 
    #endregion // PUBLIC_METHODS
}
unity+高通vuforia开发增强现实(AR)教程(三) (勘误)_第3张图片
unity+高通vuforia开发增强现实(AR)教程(三) (勘误)_第4张图片
手机判别图片后点击播放键,开始播放视频,大概就是上图这个样子。

你可能感兴趣的:(unity,高通,ar,增强现实,vuforia)