VufoiraImageTarget.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ShadowKit.Action;
  5. public class VufoiraImageTarget : DefaultTrackableEventHandler
  6. {
  7. [SerializeField]
  8. private Transform ObjTrans;//这个是需要根据图片位置更新自己位置的UI
  9. [SerializeField]
  10. private Transform Point;//相对的节点位置
  11. private Vector3 DefaultPosition;//默认的起始位置
  12. private Quaternion DefaultQuaternion;//默认的角度
  13. private bool is_found;//
  14. private bool is_tracking;// true 表示需要追踪识别
  15. protected override void Start()
  16. {
  17. base.Start();
  18. DefaultPosition = ObjTrans.position;//先记录下来初始的位置
  19. DefaultQuaternion = ObjTrans.rotation;//先记录下来初始的朝向
  20. }
  21. protected override void OnDestroy()
  22. {
  23. base.OnDestroy();
  24. }
  25. protected override void OnTrackingFound()
  26. {
  27. base.OnTrackingFound();
  28. is_found = true;
  29. }
  30. protected override void OnTrackingLost()
  31. {
  32. base.OnTrackingLost();
  33. is_found = false;
  34. }
  35. private void LateUpdate()
  36. {
  37. if(is_found)
  38. {
  39. if(is_tracking)
  40. {
  41. RefreshPos();
  42. is_tracking = false;//只需要定位一次
  43. }
  44. }
  45. }
  46. private Vector3 headPos;
  47. private void RefreshPos()
  48. {
  49. ObjTrans.position = Point.transform.position;
  50. ObjTrans.localScale = Vector3.one * 0.5f;
  51. headPos = FollowTarget.position;
  52. headPos.y = ObjTrans.position.y;
  53. ObjTrans.rotation = Quaternion.LookRotation(headPos - ObjTrans.position);
  54. }
  55. public void ResetPos()
  56. {
  57. CDebug.Log("关闭识别后的重置位置 ResetPos");
  58. ObjTrans.localScale = Vector3.one;
  59. ObjTrans.rotation = DefaultQuaternion;
  60. ObjTrans.position = DefaultPosition;
  61. SvrPlugin.Instance.RecenterTracking();
  62. }
  63. public void SetTrack(bool isTrack)
  64. {
  65. is_tracking = isTrack;
  66. if(!isTrack)
  67. {
  68. ResetPos();
  69. }
  70. }
  71. private Transform FollowTarget
  72. {
  73. get
  74. {
  75. if (Camera.main != null)
  76. {
  77. //return Camera.main.transform;
  78. }
  79. if (SvrManager.Instance.head != null)
  80. {
  81. return SvrManager.Instance.head.transform;
  82. }
  83. return null;
  84. }
  85. }
  86. }