InfraredScreenPositioningView.cs 26 KB

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