PointMarker.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. using System;
  5. using ZIM.Unity;
  6. using System.Linq;
  7. public class PointMarker : MonoBehaviour
  8. {
  9. public Text instructionText; // 用于显示提示文字
  10. public Button undoButton; // 撤回按钮
  11. public Button completeButton; // 完成按钮
  12. public bool bComplete = false;// 是否完成
  13. public GameObject markerPrefab; // 用于表示标记的UI元素,可能是一个小图标(如枪)
  14. // 新增父对象,用于存放标记的物体
  15. public Transform markerParent; // 标记物体的父对象(例如Canvas中的一个空物体)
  16. public Image[] hintImages; // 依次存放左上、右上、右下、左下四个提示图片
  17. private List<Vector2> markedPoints = new List<Vector2>();
  18. private string[] directions = { "左上", "右上", "右下", "左下" };
  19. private int currentPointIndex = 0; // 当前需要标记的点的索引
  20. private List<GameObject> markerObjects = new List<GameObject>(); // 记录标记物体
  21. [SerializeField] InfraredScreenPositioningView infraredScreenPositioningView;
  22. void Start()
  23. {
  24. if (AimHandler.ins) AimHandler.ins.OnCrossBtnEvent += OnRecordInfrared;
  25. UpdateInstruction();
  26. undoButton.onClick.AddListener(UndoLastPoint);
  27. completeButton.onClick.AddListener(CompletePoint); // 初始化时隐藏完成按钮
  28. bComplete = false;
  29. // 初始化时,显示第一个提示图片,其余隐藏
  30. ShowHintImage(0);
  31. }
  32. void OnDestroy()
  33. {
  34. if (AimHandler.ins) AimHandler.ins.OnCrossBtnEvent -= OnRecordInfrared;
  35. }
  36. private float lastClickTime = 0f;
  37. private float doubleClickThreshold = 0.3f; // 双击间隔时间,单位:秒
  38. void Update()
  39. {
  40. if (Input.GetMouseButtonDown(0)) // 检测左键按下
  41. {
  42. float currentTime = Time.time;
  43. if (currentTime - lastClickTime <= doubleClickThreshold)
  44. {
  45. // 双击检测成功,执行触发逻辑
  46. if (currentPointIndex >= 4) return;
  47. var mouse = Input.mousePosition;
  48. var u = mouse.x / Screen.width;
  49. var v = mouse.y / Screen.height;
  50. u = Math.Clamp(u, 0, 1);
  51. v = Math.Clamp(v, 0, 1);
  52. Vector2 pos = new Vector2(u * ScreenLocate.Main.getUVCTexture.width, v * ScreenLocate.Main.getUVCTexture.height);
  53. markedPoints.Add(pos);
  54. // 设置标记的位置
  55. MarkPoint(new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), markerParent.GetComponent<RectTransform>().rect));
  56. }
  57. lastClickTime = currentTime; // 更新上一次点击时间
  58. }
  59. //#if UNITY_EDITOR
  60. //if (Input.GetKeyDown(KeyCode.Alpha6) || Input.GetMouseButtonDown(1))
  61. //{
  62. // //Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  63. // //MarkPoint(mousePosition);
  64. // // OnRecordInfrared();
  65. // if (currentPointIndex >= 4) return;
  66. // var mouse = Input.mousePosition;
  67. // var u = mouse.x / Screen.width;
  68. // var v = mouse.y / Screen.height;
  69. // u = Math.Clamp(u, 0, 1);
  70. // v = Math.Clamp(v, 0, 1);
  71. // Vector2 pos = new Vector2(u * ScreenLocate.Main.getUVCTexture.width, v * ScreenLocate.Main.getUVCTexture.height);
  72. // markedPoints.Add(pos);
  73. // // 设置标记的位置
  74. // MarkPoint(new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), markerParent.GetComponent<RectTransform>().rect));
  75. //}
  76. //#endif
  77. }
  78. /// <summary>
  79. /// 记录红外点
  80. /// </summary>
  81. public void OnRecordInfrared()
  82. {
  83. if (currentPointIndex >= 4) return;
  84. var location = ScreenLocate.Main.infraredSpotBuffer.FirstOrDefault()?.CameraLocation;
  85. if (location != null)
  86. {
  87. Vector2 localPosition = location.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, infraredScreenPositioningView.Bg.rectTransform.rect);
  88. markedPoints.Add(localPosition);
  89. MarkPoint(localPosition);
  90. }
  91. }
  92. void MarkPoint(Vector2 point)
  93. {
  94. //markedPoints.Add(new Vector2(point.x * ScreenLocate.Main.getUVCTexture.width, point.y * ScreenLocate.Main.getUVCTexture.height));
  95. // 创建标记物体,并将其作为markerParent的子对象
  96. GameObject marker = Instantiate(markerPrefab, markerParent);
  97. marker.SetActive(true);
  98. marker.transform.localPosition = point;
  99. markerObjects.Add(marker);
  100. // 隐藏当前的提示图片,显示下一个提示图片
  101. currentPointIndex++;
  102. ShowHintImage(currentPointIndex);
  103. UpdateInstruction();
  104. // 如果已经标记了四个点,显示完成按钮
  105. if (currentPointIndex == 4)
  106. {
  107. instructionText.text = "标记完成!";
  108. bComplete = true;
  109. }
  110. }
  111. void UndoLastPoint()
  112. {
  113. if (markedPoints.Count > 0)
  114. {
  115. // 移除最后一个标记的点
  116. markedPoints.RemoveAt(markedPoints.Count - 1);
  117. // 删除最后一个标记的物体
  118. Destroy(markerObjects[markerObjects.Count - 1]);
  119. markerObjects.RemoveAt(markerObjects.Count - 1);
  120. // 回退到上一个提示图片
  121. currentPointIndex--;
  122. ShowHintImage(currentPointIndex);
  123. UpdateInstruction();
  124. }
  125. // 隐藏完成按钮
  126. bComplete = false;
  127. }
  128. void UpdateInstruction()
  129. {
  130. if (currentPointIndex < 4)
  131. {
  132. instructionText.text = "请标记: " + directions[currentPointIndex] + "角的点";
  133. }
  134. }
  135. // 显示当前需要的提示图片,并隐藏其余提示图片
  136. void ShowHintImage(int index)
  137. {
  138. for (int i = 0; i < hintImages.Length; i++)
  139. {
  140. if (i == index)
  141. hintImages[i].gameObject.SetActive(true);
  142. else
  143. hintImages[i].gameObject.SetActive(false);
  144. }
  145. }
  146. void CompletePoint() {
  147. if (!bComplete) {
  148. Debug.Log("未完成");
  149. //todo,加提示
  150. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("Incomplete"));
  151. return;
  152. }
  153. //完成,下一步
  154. for (int i = 0; i < 4; i++)
  155. {
  156. int index = i;
  157. infraredScreenPositioningView.onManualNewPos(3 - index, markerObjects[index].transform.localPosition);
  158. }
  159. infraredScreenPositioningView.onManualToAutomatic();
  160. ////清空数据
  161. //currentPointIndex = 0;
  162. ////清空生成的数据
  163. //markerObjects.Clear();
  164. //foreach (Transform i in markerParent.transform)
  165. // Destroy(i.gameObject);
  166. }
  167. }