303 lines
8.9 KiB
C#
303 lines
8.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
|
|
/// <summary>
|
|
/// 创建线条
|
|
/// </summary>
|
|
public class CreateLine : CabinetUIBase
|
|
{
|
|
/// <summary>
|
|
/// 存放线缆父物体
|
|
/// </summary>
|
|
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<Transform> list7 = new List<Transform>();
|
|
|
|
/// <summary>
|
|
/// 端口边距
|
|
/// </summary>
|
|
[Header("端口边距")] public float edge_Distance = -1;
|
|
|
|
[Header("不同高度集"), SerializeField] List<Transform> _vectors_tall = new List<Transform>();
|
|
|
|
/// <summary>
|
|
/// 不同高度集
|
|
/// </summary>
|
|
[HideInInspector] public List<Vector3> vectors_tall = new List<Vector3>();
|
|
|
|
public float lineWidth = 0.1f; // 线的粗细
|
|
public Color lineColor = Color.white; // 线的颜色
|
|
|
|
private LineRenderer lineRenderer; // LineRenderer 组件
|
|
|
|
/// <summary>
|
|
/// 当前创建的空物体
|
|
/// </summary>
|
|
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()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成LineGUID
|
|
/// </summary>
|
|
public static class UniqueGuidGenerator
|
|
{
|
|
private static HashSet<string> generatedGuids = new HashSet<string>();
|
|
|
|
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<LineRenderer>();
|
|
if (!lineRenderer) lineRenderer = go.AddComponent<LineRenderer>();
|
|
//lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
|
|
lineRenderer.material = Resources.Load<Material>("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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 同行(分同层、不同层两种)
|
|
/// </summary>
|
|
/// <param name="A"></param>
|
|
/// <param name="B"></param>
|
|
/// <param name="hierarchy">区</param>
|
|
public void Creat5(Transform A, Transform B, int hierarchy = 0)
|
|
{
|
|
init();
|
|
List<Vector3> vector3s = new List<Vector3>();
|
|
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<PortQuery>().portList.cableGroupName;
|
|
go.SetActive(false);
|
|
var l = go.AddComponent<LineInfor>();
|
|
l.lines.Add(A);
|
|
l.lines.Add(B);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 同列
|
|
/// </summary>
|
|
/// <param name="A"></param>
|
|
/// <param name="B"></param>
|
|
/// <param name="hierarchy">高度层级</param>
|
|
public void Creat6(Transform A, Transform B, int hierarchy)
|
|
{
|
|
init();
|
|
List<Vector3> vector3s = new List<Vector3>();
|
|
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());
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 不同行(分同列、不同列两种)
|
|
/// </summary>
|
|
/// <param name="A"></param>
|
|
/// <param name="B"></param>
|
|
/// <param name="hierarchy">区</param>
|
|
public void Creat7(Transform A, Transform B, int hierarchy)
|
|
{
|
|
init();
|
|
List<Vector3> vector3s = new List<Vector3>();
|
|
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<PortQuery>().portList.cableGroupName;
|
|
go.SetActive(false);
|
|
var l = go.AddComponent<LineInfor>();
|
|
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<DeviceQuery>().deviceList.devicePosition).Split('-')[0]);
|
|
int cjB = GameManager.Inst.CengJi((go2.GetComponent<DeviceQuery>().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>();
|
|
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<TransparentGlow>().F2();
|
|
}
|
|
}
|
|
}
|
|
}
|