feat(Connect): 实现在场景中提前布置连线,游戏开始后自动连接的功能

This commit is contained in:
2025-10-20 11:02:10 +08:00
parent ec905d2a27
commit bb2c5aa436
3 changed files with 144 additions and 4 deletions

View File

@@ -1,25 +1,58 @@
using System;
using UnityEngine;
using System.Collections.Generic;
using Core;
namespace Script.Gameplay.Connect
{
[Serializable]
public class PreviousConnection
{
public int PreConnectionID;
public MonoBehaviour source;
public MonoBehaviour target;
}
public class ConnectionLineManager : MonoSingleton<ConnectionLineManager>
{
[SerializeField] private GameObject linePrefab;
public List<ConnectionLine> connectionLines = new List<ConnectionLine>();
[SerializeField]private List<PreviousConnection> preConnectionLines = new List<PreviousConnection>();
private List<ConnectionLine> connectionLines = new List<ConnectionLine>();
private void Start()
{
GeneratePreviousConnectionLines(preConnectionLines);
}
public ConnectionLine GenerateConnectionLine(IConnectable source, IConnectable target)
{
GameObject lineObject = Instantiate(linePrefab, this.transform);
ConnectionLine connectionLine = lineObject.GetComponent<ConnectionLine>();
if (connectionLine != null)
{
source.OutputConnectionLine = connectionLine;
target.InputConnectionLine = connectionLine;
connectionLine.SetConnectable(source, target);
connectionLines.Add(connectionLine);
return connectionLine;
}
return null;
}
public void GeneratePreviousConnectionLines(List<PreviousConnection> previousConnections)
{
foreach (var preConnection in previousConnections)
{
IConnectable source = preConnection.source as IConnectable;
IConnectable target = preConnection.target as IConnectable;
if (source != null && target != null)
{
GenerateConnectionLine(source, target);
}
else
{
Debug.Log(preConnection.PreConnectionID + " connection failed to load.");
}
}
}
}
}

View File

@@ -121,8 +121,7 @@ namespace Script.Gameplay.Player
}
// 创建连接线
ConnectionLine connectionLine = ConnectionLineManager.Instance.GenerateConnectionLine(outTarget, inputTarget);
outTarget.OutputConnectionLine = connectionLine;
inputTarget.InputConnectionLine = connectionLine;
// 重置信号目标
outTarget = null;
inputTarget = null;