InfraredScreenPositioningView.cs 22 KB

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