iT邦幫忙

2021 iThome 鐵人賽

DAY 14
0
自我挑戰組

從0開始的UnityAR手機遊戲開發系列 第 14

[第十四天]從0開始的UnityAR手機遊戲開發-如何在辨識圖卡時撥放影片03

  • 分享至 

  • xImage
  •  

在Project新增Create→C# Script取名為TrackableEvent
https://ithelp.ithome.com.tw/upload/images/20210928/20140569D8L8Q6iiyc.png

撰寫程式碼

using UnityEngine;
using UnityEngine.Video;
using Vuforia;

public class TrackableEvent : MonoBehaviour, ITrackableEventHandler
{
    #region PROTECTED_MEMBER_VARIABLES

    protected TrackableBehaviour mTrackableBehaviour;
    protected TrackableBehaviour.Status m_PreviousStatus;
    protected TrackableBehaviour.Status m_NewStatus;

    #endregion // PROTECTED_MEMBER_VARIABLES
    //這裡放入要撥放的影片
    [Header("範例影片")]
    public VideoPlayer VideoPlayer;

    #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
    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
    //OnTrackingFound是照到辨識圖所觸發的事件
    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;
       //影片撥放
        VideoPlayer.Play();
    }

    //OnTrackingLost是沒照到辨識圖所觸發的事件
    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;
       //影片停止撥放
        VideoPlayer.Stop();
    }

    #endregion // PROTECTED_METHODS
}

在DefaultTrackableEventHandler點擊右鍵Remove Component刪除
https://ithelp.ithome.com.tw/upload/images/20210928/20140569mb1tv7sNc6.png

將剛剛所撰寫的TrackableEvent拖曳給ImageTarget,打開ImageTarget的Inspector把Hierarchy的VideoPlayer拖曳到TrackableEvent的範例影片欄位
https://ithelp.ithome.com.tw/upload/images/20210928/20140569H28UrKVUF3.png

按下測試就可以看到影片撥放囉
https://ithelp.ithome.com.tw/upload/images/20210928/20140569knoLyaLSmy.png


下一章節會教導大家如何切換場景(需要撰寫程式碼)。


上一篇
[第十三天]從0開始的UnityAR手機遊戲開發-如何在辨識圖卡時撥放影片02
下一篇
[第十五天]從0開始的UnityAR手機遊戲開發-如何切換場景01
系列文
從0開始的UnityAR手機遊戲開發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言