浏览代码

Use dictionary to store nodes for faster lookups

/main
Peter Bay Bastian 7 年前
当前提交
eb8a8906
共有 2 个文件被更改,包括 26 次插入13 次删除
  1. 38
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableGraph.cs
  2. 1
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/IGraph.cs

38
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableGraph.cs


public class SerializableGraph : IGraph, ISerializationCallbackReceiver
{
[NonSerialized]
private List<IEdge> m_Edges = new List<IEdge>();
List<IEdge> m_Edges = new List<IEdge>();
private List<INode> m_Nodes = new List<INode>();
Dictionary<Guid, INode> m_Nodes = new Dictionary<Guid, INode>();
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>();

public IEnumerable<T> GetNodes<T>() where T : INode
{
return m_Nodes.OfType<T>();
return m_Nodes.Values.OfType<T>();
}
public IEnumerable<IEdge> edges

public virtual void AddNode(INode node)
{
m_Nodes.Add(node);
m_Nodes.Add(node.guid, node);
node.owner = this;
ValidateGraph();
}

if (!node.canDeleteNode)
return;
m_Nodes.Remove(node);
m_Nodes.Remove(node.guid);
private void RemoveNodeNoValidate(INode node)
void RemoveNodeNoValidate(INode node)
m_Nodes.Remove(node);
m_Nodes.Remove(node.guid);
}
public virtual Dictionary<SerializationHelper.TypeSerializationInfo, SerializationHelper.TypeSerializationInfo> GetLegacyTypeRemapping()

ValidateGraph();
}
private void RemoveEdgeNoValidate(IEdge e)
void RemoveEdgeNoValidate(IEdge e)
{
m_Edges.Remove(e);
}

return m_Nodes.FirstOrDefault(x => x.guid == guid);
INode node;
m_Nodes.TryGetValue(guid, out node);
return node;
}
public bool ContainsNodeGuid(Guid guid)
{
return m_Nodes.ContainsKey(guid);
return m_Nodes.Where(x => x.guid == guid).OfType<T>().FirstOrDefault();
var node = GetNodeFromGuid(guid);
if (node is T)
return (T)node;
return default(T);
}
public IEnumerable<IEdge> GetEdges(SlotReference s)

public virtual void OnBeforeSerialize()
{
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes);
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes.Values);
m_Nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes, GetLegacyTypeRemapping());
foreach (var node in m_Nodes)
var nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes, GetLegacyTypeRemapping());
m_Nodes = new Dictionary<Guid, INode>(nodes.Count);
foreach (var node in nodes)
m_Nodes.Add(node.guid, node);
}
m_SerializableNodes = null;

1
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/IGraph.cs


void RemoveEdge(IEdge e);
void RemoveElements(IEnumerable<INode> nodes, IEnumerable<IEdge> edges);
INode GetNodeFromGuid(Guid guid);
bool ContainsNodeGuid(Guid guid);
T GetNodeFromGuid<T>(Guid guid) where T : INode;
IEnumerable<IEdge> GetEdges(SlotReference s);
void ValidateGraph();
正在加载...
取消
保存