1、生成角色,在OnTrackingFound()
OnTrackingFound() : 表示识别到目标图片
- 生成角色AIXI:
aixi = GameObject.Instantiate(AIXIPrefab,transform.localPosition, transform.localRotation);
aixi.transform.SetParent(this.transform);
- 生成特效,并在5S后销毁:
fx1 = GameObject.Instantiate(RFX1Prefab1,transform.position, transform.localRotation);
fx2 = GameObject.Instantiate(RFX1Prefab2,transform.position, transform.localRotation);
fx1.transform.position = this.transform.position;
fx2.transform.position = this.transform.position;
fx1.transform.SetParent(this.transform);
fx2.transform.SetParent(this.transform);
Destroy(fx1, 5f);
Destroy(fx2, 5f);
- 播放特效结束的欢迎音效:
Invoke("PlayWecomeClip", 5.0f);
- 完整代码片段:
/// <summary>
/// 识别到物体
/// </summary>
protected virtual void OnTrackingFound()
{
if (audioSource != null && !audioSource.isPlaying)
{
audioSource.Play();
}
aixi = GameObject.Instantiate(AIXIPrefab,transform.localPosition, transform.localRotation);
aixi.transform.SetParent(this.transform);
fx1 = GameObject.Instantiate(RFX1Prefab1,transform.position, transform.localRotation);
fx2 = GameObject.Instantiate(RFX1Prefab2,transform.position, transform.localRotation);
fx1.transform.position = this.transform.position;
fx2.transform.position = this.transform.position;
fx1.transform.SetParent(this.transform);
fx2.transform.SetParent(this.transform);
Destroy(fx1, 5f);
Destroy(fx2, 5f);
Invoke("PlayWecomeClip", 5.0f);
}
private void PlayWecomeClip()
{
audioSource.PlayOneShot(wecomeClip);
}
整完整脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
public class AR : MonoBehaviour, ITrackableEventHandler
{
protected TrackableBehaviour mTrackableBehaviour;
public GameObject AIXIPrefab;//模型预制
public GameObject RFX1Prefab1;//特效1预制
public GameObject RFX1Prefab2;//特效2预制
private GameObject aixi;//角色
private GameObject fx1;//特效1
private GameObject fx2;//特效2
private AudioSource audioSource;//播放源
public AudioClip wecomeClip;//欢迎音效片段
#region UNITY_MONOBEHAVIOUR_METHODS
protected virtual void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
if (audioSource == null)
{
audioSource = GetComponent<AudioSource>();
}
}
protected virtual void OnDestroy()
{
if (mTrackableBehaviour)
mTrackableBehaviour.UnregisterTrackableEventHandler(this);
}
#endregion // UNITY_MONOBEHAVIOUR_METHODS
/// <summary>
/// 状态的改变
/// </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
{
OnTrackingLost();
}
}
/// <summary>
/// 识别到物体
/// </summary>
protected virtual void OnTrackingFound()
{
if (audioSource != null && !audioSource.isPlaying)
{
audioSource.Play();
}
aixi = GameObject.Instantiate(AIXIPrefab,transform.localPosition, transform.localRotation);
aixi.transform.SetParent(this.transform);
fx1 = GameObject.Instantiate(RFX1Prefab1,transform.position, transform.localRotation);
fx2 = GameObject.Instantiate(RFX1Prefab2,transform.position, transform.localRotation);
fx1.transform.position = this.transform.position;
fx2.transform.position = this.transform.position;
fx1.transform.SetParent(this.transform);
fx2.transform.SetParent(this.transform);
Destroy(fx1, 5f);
Destroy(fx2, 5f);
Invoke("PlayWecomeClip", 5.0f);
}
private void PlayWecomeClip()
{
audioSource.PlayOneShot(wecomeClip);
}
/// <summary>
/// 丢失物体
/// </summary>
protected virtual void OnTrackingLost()
{
if (aixi != null)
{
Destroy(aixi);
}
if (fx1 != null)
{
Destroy(fx1);
}
if (fx2 != null)
{
Destroy(fx2);
}
}
}
TrackableBehaviour.Status 枚举
该枚举表示的是 设别的状态:
//
// 摘要:
// The tracking status of the TrackableBehaviour.
public enum Status
{
NOT_FOUND = -1,
//
// 摘要:
// The state of the TrackableResult is unknown
UNKNOWN = 0,
//
// 摘要:
// The state of the TrackableResult is not defined (it does not have a state)
UNDEFINED = 1,
//
// 摘要:
// The TrackableResult was detected
DETECTED = 2,
//
// 摘要:
// The TrackableResult was tracked
TRACKED = 3,
//
// 摘要:
// The TrackableResult was tracked while not being visible
EXTENDED_TRACKED = 4,
//
// 摘要:
// The TrackableResult has a pose of degraded quality (only supported for device
// pose)
DEGRADED = 5
}
- NOT_FOUND :没有识别到
- UNKNOWN: 不名的识别结果
- UNDEFINED :没有定义的状态
- DETECTED : 被识别到
- TRACKED: 被追踪到
- EXTENDED_TRACKED : 目标在不可见的情况下被追踪到
- DEGRADED:追踪处于质量下降的位置姿态
TrackableBehaviour 脚本:
- public string TrackableName { get; } : -----------识别目标的名字
- public virtual Trackable Trackable { get; } : --------在运行时创建的可跟踪的,该可跟踪行为增强了可跟踪性
- public Status CurrentStatus { get; } : -------当前的识别追踪状态
- public virtual void OnFrameIndexUpdate(int newFrameIndex) :----- 在每个渲染帧之后由StateManager触发(参数:处理过的帧的帧索引,多个渲染帧可以有相同的帧索引)
- public virtual void OnTrackerUpdate(Status newStatus) : ------由TrackerBehavior在更新后触发。
- public void RegisterTrackableEventHandler(ITrackableEventHandler trackableEventHandler):-----此方法在跟踪器上注册一个新的跟踪器事件处理程序。一旦在此框架中更新了所有可跟踪程序,就会调用这些处理程序。
- public bool UnregisterTrackableEventHandler(ITrackableEventHandler trackableEventHandler) : -----此方法卸载跟踪器事件处理程序。如果事件处理程序不存在,返回“false”