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()
{
go = new GameObject("EmptyObject");
lineRenderer = go.GetComponent();
if (!lineRenderer) lineRenderer = go.AddComponent();
//lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
lineRenderer.material = Resources.Load("emission");
lineRenderer.startWidth = lineWidth;
lineRenderer.endWidth = lineWidth;
lineRenderer.startColor = lineColor;
lineRenderer.endColor = lineColor;
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)
{
init();
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);
vector3s.Add(v2);
vector3s.Add(v3);
vector3s.Add(v4);
vector3s.Add(v5);
lineRenderer.positionCount = 5;
lineRenderer.SetPositions(vector3s.ToArray());
go.transform.SetParent(xianLan);
go.name = A.GetComponent().portList.cableGroupName;
go.SetActive(false);
var l = go.AddComponent();
l.lines.Add(A);
l.lines.Add(B);
}
///
/// 同列
///
///
///
/// 高度层级
public void Creat6(Transform A, Transform B, int hierarchy)
{
init();
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)
{
init();
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);
vector3s.Add(v2);
vector3s.Add(v3);
vector3s.Add(v4);
vector3s.Add(v5);
vector3s.Add(v6);
vector3s.Add(v7);
lineRenderer.positionCount = 7;
lineRenderer.SetPositions(vector3s.ToArray());
go.transform.SetParent(xianLan);
go.name = A.GetComponent().portList.cableGroupName;
go.SetActive(false);
var l = go.AddComponent();
l.lines.Add(A);
l.lines.Add(B);
}
[ContextMenu("同行")]
public void F5()
{
Creat5(A5, B5);
}
[ContextMenu("6点")]
public void F6()
{
Creat6(A6, B6, 2);
}
[ContextMenu("批量画线")]
public void F7()
{
for (int i = 0; i < xianLan.childCount; i++)
{
Destroy(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);
}
}
}
}
public override void OnMenuChanged(Menu menu)
{
base.OnMenuChanged(menu);
if (menu == Menu.M_数字孪生_线缆连接_展示)
{
GameManager.Inst.FindPortPos();
var searchName = GameManager.Inst.search_box.GetComponent();
searchName.LoadXianLan(SearchName.SearchType.线缆_展示);
//xianLan.gameObject.SetActive(true);
}
else
{
for (int i = 0; i < xianLan.childCount; i++)
{
Destroy(xianLan.GetChild(i).gameObject);
}
for (int i = 0; i < GameManager.Inst.Cabinets_go.Count; i++)
{
GameManager.Inst.Cabinets_go[i].GetComponent().F2();
}
}
}
}