using System; using UnityEngine; using System.Collections.Generic; using Script.Gameplay.Interface; using Core; namespace Script.Gameplay.Connect { [Serializable] public class PreviousConnection { public int PreConnectionID; public MonoBehaviour source; public MonoBehaviour target; } public class ConnectionLineManager : MonoSingleton { [SerializeField] private GameObject linePrefab; [SerializeField]private List preConnectionLines = new List(); private List connectionLines = new List(); private void Start() { GeneratePreviousConnectionLines(preConnectionLines); } public ConnectionLine GenerateConnectionLine(IConnectable a, IConnectable b) { // 先检测 a b 点是否已经存在连接线 foreach (var aline in a.ConnectionLines) { if (aline.PointA.GetGameObject() == b.GetGameObject() || aline.PointB.GetGameObject() == b.GetGameObject()) { Debug.Log("ab点间已经存在连接线"); return null; } } GameObject lineObject = Instantiate(linePrefab, this.transform); ConnectionLine connectionLine = lineObject.GetComponent(); if (connectionLine != null) { connectionLine.SetConnectable(a, b); connectionLines.Add(connectionLine); return connectionLine; } return null; } public void CutTargetConnectionLines(IConnectable target) { List linesToRemove = new List(); foreach (var line in target.ConnectionLines) { if (line != null) { linesToRemove.Add(line); } } foreach (var line in linesToRemove) { connectionLines.Remove(line); Destroy(line.gameObject); } } public void GeneratePreviousConnectionLines(List previousConnections) { foreach (var preConnection in previousConnections) { IConnectable source = preConnection.source as IConnectable; IConnectable target = preConnection.target as IConnectable; if (source != null && target != null) { var connectionLine = GenerateConnectionLine(source, target); if (connectionLine == null) { Debug.LogWarning($"编号为{preConnection.PreConnectionID}的连接线存在重复连接,生成失败"); } } else { Debug.LogWarning($"编号为{preConnection.PreConnectionID}的连接线的配置错误,生成失败"); } } } } }