本文的测试环境:
- macOS Mojave 10.14.2 / MacBook Pro 2015
- Unity 2018.3.8f1
- Vuforia 8.0.10
非常简单,只需要创建一个脚本,你可以命名为CustomTrackableEventHandler
, 然后:
- 继承
DefaultTrackableEventHandler
, - 重载父类的函数
- 把
ImageTarget
上的DefaultTrackableEventHandler
换成CustomTrackableEventHandler
例子如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CustomTrackableEventHandler : DefaultTrackableEventHandler
{
protected override void OnTrackingFound()
{
var o = GameObject.CreatePrimitive(PrimitiveType.Sphere);
o.transform.SetParent(mTrackableBehaviour.transform);
o.transform.localPosition = Vector3.zero;
o.transform.localRotation = Quaternion.identity;
o.transform.localScale = Vector3.one;
}
protected override void OnTrackingLost()
{
if (transform.childCount != 0)
Destroy(transform.GetChild(0).gameObject);
}
}
你可以打开DefaultTrackableEventHandler
查看,这个类里面只有下面这点东西:
-
mTrackableBehaviour
m_PreviousStatus
m_NewStatus
-
Start
OnDestroy
OnTrackableStateChanged
-
OnTrackingFound
OnTrackingLost
根据需要重写代码就行了,有点无脑,没多大意思。
DefaultTrackableEventHandler
开头就是注释:
/// <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>
它就表示需要自定义跟踪事件处理程序的话,可以考虑继承这个类。
如果你千辛万苦找到这里才解决你的问题,那么我就要告诉你了:😂😂😂,没事翻翻源代码还是很有用的。
网友评论