您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
136 行
4.3 KiB
136 行
4.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
using UnityEditor.Graphing;
|
|
|
|
namespace UnityEditor.Graphing.Util
|
|
{
|
|
[Serializable]
|
|
sealed class CopyPasteGraph : ISerializationCallbackReceiver
|
|
{
|
|
[NonSerialized]
|
|
HashSet<IEdge> m_Edges = new HashSet<IEdge>();
|
|
|
|
[NonSerialized]
|
|
HashSet<INode> m_Nodes = new HashSet<INode>();
|
|
|
|
[SerializeField]
|
|
List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>();
|
|
|
|
[SerializeField]
|
|
List<SerializationHelper.JSONSerializedElement> m_SerializableEdges = new List<SerializationHelper.JSONSerializedElement>();
|
|
|
|
public CopyPasteGraph() { }
|
|
|
|
public CopyPasteGraph(IEnumerable<INode> nodes, IEnumerable<IEdge> edges)
|
|
{
|
|
foreach (var node in nodes)
|
|
{
|
|
AddNode(node);
|
|
foreach (var edge in NodeUtils.GetAllEdges(node))
|
|
AddEdge(edge);
|
|
}
|
|
|
|
foreach (var edge in edges)
|
|
AddEdge(edge);
|
|
}
|
|
|
|
public void AddNode(INode node)
|
|
{
|
|
m_Nodes.Add(node);
|
|
}
|
|
|
|
public void AddEdge(IEdge edge)
|
|
{
|
|
m_Edges.Add(edge);
|
|
}
|
|
|
|
public IEnumerable<T> GetNodes<T>() where T : INode
|
|
{
|
|
return m_Nodes.OfType<T>();
|
|
}
|
|
|
|
public IEnumerable<IEdge> edges
|
|
{
|
|
get { return m_Edges; }
|
|
}
|
|
|
|
public void InsertInGraph(IGraph graph, List<INode> remappedNodes, List<IEdge> remappedEdges)
|
|
{
|
|
var nodeGuidMap = new Dictionary<Guid, Guid>();
|
|
foreach (var node in GetNodes<INode>())
|
|
{
|
|
var oldGuid = node.guid;
|
|
var newGuid = node.RewriteGuid();
|
|
nodeGuidMap[oldGuid] = newGuid;
|
|
|
|
var drawState = node.drawState;
|
|
var position = drawState.position;
|
|
position.x += 30;
|
|
position.y += 30;
|
|
drawState.position = position;
|
|
node.drawState = drawState;
|
|
remappedNodes.Add(node);
|
|
graph.AddNode(node);
|
|
}
|
|
|
|
// only connect edges within pasted elements, discard
|
|
// external edges.
|
|
foreach (var edge in edges)
|
|
{
|
|
var outputSlot = edge.outputSlot;
|
|
var inputSlot = edge.inputSlot;
|
|
|
|
Guid remappedOutputNodeGuid;
|
|
Guid remappedInputNodeGuid;
|
|
if (nodeGuidMap.TryGetValue(outputSlot.nodeGuid, out remappedOutputNodeGuid)
|
|
&& nodeGuidMap.TryGetValue(inputSlot.nodeGuid, out remappedInputNodeGuid))
|
|
{
|
|
var outputSlotRef = new SlotReference(remappedOutputNodeGuid, outputSlot.slotId);
|
|
var inputSlotRef = new SlotReference(remappedInputNodeGuid, inputSlot.slotId);
|
|
remappedEdges.Add(graph.Connect(outputSlotRef, inputSlotRef));
|
|
}
|
|
}
|
|
|
|
m_Nodes.Clear();
|
|
m_Edges.Clear();
|
|
graph.ValidateGraph();
|
|
}
|
|
|
|
public void OnBeforeSerialize()
|
|
{
|
|
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes);
|
|
m_SerializableEdges = SerializationHelper.Serialize<IEdge>(m_Edges);
|
|
}
|
|
|
|
public void OnAfterDeserialize()
|
|
{
|
|
var nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes, null);
|
|
m_Nodes.Clear();
|
|
foreach (var node in nodes)
|
|
m_Nodes.Add(node);
|
|
m_SerializableNodes = null;
|
|
|
|
var edges = SerializationHelper.Deserialize<IEdge>(m_SerializableEdges, null);
|
|
m_Edges.Clear();
|
|
foreach (var edge in edges)
|
|
m_Edges.Add(edge);
|
|
m_SerializableEdges = null;
|
|
}
|
|
|
|
internal static CopyPasteGraph FromJson(string copyBuffer)
|
|
{
|
|
try
|
|
{
|
|
return JsonUtility.FromJson<CopyPasteGraph>(copyBuffer);
|
|
}
|
|
catch
|
|
{
|
|
// ignored. just means copy buffer was not a graph :(
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|