using System.Collections; using System.Collections.Generic; using UnityEngine; using System; /// Base class for all node graphs [Serializable] public abstract class NodeGraph : ScriptableObject, ISerializationCallbackReceiver { /// All nodes in the graph. /// See: [SerializeField] public List nodes = new List(); public T AddNode() where T : Node { return AddNode(typeof(T)) as T; } public virtual Node AddNode(Type type) { Node node = ScriptableObject.CreateInstance(type) as Node; #if UNITY_EDITOR UnityEditor.AssetDatabase.AddObjectToAsset(node, this); #endif nodes.Add(node); node.graph = this; return node; } /// Safely remove a node and all its connections /// public void RemoveNode(Node node) { node.ClearConnections(); nodes.Remove(node); } /// Remove all nodes and connections from the graph public void Clear() { nodes.Clear(); } public void OnBeforeSerialize() { } public void OnAfterDeserialize() { for (int i = 0; i < nodes.Count; i++) { nodes[i].graph = this; } } }