InfraredScreenPositioningView.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LineUI;
  5. using UnityEngine.UI;
  6. using System.Linq;
  7. using ZIM;
  8. using o0;
  9. using Color = UnityEngine.Color;
  10. using InfraredManager;
  11. using Org.BouncyCastle.Asn1.Crmf;
  12. using ZIM.Unity;
  13. public class LinePosition
  14. {
  15. public int index;
  16. public List<Vector3> pos;
  17. }
  18. public class InfraredScreenPositioningView : JCUnityLib.ViewBase
  19. {
  20. [SerializeField]
  21. RectTransform canvasRectTransform;
  22. [SerializeField]
  23. RectTransform draggableParent;
  24. [SerializeField]
  25. RectTransform pointsParent;
  26. [SerializeField]
  27. RectTransform pos1;
  28. [SerializeField]
  29. RectTransform pos2;
  30. [SerializeField]
  31. RectTransform pos3;
  32. [SerializeField]
  33. RectTransform pos4;
  34. //画线时候的点偏移量
  35. float offset = 10;
  36. [SerializeField]
  37. Line line;
  38. List<LinePosition> oldLinePosition;
  39. Vector3 beginPos;
  40. Vector3 endPos;
  41. //相机感光部分
  42. [SerializeField]
  43. Slider slider;
  44. [SerializeField]
  45. RawImage rawImage;
  46. [SerializeField]
  47. RectTransform crosshair;
  48. [SerializeField]
  49. GameObject textTip1;
  50. [SerializeField]
  51. GameObject textTip2;
  52. [SerializeField]
  53. Button btnAuto;
  54. [SerializeField]
  55. Button btnHandMovement;
  56. bool bAuto = true;
  57. [SerializeField] Color normalColor = Color.white;
  58. [SerializeField] Color highlightedColor = Color.green;
  59. [SerializeField] Color normalTextColor = Color.black;
  60. [SerializeField] Color highlightedTextColor = Color.white;
  61. int DefaultResolutionIndex;
  62. private void Awake()
  63. {
  64. offset = line.MyThickness;
  65. }
  66. void Start()
  67. {
  68. textTip1.SetActive(true);
  69. //设置btnAuto 高亮
  70. OnButtonClick(btnAuto);
  71. textTip2.SetActive(false);
  72. //手动不高亮
  73. //ResetButton(btnHandMovement);
  74. //quadUnityVectorList.Clear();
  75. //quadUnityVectorList.Add(new Vector2(16.39f, 35.91f));
  76. //quadUnityVectorList.Add(new Vector2(233.35f, 51.08f));
  77. //quadUnityVectorList.Add(new Vector2(94.15f, 219.11f));
  78. //quadUnityVectorList.Add(new Vector2(243.12f, 237.40f));
  79. Debug.Log("quadUnityVectorList count:" + ScreenLocate.quadUnityVectorList.Count);
  80. if (ScreenLocate.quadUnityVectorList.Count == 0)
  81. {
  82. Debug.Log("GetLocalPos");
  83. GetLocalPos();
  84. }
  85. else
  86. {
  87. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  88. Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
  89. //自动识别时候四个点
  90. pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  91. pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  92. pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  93. pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  94. }
  95. //记录操作的位置信息
  96. oldLinePosition = new List<LinePosition>();
  97. SetLinePos();
  98. //相机感光度
  99. if (InfraredDemo._ins)
  100. {
  101. slider.onValueChanged.AddListener((value) =>
  102. {
  103. InfraredDemo._ins.onSliderEvent(value);
  104. });
  105. InfraredDemo._ins.onSetSliderValue(slider);
  106. offset = line.MyThickness = InfraredDemo._ins.lineWidth.Get();
  107. }
  108. else {
  109. //编辑器使用
  110. if (Application.isEditor || Application.platform == RuntimePlatform.WindowsPlayer) {
  111. slider.onValueChanged.AddListener((value) =>
  112. {
  113. double originalMin = 0.0;
  114. double originalMax = 10.0;
  115. double targetMin = -1;
  116. double targetMax = 1;
  117. // 计算转换后的值
  118. double result = InfraredDemo.MapValue(value, originalMin, originalMax, targetMin, targetMax);
  119. //int _current = (int)(result);
  120. Debug.Log("_current:" + value + " , result:" + result);
  121. ScreenLocate.Main.pcContrast = (float)result;
  122. });
  123. float v = ScreenLocate.Main.pcContrast;
  124. // 目标区间 [0, 10] 的边界值
  125. double targetMin = 0.0;
  126. double targetMax = 10.0;
  127. double originalMin = -1;
  128. double originalMax = 1;
  129. // 计算转换后的值
  130. double v2 = InfraredDemo.MapValue(v, originalMin, originalMax, targetMin, targetMax);
  131. Debug.Log("PC获取相机的感光度:" + v + " = " + v2);
  132. slider.SetValueWithoutNotify((float)v2);
  133. }
  134. }
  135. //if (ScreenLocate.Main.getUVCTexture)
  136. //{
  137. // //渲染相机画面
  138. // rawImage.texture = ScreenLocate.Main.getUVCTexture;
  139. // //Debug.Log("rawImage.texture:" + rawImage.texture.name);
  140. //}
  141. //slider.onValueChanged.AddListener((value) => {
  142. // //onSliderEvent(value);
  143. // InfraredDemo._ins.SetBrightness(value);
  144. //});
  145. //slider.value = InfraredDemo._ins.brightness.Get();
  146. //修改分辨率。是否清晰一点?
  147. //DefaultResolutionIndex = InfraredDemo._ins?.ResolutionIndex ?? 0;
  148. //Debug.Log("[InfraredScreenPositioningView]开始记录进入时候的分辨率:" + DefaultResolutionIndex);
  149. //InfraredDemo._ins?.SetResolutionNew(ScreenLocate.Main.HighScreenLocateResolutionIndex);
  150. }
  151. private void OnDestroy()
  152. {
  153. //修改回进入手动调节页面时候的分辨率
  154. //InfraredDemo._ins?.SetResolutionNew(DefaultResolutionIndex);
  155. }
  156. public RawImage Bg => rawImage;
  157. void Update()
  158. {
  159. if (enterFromZimWebCamera && ScreenLocate.Main.DebugOnEditorWin) {
  160. // ZimWebCamera场景测试
  161. rawImage.texture = ScreenLocate.Main.outputRawImages[6].texture;
  162. if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
  163. {
  164. // 检测到光点
  165. var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
  166. crosshair.gameObject.SetActive(true);
  167. crosshair.anchoredPosition = posInCanvas;
  168. }
  169. else
  170. crosshair.gameObject.SetActive(false);
  171. }
  172. else if (InfraredDemo.running)
  173. {
  174. //渲染相机画面
  175. Texture texture = InfraredDemo.infraredCameraHelper.GetCameraTexture();
  176. if (rawImage.texture == null || texture.GetNativeTexturePtr() != rawImage.texture.GetNativeTexturePtr())
  177. rawImage.texture = texture;
  178. //rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
  179. }
  180. }
  181. public void OnClick_Back()
  182. {
  183. AudioMgr.ins.PlayBtn();
  184. ViewMgr.Instance.DestroyView<InfraredScreenPositioningView>();
  185. }
  186. /// <summary>
  187. /// 自动识别
  188. /// </summary>
  189. public void OnClick_Auto()
  190. {
  191. bAuto = true;
  192. textTip1.SetActive(false);
  193. textTip2.SetActive(true);
  194. ResetButton(btnAuto);
  195. if (enterFromZimWebCamera)
  196. {
  197. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  198. _screenLocate.EnterScreenLocateManualAuto();
  199. return;
  200. }
  201. if (InfraredDemo.running)
  202. {
  203. InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
  204. }
  205. }
  206. /// <summary>
  207. /// 切换成手动方式
  208. /// </summary>
  209. public void onHandMovement()
  210. {
  211. bAuto = false;
  212. draggableParent.gameObject.SetActive(true);
  213. pointsParent.gameObject.SetActive(false);
  214. }
  215. /// <summary>
  216. /// 自动识别后同步
  217. /// </summary>
  218. public void SyncScreenPosition()
  219. {
  220. //Debug.Log("quadUnityVectorList[i]:" + quadUnityVectorList[i]);
  221. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  222. Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
  223. //自动识别时候四个点
  224. pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  225. pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  226. pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  227. pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  228. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  229. //自动识别后隐藏手动按钮
  230. draggableParent.gameObject.SetActive(false);
  231. pointsParent.gameObject.SetActive(true);
  232. if (ScreenLocate.Main) {
  233. ZIM.Unity.QuadrilateralInCamera screen = ScreenLocate.Main.getScreenIdentification.Screen.QuadInCamera;
  234. //设置points点
  235. for (int i = 0; i < 4; i++)
  236. {
  237. RectTransform t = pointsParent.GetChild(i) as RectTransform;
  238. t.anchoredPosition = screen.Quad[i].pixelToLocalPosition_AnchorCenter(screen.CameraSize, pointsParent.rect);
  239. }
  240. }
  241. }
  242. #region 绘制线段部分
  243. //点击拖拽的开始位置
  244. public void onBeginPos(int index, Vector3 pos)
  245. {
  246. Debug.Log("pos begin: " + pos);
  247. beginPos = pos;
  248. }
  249. public void onDragPos(int index, Vector3 pos)
  250. {
  251. //设置线段的点
  252. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  253. }
  254. //点击拖拽的结束位置
  255. public void onEndPos(int index, Vector3 pos)
  256. {
  257. Debug.Log("pos end: " + pos);
  258. endPos = pos;
  259. if (beginPos == endPos) return;
  260. //Debug.Log(index+",最后的点:" + pos);
  261. //再记录一次最后的点
  262. SetLinePos();
  263. }
  264. //同步设置图片对应的位置到line
  265. public void SetLinePos()
  266. {
  267. //记录一个操作点的操作位置
  268. AddOldLinePosition();
  269. //设置线段的点
  270. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  271. }
  272. void AddOldLinePosition()
  273. {
  274. Vector3[] v = new Vector3[4];
  275. pos1.GetWorldCorners(v);
  276. Vector3[] v1 = new Vector3[4];
  277. pos1.GetLocalCorners(v1);
  278. //for (int i = 0; i < 4; i++)
  279. //{
  280. // Debug.Log(i + " =1= " + v[i]);
  281. // Debug.Log(i + " =2= " + v1[i]);
  282. //}
  283. //记录一个操作点的操作位置
  284. List<Vector3> screenPositions = new List<Vector3>();
  285. screenPositions.Add(pos1.localPosition);
  286. screenPositions.Add(pos2.localPosition);
  287. screenPositions.Add(pos3.localPosition);
  288. screenPositions.Add(pos4.localPosition);
  289. LinePosition linePosition = new LinePosition();
  290. linePosition.index = oldLinePosition.Count;
  291. linePosition.pos = screenPositions;
  292. oldLinePosition.Add(linePosition);
  293. }
  294. //转换绘制线段的点
  295. List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
  296. {
  297. List<Vector2> _screenPositions = new List<Vector2>();
  298. //点为负数的增大 offset,正数减少 offset
  299. //Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.5f + offset, _pos1.y - pos1.rect.height * 0.5f + offset);
  300. //Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.5f - offset, _pos2.y - pos2.rect.height * 0.5f + offset);
  301. //Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.5f - offset, _pos3.y + pos3.rect.height * 0.5f - offset);
  302. //Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.5f + offset, _pos4.y + pos4.rect.height * 0.5f - offset);
  303. Vector2 startPos1 = new Vector2(_pos1.x + offset, _pos1.y + offset);
  304. Vector2 startPos2 = new Vector2(_pos2.x - offset, _pos2.y + offset);
  305. Vector2 startPos3 = new Vector2(_pos3.x - offset, _pos3.y - offset);
  306. Vector2 startPos4 = new Vector2(_pos4.x + offset, _pos4.y - offset);
  307. _screenPositions.Add(startPos1);
  308. _screenPositions.Add(startPos2);
  309. _screenPositions.Add(startPos3);
  310. _screenPositions.Add(startPos4);
  311. return _screenPositions;
  312. }
  313. void SetRectanglePoints(List<Vector2> screenPositions)
  314. {
  315. line.SetLine(screenPositions);
  316. }
  317. //撤回上一个元素
  318. public void onRecall()
  319. {
  320. // 获取并删除最后一个元素,并且保留一个元素
  321. if (oldLinePosition.Count > 1) // 确保列表不为空
  322. {
  323. // 获取回退的那个元素点
  324. LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
  325. // 获取最后一个元素
  326. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  327. //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
  328. oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
  329. pos1.localPosition = lastElement_second.pos[0];
  330. pos2.localPosition = lastElement_second.pos[1];
  331. pos3.localPosition = lastElement_second.pos[2];
  332. pos4.localPosition = lastElement_second.pos[3];
  333. //设置线段的点
  334. SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3]));
  335. }
  336. }
  337. //是不是从Demo界面进入该页面的
  338. public bool enterFromInfraredDemo { get; set; } = false;
  339. //是否从测试场景进入
  340. public bool enterFromZimWebCamera { get; set; } = false;
  341. //确认修改
  342. public void onConfirmation()
  343. {
  344. if (enterFromZimWebCamera)
  345. {
  346. ConfirmScreenLocateManualTest();
  347. if (oldLinePosition.Count > 1) // 确保列表不为空
  348. {
  349. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  350. oldLinePosition.Clear();
  351. oldLinePosition.Add(lastElement);
  352. }
  353. SaveLocalPos();
  354. AudioMgr.ins.PlayBtn();
  355. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  356. return;
  357. }
  358. if (!ConfirmScreenLocateManual()) return;
  359. if (oldLinePosition.Count > 1) // 确保列表不为空
  360. {
  361. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  362. oldLinePosition.Clear();
  363. oldLinePosition.Add(lastElement);
  364. }
  365. if (InfraredDemo.running)
  366. {
  367. //跳转入界面
  368. AudioMgr.ins.PlayBtn();
  369. if (!enterFromInfraredDemo)
  370. {
  371. //每次初始化重置一下引导
  372. InfraredDemo._ins.resetInfraredPlayerPrefs();
  373. //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  374. //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  375. if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
  376. {
  377. //如果是红外连接成功,记录一个tag
  378. PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
  379. }
  380. onEnterInfrared();
  381. }
  382. else {
  383. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  384. }
  385. }
  386. //存储一次节点
  387. SaveLocalPos();
  388. }
  389. //设置位置
  390. public void onReset()
  391. {
  392. oldLinePosition.Clear();
  393. // 获取屏幕的四个角的像素坐标
  394. Vector2 bottomLeft = new Vector2(0, 0);
  395. // 将屏幕像素坐标转换为 Canvas 的局部坐标
  396. Vector2 localBottomLeft;
  397. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, bottomLeft, null, out localBottomLeft);
  398. // 打印结果
  399. Debug.Log("Local Bottom Left: " + localBottomLeft);
  400. //int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x) - 120), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y) - 77);
  401. int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x)), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y));
  402. pos1.anchoredPosition = new Vector3(-_x, -_y, 0); //Bottom Left
  403. pos2.anchoredPosition = new Vector3(_x, -_y, 0); //Bottom Right
  404. pos3.anchoredPosition = new Vector3(_x, _y, 0); //Top Right
  405. pos4.anchoredPosition = new Vector3(-_x, _y, 0); //Top Left
  406. //pos1.anchoredPosition = new Vector3(_x, _y, 0);
  407. //pos2.anchoredPosition = new Vector3(-_x, _y, 0);
  408. //pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
  409. //pos4.anchoredPosition = new Vector3(_x, -_y, 0);
  410. //设置一次位置
  411. SetLinePos();
  412. }
  413. #endregion
  414. List<Vector2> _locatePointList = new();
  415. float _texWidth;
  416. float _texHeight;
  417. void RecordLocatePoint(RectTransform p, Vector2 pivot)
  418. {
  419. Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
  420. pos.x = Mathf.Clamp01(pos.x / Screen.width);
  421. pos.y = Mathf.Clamp01(pos.y / Screen.height);
  422. _locatePointList.Add(pos);
  423. }
  424. bool ConfirmScreenLocateManual()
  425. {
  426. if (InfraredDemo.running)
  427. {
  428. //渲染截图
  429. Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual();
  430. if (texture2D == null)
  431. {
  432. Debug.Log("EnterScreenLocateManual = null");
  433. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null);
  434. return false;
  435. }
  436. _locatePointList.Clear();
  437. _texWidth = texture2D.width;
  438. _texHeight = texture2D.height;
  439. RecordLocatePoint(pos1, new Vector2(0, 0));
  440. RecordLocatePoint(pos2, new Vector2(1, 0));
  441. RecordLocatePoint(pos3, new Vector2(1, 1));
  442. RecordLocatePoint(pos4, new Vector2(0, 1));
  443. //RecordLocatePoint(pos1, new Vector2(0.5f, 0.5f));
  444. //RecordLocatePoint(pos2, new Vector2(0.5f, 0.5f));
  445. //RecordLocatePoint(pos3, new Vector2(0.5f, 0.5f));
  446. //RecordLocatePoint(pos4, new Vector2(0.5f, 0.5f));
  447. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
  448. FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, 1, 1);
  449. //setPointsLocation(_locatePointList, pointsParent.gameObject, !bAuto);
  450. //同步数据
  451. ScreenLocate.quadUnityVectorList.Clear();
  452. ScreenLocate.quadUnityVectorList.Add(_locatePointList[0]);
  453. ScreenLocate.quadUnityVectorList.Add(_locatePointList[1]);
  454. //两个点切换,顺序不一样
  455. ScreenLocate.quadUnityVectorList.Add(_locatePointList[3]);
  456. ScreenLocate.quadUnityVectorList.Add(_locatePointList[2]);
  457. ScreenLocate.SaveScreenLocateVectorList();
  458. return true;
  459. }
  460. return false;
  461. }
  462. void ConfirmScreenLocateManualTest()
  463. {
  464. //渲染截图
  465. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  466. Texture2D texture2D = _screenLocate.EnterScreenLocateManual();
  467. if (texture2D == null)
  468. {
  469. _screenLocate.QuitScreenLocateManual(null);
  470. return;
  471. }
  472. _locatePointList.Clear();
  473. _texWidth = texture2D.width;
  474. _texHeight = texture2D.height;
  475. RecordLocatePoint(pos1, new Vector2(0, 0));
  476. RecordLocatePoint(pos2, new Vector2(1, 0));
  477. RecordLocatePoint(pos3, new Vector2(1, 1));
  478. RecordLocatePoint(pos4, new Vector2(0, 1));
  479. _screenLocate.QuitScreenLocateManual(_locatePointList);
  480. //FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
  481. setPointsLocation(_locatePointList,GameObject.Find("WebCameraView/CameraImage0/ScreenQuad"));
  482. setPointsLocation(_locatePointList,pointsParent.gameObject,!bAuto);
  483. //同步数据
  484. ScreenLocate.quadUnityVectorList.Clear();
  485. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[0].x, _locatePointList[0].y));
  486. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[1].x, _locatePointList[1].y));
  487. //两个点切换,顺序不一样
  488. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[3].x, _locatePointList[3].y));
  489. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[2].x, _locatePointList[2].y));
  490. ScreenLocate.SaveScreenLocateVectorList();
  491. }
  492. void setPointsLocation(List<Vector2> targetList ,GameObject pointsTF2,bool active = true) {
  493. //GameObject pointsTF2 = GameObject.Find("WebCameraView/CameraImage0/ScreenQuad");
  494. pointsTF2.SetActive(active);
  495. if (pointsTF2.transform.childCount == targetList.Count)
  496. {
  497. Vector2 texSize = new Vector2(_texWidth, _texHeight);
  498. for (int i = 0; i < pointsTF2.transform.childCount; i++)
  499. {
  500. Transform pointTF = pointsTF2.transform.GetChild(i);
  501. Vector2 pos = targetList[i];
  502. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(Vector2.one, pointsTF2.GetComponent<RectTransform>().rect);
  503. pointTF.gameObject.SetActive(true);
  504. }
  505. }
  506. }
  507. void SaveLocalPos()
  508. {
  509. List<Vector3> screenPositions = new List<Vector3>();
  510. screenPositions.Add(pos1.anchoredPosition);
  511. screenPositions.Add(pos2.anchoredPosition);
  512. screenPositions.Add(pos3.anchoredPosition);
  513. screenPositions.Add(pos4.anchoredPosition);
  514. string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y}")); //,{v.z}
  515. Debug.Log("Local UI Position: " + saveStr);
  516. PlayerPrefs.SetString("ScreenPositioningView", saveStr);
  517. }
  518. void GetLocalPos()
  519. {
  520. string posListStr = PlayerPrefs.GetString("ScreenPositioningView", "");
  521. if (!string.IsNullOrWhiteSpace(posListStr))
  522. {
  523. List<Vector2> posList = posListStr.Split(';')
  524. .Select(s =>
  525. {
  526. string[] parts = s.Split(',');
  527. return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
  528. })
  529. .ToList();
  530. pos1.anchoredPosition = posList[0];
  531. pos2.anchoredPosition = posList[1];
  532. pos3.anchoredPosition = posList[2];
  533. pos4.anchoredPosition = posList[3];
  534. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  535. }
  536. }
  537. #region 按钮颜色切换
  538. private void OnButtonClick(Button button)
  539. {
  540. // 切换按钮颜色
  541. ColorBlock colors = button.colors;
  542. colors.normalColor = highlightedColor;
  543. colors.highlightedColor = highlightedColor;
  544. button.colors = colors;
  545. // 切换字体颜色
  546. Text buttonText = button.GetComponentInChildren<Text>();
  547. buttonText.color = highlightedTextColor;
  548. }
  549. private void ResetButton(Button button)
  550. {
  551. // 重置按钮颜色
  552. ColorBlock colors = button.colors;
  553. colors.normalColor = normalColor;
  554. colors.highlightedColor = normalColor;
  555. button.colors = colors;
  556. // 重置字体颜色
  557. Text buttonText = button.GetComponentInChildren<Text>();
  558. buttonText.color = normalTextColor;
  559. }
  560. #endregion
  561. #region 标定完成之后进入游戏界面
  562. void onEnterInfrared()
  563. {
  564. //添加进入射箭场景
  565. if (PlayerPrefs.GetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 0) == 0)
  566. {
  567. Debug.Log("-----进入射箭场景!");
  568. NewUserGuiderManager newUserGuiderManager = FindObjectOfType<NewUserGuiderManager>();
  569. newUserGuiderManager.curConfigKey = "开始-红外调整";
  570. newUserGuiderManager.isNewModule = AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.HOUYIPRO || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.Gun || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.ARTEMISPRO;
  571. //进入射箭场景
  572. GlobalData.pkMatchType = PKMatchType.None;
  573. GameMgr.gameType = 1;
  574. //射一箭回到连接页面,Device.view
  575. GameMgr.bNavBack = true;
  576. GameMgr.bShowDistance = false;
  577. AimHandler.ins.bInitOne = true;
  578. //关闭计时器
  579. GameMgr.turnOffTimer = true;
  580. UnityEngine.SceneManagement.SceneManager.LoadScene(
  581. "Game", UnityEngine.SceneManagement.LoadSceneMode.Single);
  582. PlayerPrefs.SetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 1);
  583. }
  584. else
  585. {
  586. Debug.Log("-----跳转回连接界面!");
  587. //如不是第一次,则应该跳转回连接界面
  588. ViewMgr.Instance.DestroyView<SmartArcheryView>();
  589. ViewManager2.HideView(ViewManager2.Path_ConnectGuidanceView);
  590. }
  591. }
  592. #endregion
  593. }