InfraredScreenPositioningView.cs 26 KB

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