using System; using System.Collections.Generic; using UnityEngine; /// /// 创建线条 /// public class CreateLine : CabinetUIBase { /// /// 存放线缆父物体 /// public Transform xianLan; public Transform A5; public Transform B5; public Transform A6; public Transform B6; public Transform A7; public Transform B7; public Transform A7_1; public Transform B7_1; public Transform A7_2; public Transform B7_2; public List list7 = new List(); /// /// 端口边距 /// [Header("端口边距")] public float edge_Distance = -1; [Header("不同高度集"), SerializeField] List _vectors_tall = new List(); /// /// 不同高度集 /// [HideInInspector] public List vectors_tall = new List(); public float lineWidth = 0.1f; // 线的粗细 public Color lineColor = Color.white; // 线的颜色 private LineRenderer lineRenderer; // LineRenderer 组件 /// /// 当前创建的空物体 /// private GameObject go; private void Awake() { for (int i = 0; i < _vectors_tall.Count; i++) vectors_tall.Add(_vectors_tall[i].position); } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } /// /// 生成LineGUID /// public static class UniqueGuidGenerator { private static HashSet generatedGuids = new HashSet(); public static string GenerateUniqueGuid() { string guidString; do { guidString = Guid.NewGuid().ToString("N"); } while (!generatedGuids.Add(guidString)); return guidString; } } public void init(GameObject go, Color color) { lineRenderer = go.GetComponent(); if (!lineRenderer) lineRenderer = go.AddComponent(); //lineRenderer.material = new Material(Shader.Find("Sprites/Default")); lineRenderer.material = GameObject.Instantiate(Resources.Load("emission")); lineRenderer.material.color = color; lineRenderer.startWidth = lineWidth; lineRenderer.endWidth = lineWidth; lineRenderer.startColor = lineColor; lineRenderer.endColor = lineColor; lineRenderer.numCornerVertices = 90; // 设置角处的顶点数 lineRenderer.numCapVertices = 90; // 设置端点处的顶点数 lineRenderer.useWorldSpace = false; // 将坐标设置为局部坐标 //lineRenderer.positionCount = (numSegments + 1) * 2; //UpdateCylinder(); } //public float radius = 1f; // 圆柱体半径 //public float height = 1f; // 圆柱体高度 //public int numSegments = 32; // 圆柱体分段数 void UpdateCylinder() { //float segmentAngle = 2 * Mathf.PI / numSegments; //float currentAngle = 0f; //for (int i = 0; i <= numSegments; i++) //{ // float x = Mathf.Cos(currentAngle); // float z = Mathf.Sin(currentAngle); // Vector3 topPoint = new Vector3(x * radius, height / 2f, z * radius); // Vector3 bottomPoint = new Vector3(x * radius, -height / 2f, z * radius); // lineRenderer.SetPosition(i * 2, topPoint); // lineRenderer.SetPosition(i * 2 + 1, bottomPoint); // currentAngle += segmentAngle; //} } /// /// 同行(分同层、不同层两种) /// /// /// /// 区 public void Creat5(Transform A, Transform B, int hierarchy = 0) { GameObject xian = null; var p = A.GetComponent().portList; var v = Array.Find(xianLan.GetComponentsInChildren(), (item) => { return (item.cableGroupName == p.cableGroupName); }); //CreateNewCablesTrail(); //GameObject prefab_xianlan = prefab_xianlan.gameObject; if (v == null) { go = new GameObject("EmptyObject"); go.name = p.cableGroupName; go.transform.SetParent(xianLan); xian = new GameObject(p.cableName); xian.transform.SetParent(go.transform); go.SetActive(false); } else { xian = new GameObject(p.cableName); go = v.gameObject; xian.transform.SetParent(go.transform); //prefab_xianlan.transform.SetParent(v.transform); //prefab_xianlan.name = p.cableName; go.SetActive(false); //setBasicInfo(A, B, prefab_xianlan.gameObject, p); } Color newColor = lineColor; //setColorline(p, prefab_xianlan.gameObject); try { HexToColor(PatternChoose.Inst.xianlan.lineCode_dic[p.portType], (x) => { init(xian, x); newColor = x; }); } catch (Exception e) { Debug.Log("匹配线缆颜色错误 " + e.Message + "\n" + e.StackTrace); init(xian, lineColor); } List vector3s = new List(); Vector3 v1 = A.position; Vector3 v2 = A.position + new Vector3(edge_Distance, 0, 0); //同层 V3=V4 不同层 V3!=V4 Vector3 v3 = v2 + new Vector3(0, 0, B.position.z - v2.z); Vector3 v4 = v3 + new Vector3(0, B.position.y - v3.y, 0); Vector3 v5 = B.position; vector3s.Add(v1); if (vector3s[vector3s.Count - 1] != v2) vector3s.Add(v2); if (vector3s[vector3s.Count - 1] != v3) vector3s.Add(v3); if (vector3s[vector3s.Count - 1] != v4) vector3s.Add(v4); if (vector3s[vector3s.Count - 1] != v5) vector3s.Add(v5); lineRenderer.positionCount = vector3s.Count; lineRenderer.SetPositions(vector3s.ToArray()); setBasicInfo(A, B, xian, p, newColor); //setPoss(prefab_xianlan, vector3s); } /// /// 设置线缆点 /// /// /// //public void setPoss(ACC_Trail _prefab, List vector3s) //{ //_prefab.transform.parent.gameObject.SetActive(true); //_prefab.controlPoints.Clear(); ////var acc = _prefab.GetComponent(); //for (int i = 0; i < vector3s.Count; i++) //{ // poss[i].position = vector3s[i]; // _prefab.controlPoints.Add(poss[i]); //} //_prefab.SetControlPoints(); //_prefab.UpdateSequencesPositions(); //} // 设置基本信息 public void setBasicInfo(Transform A, Transform B, GameObject xian, PortQuery.PortList p, Color x) { //prefab_xianlan.lengthSegments = 5; //prefab_xianlan.lengthDependentHeight = 0; //prefab_xianlan.verticalCurvature = 0; //prefab_xianlan.thickness = new Vector2(0.05f, 0.05f); var lineInfor = xian.AddComponent(); lineInfor.newColor = x; lineInfor.lines.Add(A); lineInfor.lines.Add(B); lineInfor.cableGroupName = p.cableGroupName; lineInfor.cableName = p.cableName; lineInfor.portType = p.portType; LineRendererInteraction interaction = xian.AddComponent(); interaction.lineInfor = lineInfor; interaction.lineRenderer = xian.GetComponent(); PortQuery p_a = A.GetComponent(); PortQuery p_b = B.GetComponent(); GameObject cab_a = GameManager.Inst.FindParent(A.gameObject, GameManager.Inst.IsDesiredParent); GameObject cab_b = GameManager.Inst.FindParent(B.gameObject, GameManager.Inst.IsDesiredParent); interaction.popupUI = PatternChoose.Inst.xianlan_popup; interaction.cableUI = interaction.popupUI.GetComponent(); interaction.port_A = p_a.portList.port; interaction.port_A_cabinet = cab_a != null ? cab_a.name : null; interaction.port_A_deviceName = p_a.portList.deviceName; interaction.port_B = p_b.portList.port; interaction.port_B_cabinet = cab_b != null ? cab_b.name : null; interaction.port_B_deviceName = p_b.portList.deviceName; CreateCableBox(interaction, lineInfor); } public void CreateCableBox(LineRendererInteraction interaction, LineInfor lineInfor) { lineRenderer = interaction.lineRenderer; //// 计算折线的边界框 //Bounds bounds = new Bounds(lineRenderer.GetPosition(0), Vector3.zero); //for (int i = 1; i < lineRenderer.positionCount; i++) //{ // bounds.Encapsulate(lineRenderer.GetPosition(i)); //} Bounds bounds = new Bounds(lineRenderer.GetPosition(0), Vector3.zero); bounds.Encapsulate(lineRenderer.GetPosition(1)); Bounds bounds2 = new Bounds(lineRenderer.GetPosition(lineRenderer.positionCount - 1), Vector3.zero); bounds2.Encapsulate(lineRenderer.GetPosition(lineRenderer.positionCount - 2)); // 添加BoxCollider组件并设置大小和位置 BoxCollider boxCollider = lineRenderer.gameObject.AddComponent(); boxCollider.center = bounds.center; //boxCollider.size = bounds.size; boxCollider.size = new Vector3(0.5F, lineInfor.lineWidth_min, lineInfor.lineWidth_min); BoxCollider boxCollider2 = lineRenderer.gameObject.AddComponent(); boxCollider2.center = bounds2.center; //boxCollider2.size = bounds2.size; boxCollider2.size = new Vector3(0.5F, lineInfor.lineWidth_min, lineInfor.lineWidth_min); } // 生成线缆预制体 public void CreateNewCablesTrail() { //prefab_xianlan = new GameObject("Cable Trail", typeof(ACC_Trail)).GetComponent(); //prefab_xianlan.CreateFirstCableSegment(prefab_xianlan.transform); //prefab_xianlan.UpdateAllSequences(); } /// /// 匹配线缆颜色 /// public void setColorline(PortQuery.PortList p, GameObject _prefab) { try { HexToColor(PatternChoose.Inst.xianlan.lineCode_dic[p.portType], (x) => { //init(x); _prefab.GetComponent().newColor = x; _prefab.GetComponent().setcolor(); }); //HexToColor(PatternChoose.Inst.xianlan.lineCode_dic[p.portType], (x) => //{ // lineRenderer.endColor = x; //}); } catch (Exception e) { _prefab.GetComponent().setcolor(); Debug.Log("匹配线缆颜色错误 " + e.Message + "\n" + e.StackTrace); //init(lineColor); } } private void HexToColor(string hex, Action callback) { if (ColorUtility.TryParseHtmlString(hex, out Color color)) { callback.Invoke(color); } else { Debug.LogError("无法识别色值: " + hex); } } /// /// 同列 /// /// /// /// 高度层级 public void Creat6(Transform A, Transform B, int hierarchy) { //init(Color.white); List vector3s = new List(); Vector3 v1 = A.position; Vector3 v2 = A.position + new Vector3(edge_Distance, 0, 0); Vector3 v3 = v2 + new Vector3(0, vectors_tall[hierarchy].y - v2.y, 0); Vector3 v4 = v3 + new Vector3(B.position.x - v3.x + edge_Distance, 0, 0); Vector3 v5 = v4 + new Vector3(0, B.position.y - v4.y, 0); Vector3 v6 = B.position; vector3s.Add(v1); vector3s.Add(v2); vector3s.Add(v3); vector3s.Add(v4); vector3s.Add(v5); vector3s.Add(v6); //lineRenderer.positionCount = 6; //lineRenderer.SetPositions(vector3s.ToArray()); } /// /// 不同行(分同列、不同列两种) /// /// /// /// 区 public void Creat7(Transform A, Transform B, int hierarchy) { GameObject xian = null; var p = A.GetComponent().portList; var v = Array.Find(xianLan.GetComponentsInChildren(), (item) => { return (item.cableGroupName == p.cableGroupName); }); if (v == null) { go = new GameObject("EmptyObject"); go.name = p.cableGroupName; go.transform.SetParent(xianLan); xian = new GameObject(p.cableName); xian.transform.SetParent(go.transform); go.SetActive(false); } else { xian = new GameObject(p.cableName); go = v.gameObject; xian.transform.SetParent(go.transform); go.SetActive(false); } Color newColor = lineColor; try { HexToColor(PatternChoose.Inst.xianlan.lineCode_dic[p.portType], (x) => { init(xian, x); newColor = x; }); } catch (Exception e) { Debug.Log("匹配线缆颜色错误 " + e.Message + "\n" + e.StackTrace); init(xian, lineColor); } //setColorline(p, prefab_xianlan.gameObject); List vector3s = new List(); Vector3 v1 = A.position; Vector3 v2 = A.position + new Vector3(edge_Distance, 0, 0); Vector3 v3 = v2 + new Vector3(0, vectors_tall[hierarchy].y - v2.y, 0); Vector3 v4 = v3 + new Vector3(B.position.x - v3.x + edge_Distance, 0, 0); Vector3 v5 = v4 + new Vector3(0, 0, B.position.z - v4.z); Vector3 v6 = v5 + new Vector3(0, B.position.y - v5.y, 0); Vector3 v7 = B.position; vector3s.Add(v1); if (vector3s[vector3s.Count - 1] != v2) vector3s.Add(v2); if (vector3s[vector3s.Count - 1] != v3) vector3s.Add(v3); if (vector3s[vector3s.Count - 1] != v4) vector3s.Add(v4); if (vector3s[vector3s.Count - 1] != v5) vector3s.Add(v5); if (vector3s[vector3s.Count - 1] != v6) vector3s.Add(v6); if (vector3s[vector3s.Count - 1] != v7) vector3s.Add(v7); lineRenderer.positionCount = vector3s.Count; lineRenderer.SetPositions(vector3s.ToArray()); setBasicInfo(A, B, xian, p, newColor); } [ContextMenu("同行")] public void F5() { Creat5(A5, B5); } [ContextMenu("6点")] public void F6() { Creat6(A6, B6, 2); } [ContextMenu("批量画线")] public void F7() { for (int i = xianLan.childCount - 1; i >= 0; i--) { DestroyImmediate(xianLan.GetChild(i).gameObject); } for (int i = 0; i < list7.Count; i++) { if (i != 0 && i % 2 != 0) { try { GameObject go1 = GameManager.Inst.FindParent(list7[i - 1].gameObject, GameManager.Inst.IsDesiredParent); GameObject go2 = GameManager.Inst.FindParent(list7[i].gameObject, GameManager.Inst.IsDesiredParent); if (!go1 || !go2) continue; int cjA = GameManager.Inst.CengJi((go1.GetComponent().deviceList.devicePosition).Split('-')[0]); int cjB = GameManager.Inst.CengJi((go2.GetComponent().deviceList.devicePosition).Split('-')[0]); if (cjA == cjB) { Creat5(list7[i - 1], list7[i]); } else { Creat7(list7[i - 1], list7[i], cjB); } } catch (Exception e) { Debug.Log("批量画线错误 " + e.Message + e.StackTrace); } } } } public override void OnMenuChanged(Menu menu) { base.OnMenuChanged(menu); if (menu == Menu.M_数字孪生_线缆连接_展示 || menu == Menu.M_数字孪生_线缆连接_配置 || menu == Menu.M_数字孪生_线缆组配置) { GameManager.Inst.FindPortPos(); var searchName = GameManager.Inst.search_box.GetComponent(); searchName.LoadXianLan(SearchName.SearchType.线缆_展示); //xianLan.gameObject.SetActive(true); } else { deletxianLan(menu); } } public void deletxianLan(Menu menu) { if (menu == Menu.M_数字孪生_线缆连接_展示 || menu == Menu.M_数字孪生_线缆连接_配置 || menu == Menu.M_数字孪生_线缆组配置) return; for (int i = xianLan.childCount - 1; i >= 0; i--) { DestroyImmediate(xianLan.GetChild(i).gameObject); } //for (int i = 0; i < GameManager.Inst.Cabinets_go.Count; i++) //{ // GameManager.Inst.Cabinets_go[i].GetComponent().F2(); //} } }