123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using ShadowKit.Action;
- public class VufoiraImageTarget : DefaultTrackableEventHandler
- {
- [SerializeField]
- private Transform ObjTrans;//这个是需要根据图片位置更新自己位置的UI
- [SerializeField]
- private Transform Point;//相对的节点位置
- private Vector3 DefaultPosition;//默认的起始位置
- private Quaternion DefaultQuaternion;//默认的角度
- private bool is_found;//
- private bool is_tracking;// true 表示需要追踪识别
- protected override void Start()
- {
- base.Start();
- DefaultPosition = ObjTrans.position;//先记录下来初始的位置
- DefaultQuaternion = ObjTrans.rotation;//先记录下来初始的朝向
- }
- protected override void OnDestroy()
- {
- base.OnDestroy();
- }
- protected override void OnTrackingFound()
- {
- base.OnTrackingFound();
- is_found = true;
- }
- protected override void OnTrackingLost()
- {
- base.OnTrackingLost();
- is_found = false;
- }
- private void LateUpdate()
- {
- if(is_found)
- {
- if(is_tracking)
- {
- RefreshPos();
- is_tracking = false;//只需要定位一次
- }
- }
- }
- private Vector3 headPos;
- private void RefreshPos()
- {
- ObjTrans.position = Point.transform.position;
- ObjTrans.localScale = Vector3.one * 0.5f;
- headPos = FollowTarget.position;
- headPos.y = ObjTrans.position.y;
- ObjTrans.rotation = Quaternion.LookRotation(headPos - ObjTrans.position);
- }
- public void ResetPos()
- {
- CDebug.Log("关闭识别后的重置位置 ResetPos");
- ObjTrans.localScale = Vector3.one;
- ObjTrans.rotation = DefaultQuaternion;
- ObjTrans.position = DefaultPosition;
- SvrPlugin.Instance.RecenterTracking();
- }
- public void SetTrack(bool isTrack)
- {
- is_tracking = isTrack;
- if(!isTrack)
- {
- ResetPos();
- }
- }
- private Transform FollowTarget
- {
- get
- {
- if (Camera.main != null)
- {
- //return Camera.main.transform;
- }
- if (SvrManager.Instance.head != null)
- {
- return SvrManager.Instance.head.transform;
- }
- return null;
- }
- }
- }
|