using System.Collections; using System.Collections.Generic; using UnityEngine; using LineUI; using UnityEngine.UI; using System.Linq; using ZIM; public class LinePosition { public int index; public List pos; } public class InfraredScreenPositioningView : JCUnityLib.ViewBase { [SerializeField] RectTransform canvasRectTransform; [SerializeField] RectTransform draggableParent; [SerializeField] RectTransform pos1; [SerializeField] RectTransform pos2; [SerializeField] RectTransform pos3; [SerializeField] RectTransform pos4; //画线时候的点偏移量 float offset = 20; [SerializeField] Line line; List oldLinePosition; Vector3 beginPos; Vector3 endPos; //相机感光部分 [SerializeField] Slider slider; [SerializeField] RawImage rawImage; private void Awake() { offset = line.MyThickness; } void Start() { //quadUnityVectorList.Clear(); //quadUnityVectorList.Add(new Vector2(16.39f, 35.91f)); //quadUnityVectorList.Add(new Vector2(233.35f, 51.08f)); //quadUnityVectorList.Add(new Vector2(94.15f, 219.11f)); //quadUnityVectorList.Add(new Vector2(243.12f, 237.40f)); Debug.Log("quadUnityVectorList count:" + ScreenLocate.quadUnityVectorList.Count); if (ScreenLocate.quadUnityVectorList.Count == 0) { Debug.Log("GetLocalPos"); GetLocalPos(); } else { Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize; Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect); //自动识别时候四个点 pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(texSize, canvasRectTransform.rect); pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(texSize, canvasRectTransform.rect); pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(texSize, canvasRectTransform.rect); pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(texSize, canvasRectTransform.rect); } //记录操作的位置信息 oldLinePosition = new List(); SetLinePos(); //相机感光度 if (InfraredDemo._ins) { slider.onValueChanged.AddListener((value) => { InfraredDemo._ins.onSliderEvent(value); }); InfraredDemo._ins.onSetSliderValue(slider); offset = line.MyThickness = InfraredDemo._ins.lineWidth.Get(); } if (ScreenLocate.Main.getUVCTexture) { //渲染相机画面 rawImage.texture = ScreenLocate.Main.getUVCTexture; Debug.Log("rawImage.texture:" + rawImage.texture.name); } //slider.onValueChanged.AddListener((value) => { // //onSliderEvent(value); // InfraredDemo._ins.SetBrightness(value); //}); //slider.value = InfraredDemo._ins.brightness.Get(); } void Update() { //if (InfraredDemo.running) //{ // //渲染相机画面 // //rawImage.texture = InfraredDemo.infraredCameraHelper.GetCameraTexture(); // //rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial(); //} } public void OnClick_Back() { AudioMgr.ins.PlayBtn(); ViewMgr.Instance.DestroyView(); } public void OnClick_Auto() { if (enterFromInfraredDemo) { ScreenLocate _screenLocate = FindAnyObjectByType(); _screenLocate.EnterScreenLocateManualAuto(); return; } if (InfraredDemo.running) { InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto(); } } public void SyncScreenPosition() { //Debug.Log("quadUnityVectorList[i]:" + quadUnityVectorList[i]); Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize; Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect); //自动识别时候四个点 pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(texSize, canvasRectTransform.rect); pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(texSize, canvasRectTransform.rect); pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(texSize, canvasRectTransform.rect); pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(texSize, canvasRectTransform.rect); SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition)); } #region 绘制线段部分 //点击拖拽的开始位置 public void onBeginPos(int index, Vector3 pos) { beginPos = pos; } public void onDragPos(int index, Vector3 pos) { //设置线段的点 SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition)); } //点击拖拽的结束位置 public void onEndPos(int index, Vector3 pos) { endPos = pos; if (beginPos == endPos) return; //Debug.Log(index+",最后的点:" + pos); //再记录一次最后的点 SetLinePos(); } //同步设置图片对应的位置到line public void SetLinePos() { //记录一个操作点的操作位置 AddOldLinePosition(); //设置线段的点 SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition)); } void AddOldLinePosition() { Vector3[] v = new Vector3[4]; pos1.GetWorldCorners(v); Vector3[] v1 = new Vector3[4]; pos1.GetLocalCorners(v1); //for (int i = 0; i < 4; i++) //{ // Debug.Log(i + " =1= " + v[i]); // Debug.Log(i + " =2= " + v1[i]); //} //记录一个操作点的操作位置 List screenPositions = new List(); screenPositions.Add(pos1.localPosition); screenPositions.Add(pos2.localPosition); screenPositions.Add(pos3.localPosition); screenPositions.Add(pos4.localPosition); LinePosition linePosition = new LinePosition(); linePosition.index = oldLinePosition.Count; linePosition.pos = screenPositions; oldLinePosition.Add(linePosition); } //转换绘制线段的点 List linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4) { List _screenPositions = new List(); //点为负数的增大 offset,正数减少 offset //Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.5f + offset, _pos1.y - pos1.rect.height * 0.5f + offset); //Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.5f - offset, _pos2.y - pos2.rect.height * 0.5f + offset); //Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.5f - offset, _pos3.y + pos3.rect.height * 0.5f - offset); //Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.5f + offset, _pos4.y + pos4.rect.height * 0.5f - offset); Vector2 startPos1 = new Vector2(_pos1.x + offset, _pos1.y + offset); Vector2 startPos2 = new Vector2(_pos2.x - offset, _pos2.y + offset); Vector2 startPos3 = new Vector2(_pos3.x - offset, _pos3.y - offset); Vector2 startPos4 = new Vector2(_pos4.x + offset, _pos4.y - offset); _screenPositions.Add(startPos1); _screenPositions.Add(startPos2); _screenPositions.Add(startPos3); _screenPositions.Add(startPos4); return _screenPositions; } void SetRectanglePoints(List screenPositions) { line.SetLine(screenPositions); } //撤回上一个元素 public void onRecall() { // 获取并删除最后一个元素,并且保留一个元素 if (oldLinePosition.Count > 1) // 确保列表不为空 { // 获取回退的那个元素点 LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2]; // 获取最后一个元素 //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1]; //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count); oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素 pos1.localPosition = lastElement_second.pos[0]; pos2.localPosition = lastElement_second.pos[1]; pos3.localPosition = lastElement_second.pos[2]; pos4.localPosition = lastElement_second.pos[3]; //设置线段的点 SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3])); } } //是不是从Demo界面进入该页面的 [System.NonSerialized] public bool enterFromInfraredDemo; //确认修改 public void onConfirmation() { if (enterFromInfraredDemo) { ConfirmScreenLocateManualTest(); if (oldLinePosition.Count > 1) // 确保列表不为空 { LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1]; oldLinePosition.Clear(); oldLinePosition.Add(lastElement); } SaveLocalPos(); AudioMgr.ins.PlayBtn(); ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView); return; } if (!ConfirmScreenLocateManual()) return; if (oldLinePosition.Count > 1) // 确保列表不为空 { LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1]; oldLinePosition.Clear(); oldLinePosition.Add(lastElement); } if (InfraredDemo.running) { //跳转入界面 AudioMgr.ins.PlayBtn(); ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView); ViewManager2.ShowView(ViewManager2.Path_ConnectGuidanceView); } //存储一次节点 SaveLocalPos(); } //设置位置 public void onReset() { oldLinePosition.Clear(); // 获取屏幕的四个角的像素坐标 Vector2 bottomLeft = new Vector2(0, 0); // 将屏幕像素坐标转换为 Canvas 的局部坐标 Vector2 localBottomLeft; RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, bottomLeft, null, out localBottomLeft); // 打印结果 Debug.Log("Local Bottom Left: " + localBottomLeft); //int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x) - 120), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y) - 77); int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x) ), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y) ); pos1.anchoredPosition = new Vector3(-_x, -_y, 0); //Bottom Left pos2.anchoredPosition = new Vector3(_x, -_y, 0); //Bottom Right pos3.anchoredPosition = new Vector3(_x, _y, 0); //Top Right pos4.anchoredPosition = new Vector3(-_x, _y, 0); //Top Left //pos1.anchoredPosition = new Vector3(_x, _y, 0); //pos2.anchoredPosition = new Vector3(-_x, _y, 0); //pos3.anchoredPosition = new Vector3(-_x, -_y, 0); //pos4.anchoredPosition = new Vector3(_x, -_y, 0); //设置一次位置 SetLinePos(); } #endregion List _locatePointList = new(); float _texWidth; float _texHeight; void RecordLocatePoint(RectTransform p, Vector2 pivot) { Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot); pos.x = Mathf.Clamp01(pos.x / Screen.width) * _texWidth; pos.y = Mathf.Clamp01(pos.y / Screen.height) * _texHeight; _locatePointList.Add(pos); } bool ConfirmScreenLocateManual() { if (InfraredDemo.running) { //渲染截图 Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual(); if (texture2D == null) { InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null); return false; } _locatePointList.Clear(); _texWidth = texture2D.width; _texHeight = texture2D.height; RecordLocatePoint(pos1, new Vector2(0, 0)); RecordLocatePoint(pos2, new Vector2(1, 0)); RecordLocatePoint(pos3, new Vector2(1, 1)); RecordLocatePoint(pos4, new Vector2(0, 1)); //RecordLocatePoint(pos1, new Vector2(0.5f, 0.5f)); //RecordLocatePoint(pos2, new Vector2(0.5f, 0.5f)); //RecordLocatePoint(pos3, new Vector2(0.5f, 0.5f)); //RecordLocatePoint(pos4, new Vector2(0.5f, 0.5f)); InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList); FindObjectOfType().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight); //同步数据 ScreenLocate.quadUnityVectorList.Clear(); ScreenLocate.quadUnityVectorList.Add(_locatePointList[0]); ScreenLocate.quadUnityVectorList.Add(_locatePointList[1]); //两个点切换,顺序不一样 ScreenLocate.quadUnityVectorList.Add(_locatePointList[3]); ScreenLocate.quadUnityVectorList.Add(_locatePointList[2]); ScreenLocate.SaveScreenLocateVectorList(); return true; } return false; } void ConfirmScreenLocateManualTest() { //渲染截图 ScreenLocate _screenLocate = FindAnyObjectByType(); Texture2D texture2D = _screenLocate.EnterScreenLocateManual(); if (texture2D == null) { _screenLocate.QuitScreenLocateManual(null); return ; } _locatePointList.Clear(); _texWidth = texture2D.width; _texHeight = texture2D.height; RecordLocatePoint(pos1, new Vector2(0, 0)); RecordLocatePoint(pos2, new Vector2(1, 0)); RecordLocatePoint(pos3, new Vector2(1, 1)); RecordLocatePoint(pos4, new Vector2(0, 1)); _screenLocate.QuitScreenLocateManual(_locatePointList); //FindObjectOfType().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight); GameObject pointsTF2 = GameObject.Find("WebCameraView/CameraImage0/ScreenQuad"); pointsTF2.SetActive(true); if (pointsTF2.transform.childCount == _locatePointList.Count) { Vector2 texSize = new Vector2(_texWidth, _texHeight); for (int i = 0; i < pointsTF2.transform.childCount; i++) { Transform pointTF = pointsTF2.transform.GetChild(i); Vector2 pos = _locatePointList[i]; pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(texSize, pointsTF2.GetComponent().rect); pointTF.gameObject.SetActive(true); } } //同步数据 ScreenLocate.quadUnityVectorList.Clear(); ScreenLocate.quadUnityVectorList.Add(_locatePointList[0]); ScreenLocate.quadUnityVectorList.Add(_locatePointList[1]); //两个点切换,顺序不一样 ScreenLocate.quadUnityVectorList.Add(_locatePointList[3]); ScreenLocate.quadUnityVectorList.Add(_locatePointList[2]); ScreenLocate.SaveScreenLocateVectorList(); } void SaveLocalPos() { List screenPositions = new List(); screenPositions.Add(pos1.anchoredPosition); screenPositions.Add(pos2.anchoredPosition); screenPositions.Add(pos3.anchoredPosition); screenPositions.Add(pos4.anchoredPosition); string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y}")); //,{v.z} Debug.Log(saveStr); PlayerPrefs.SetString("ScreenPositioningView", saveStr); } void GetLocalPos() { string posListStr = PlayerPrefs.GetString("ScreenPositioningView", ""); if (!string.IsNullOrWhiteSpace(posListStr)) { List posList = posListStr.Split(';') .Select(s => { string[] parts = s.Split(','); return new Vector2(float.Parse(parts[0]), float.Parse(parts[1])); }) .ToList(); pos1.anchoredPosition = posList[0]; pos2.anchoredPosition = posList[1]; pos3.anchoredPosition = posList[2]; pos4.anchoredPosition = posList[3]; SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition)); } } }