码迷,mamicode.com
首页 > 其他好文 > 详细

ARLostCardShowOBJ

时间:2016-06-25 09:31:29      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

using UnityEngine;
using System.Collections;
using Vuforia;
/// <summary>
/// 自己重新默认的DefaultTrackableEventHandler脚本(当然你不重写也行,
/// 脚本的现实的效果:当摄像头拍摄到识别图时模型显示在识别图上,当摄像头离开识别图时,模型居中显示在屏幕上。
///
/// 注意:需要重写DefaultTrackableEventHandler脚本,需要引入using Vuforia命名空间,还需继承ITrackableEventHandler脚本
/// mTrackableBehaviour.TrackableName可以指定你当前识别图所在的物体名称,可以直接print
/// </summary>

public class LJTrackableEventHandler : MonoBehaviour, ITrackableEventHandler //继承ITrackableEventHandler,ITrackableEventHandler处理的是识别图的跟踪事件
{
//创建一个TrackableBehaviour的对象
private TrackableBehaviour mTrackableBehaviour;

//定义一个bool作用是控制模型是否显示或者隐藏(初始化为false,因为一开始不需要模型显示
bool ArShowBool = false;

//存放Ar相机
GameObject ARCameraObj;

// Use this for initialization
void Start ()
{
//初始化获取到Ar相机
ARCameraObj = GameObject.Find("Camera");

//初始化mTrackableBehaviour对象
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}

//处理识别图找到或者丢失事件
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("找到识别图!");

FoundOBJ();

ArShowBool = true;
/// <summary>
/// testOBJ===是指识别图下模型 ImageTarget==识别图
/// </summary>
GameObject.Find("testOBJ").transform.parent = GameObject.Find("ImageTarget").transform;
//重新指定坐标
GameObject.Find("testOBJ").transform.localPosition = new Vector3(0, 0, 0);
//重新指定旋转坐标
GameObject.Find("testOBJ").transform.eulerAngles = new Vector3(0, 0, 0);
}
else
{
Debug.Log("丢失识别图!");

LostOBJ();

if (ArShowBool == true)
{
FoundOBJ();
//如果离开识别图 模型显示在屏幕上
GameObject.Find("testOBJ").transform.parent = ARCameraObj.transform;
//重新指定旋转坐标
GameObject.Find("testOBJ").transform.localPosition = new Vector3(0, 0, 40);
//重新指定旋转坐标
GameObject.Find("testOBJ").transform.localEulerAngles = new Vector3(0, -90, 0);
}
}
}

//显示模型
private void FoundOBJ()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

//遍历模型的Renderer组件然后显示
foreach (Renderer component in rendererComponents)
{
component.enabled = true;
}

//遍历模型的Collider组件然后显示
foreach (Collider component in colliderComponents)
{
component.enabled = true;
}
}
//隐藏模型
private void LostOBJ()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

//遍历模型的Renderer组件然后隐藏
foreach (Renderer component in rendererComponents)
{
component.enabled = false;
}

//遍历模型的Collider组件然后隐藏
foreach (Collider component in colliderComponents)
{
component.enabled = false;
}
}
}

ARLostCardShowOBJ

标签:

原文地址:http://www.cnblogs.com/ZeroMurder/p/5615824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!