Vuforia学习之播放音频文件

  1. 创建Audio Source


    Vuforia学习之播放音频文件_第1张图片
    Audio Source
  2. Audio Source添加音频文件


    Vuforia学习之播放音频文件_第2张图片
    AudioClip
  3. 取消Image Target的Defaule Trackable Event Handler(script)选中


    Vuforia学习之播放音频文件_第3张图片
    Defaule Trackable Event Handler(script)
  4. 创建C#Script


    Vuforia学习之播放音频文件_第4张图片
    C#Script
  5. 编辑代码

using UnityEngine;
using System.Collections;  
using Vuforia; 
public class Music_Lhy : MonoBehaviour,ITrackableEventHandler {  
    #region PRIVATE_MEMBER_VARIABLES  
    public GameObject UI;  
    private TrackableBehaviour mTrackableBehaviour;  

    #endregion // PRIVATE_MEMBER_VARIABLES  

    #region UNTIY_MONOBEHAVIOUR_METHODS  

    void Start()  
    {  
        mTrackableBehaviour = GetComponent();  
        if (mTrackableBehaviour)  
        {  
            mTrackableBehaviour.RegisterTrackableEventHandler(this);  
        }  
    }  

    #endregion // UNTIY_MONOBEHAVIOUR_METHODS  
    #region PUBLIC_METHODS  
    public void OnTrackableStateChanged(  
        TrackableBehaviour.Status previousStatus,  
        TrackableBehaviour.Status newStatus)  
    {  
        if (newStatus == TrackableBehaviour.Status.DETECTED ||  
            newStatus == TrackableBehaviour.Status.TRACKED ||  
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)  
        {  
            OnTrackingFound();  
            UI.SetActive (true);  
        }  
        else  
        {  
            UI.SetActive (false);  
            OnTrackingLost();  
        }  
    }  

    #endregion // PUBLIC_METHODS  

    #region PRIVATE_METHODS  

    private void OnTrackingFound()  
    {  
        Renderer[] rendererComponents = GetComponentsInChildren(true);  
        Collider[] colliderComponents = GetComponentsInChildren(true);  

        // Enable rendering:  
        foreach (Renderer component in rendererComponents)  
        {  
            component.enabled = true;  
        }  

        // Enable colliders:  
        foreach (Collider component in colliderComponents)  
        {  
            component.enabled = true;  
        }  

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");  
    }  

    private void OnTrackingLost()  
    {  
        Renderer[] rendererComponents = GetComponentsInChildren(true);  
        Collider[] colliderComponents = GetComponentsInChildren(true);  

        // Disable rendering:  
        foreach (Renderer component in rendererComponents)  
        {  
            component.enabled = false;  
        }  

        // Disable colliders:  
        foreach (Collider component in colliderComponents)  
        {  
            component.enabled = false;  
        }  

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");  
    }  

    #endregion // PRIVATE_METHODS  
}  
  1. 把脚本拖给Image Target并选择UI文件


    选择UI
  2. 补充说明
    这里可以设置Audio Source的一些属性

    Vuforia学习之播放音频文件_第5张图片
    Audio Source属性

http://blog.csdn.net/bless2015/article/details/51046060

你可能感兴趣的:(Vuforia学习之播放音频文件)