您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

218 行
5.9 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UnityEditor.Graphing
{
[Serializable]
public class SerializableNode : INode, ISerializationCallbackReceiver
{
[NonSerialized]
private Guid m_Guid;
[SerializeField]
private string m_GuidSerialized;
[SerializeField]
private string m_Name;
[SerializeField]
private DrawState m_DrawState;
[NonSerialized]
private List<ISlot> m_Slots = new List<ISlot>();
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableSlots = new List<SerializationHelper.JSONSerializedElement>();
public IGraph owner { get; set; }
public Guid guid
{
get { return m_Guid; }
}
public string name
{
get { return m_Name; }
set { m_Name = value; }
}
public virtual bool canDeleteNode
{
get { return true; }
}
public DrawState drawState
{
get { return m_DrawState; }
set
{
m_DrawState = value;
if (onModified != null)
onModified(this, ModificationScope.Node);
}
}
public virtual bool hasError { get; protected set; }
public SerializableNode()
{
m_DrawState.expanded = true;
m_Guid = Guid.NewGuid();
}
public Guid RewriteGuid()
{
m_Guid = Guid.NewGuid();
return m_Guid;
}
public virtual void ValidateNode()
{}
public OnNodeModified onModified { get; set; }
public void GetInputSlots<T>(List<T> foundSlots) where T : ISlot
{
foreach (var slot in m_Slots)
{
if (slot.isInputSlot && slot is T)
foundSlots.Add((T)slot);
}
}
public void GetOutputSlots<T>(List<T> foundSlots) where T : ISlot
{
foreach (var slot in m_Slots)
{
if (slot.isOutputSlot && slot is T)
foundSlots.Add((T)slot);
}
}
public void GetSlots<T>(List<T> foundSlots) where T : ISlot
{
foreach (var slot in m_Slots)
{
if (slot is T)
foundSlots.Add((T)slot);
}
}
public virtual void AddSlot(ISlot slot)
{
if (slot == null)
return;
m_Slots.RemoveAll(x => x.id == slot.id);
m_Slots.Add(slot);
slot.owner = this;
if (onModified != null)
{
onModified(this, ModificationScope.Topological);
}
}
public void RemoveSlot(int slotId)
{
// Remove edges that use this slot
// no owner can happen after creation
// but before added to graph
if (owner != null)
{
var edges = owner.GetEdges(GetSlotReference(slotId));
foreach (var edge in edges.ToArray())
owner.RemoveEdge(edge);
}
//remove slots
m_Slots.RemoveAll(x => x.id == slotId);
if (onModified != null)
{
onModified(this, ModificationScope.Topological);
}
}
public void RemoveSlotsNameNotMatching(IEnumerable<int> slotIds)
{
var invalidSlots = m_Slots.Select(x => x.id).Except(slotIds);
foreach (var invalidSlot in invalidSlots.ToArray())
{
Debug.LogWarningFormat("Removing Invalid MaterialSlot: {0}", invalidSlot);
RemoveSlot(invalidSlot);
}
}
public SlotReference GetSlotReference(int slotId)
{
var slot = FindSlot<ISlot>(slotId);
if (slot == null)
throw new ArgumentException("Slot could not be found", "slotId");
return new SlotReference(guid, slotId);
}
public T FindSlot<T>(int slotId) where T : ISlot
{
foreach (var slot in m_Slots)
{
if (slot.id == slotId && slot is T)
return (T)slot;
}
return default(T);
}
public T FindInputSlot<T>(int slotId) where T : ISlot
{
foreach (var slot in m_Slots)
{
if (slot.isInputSlot && slot.id == slotId && slot is T)
return (T)slot;
}
return default(T);
}
public T FindOutputSlot<T>(int slotId) where T : ISlot
{
foreach (var slot in m_Slots)
{
if (slot.isOutputSlot && slot.id == slotId && slot is T)
return (T)slot;
}
return default(T);
}
public virtual IEnumerable<ISlot> GetInputsWithNoConnection()
{
return this.GetInputSlots<ISlot>().Where(x => !owner.GetEdges(GetSlotReference(x.id)).Any());
}
public virtual void OnBeforeSerialize()
{
m_GuidSerialized = m_Guid.ToString();
m_SerializableSlots = SerializationHelper.Serialize<ISlot>(m_Slots);
}
public virtual void OnAfterDeserialize()
{
if (!string.IsNullOrEmpty(m_GuidSerialized))
m_Guid = new Guid(m_GuidSerialized);
else
m_Guid = Guid.NewGuid();
m_Slots = SerializationHelper.Deserialize<ISlot>(m_SerializableSlots, null);
m_SerializableSlots = null;
foreach (var s in m_Slots)
s.owner = this;
UpdateNodeAfterDeserialization();
}
public virtual void UpdateNodeAfterDeserialization()
{}
}
}