using System;
using System.Collections.Generic;
using UnityEngine;
namespace XNode {
/// Base class for all node graphs
[Serializable]
public abstract class NodeGraph : ScriptableObject {
/// All nodes in the graph.
/// See:
[SerializeField] public List nodes = new List();
/// Add a node to the graph by type
public T AddNode() where T : Node {
return AddNode(typeof(T)) as T;
}
/// Add a node to the graph by type
public virtual Node AddNode(Type type) {
Node node = ScriptableObject.CreateInstance(type) as Node;
#if UNITY_EDITOR
if (!Application.isPlaying) {
UnityEditor.AssetDatabase.AddObjectToAsset(node, this);
UnityEditor.AssetDatabase.SaveAssets();
}
#endif
nodes.Add(node);
node.graph = this;
return node;
}
/// Creates a copy of the original node in the graph
public virtual Node CopyNode(Node original) {
Node node = ScriptableObject.Instantiate(original);
node.ClearConnections();
#if UNITY_EDITOR
if (!Application.isPlaying) {
UnityEditor.AssetDatabase.AddObjectToAsset(node, this);
UnityEditor.AssetDatabase.SaveAssets();
}
#endif
nodes.Add(node);
node.graph = this;
return node;
}
/// Safely remove a node and all its connections
///
public void RemoveNode(Node node) {
node.ClearConnections();
#if UNITY_EDITOR
if (!Application.isPlaying) {
DestroyImmediate(node, true);
UnityEditor.AssetDatabase.SaveAssets();
}
#endif
nodes.Remove(node);
}
/// Remove all nodes and connections from the graph
public void Clear() {
nodes.Clear();
}
}
}