How To Access From Code
This page contains examples about the usage of GDIs graphs in your project.
Use A Graph In Your Game
The following example loads a graph at runtime and uses its chunk generator node
to create landscapes. You can also find the script in the behaviour collection
of GDI.
public class GhunkGeneratorBehaviour : MonoBehaviour
{
private ChunkGeneratorNode _chunkGenerator;
private GameObject _camera;
void Start ()
{
string filePath = Application.streamingAssetsPath + "/Examples/BasicLandscape.json";
Graph.Graph graph = Launcher.Instance.LoadGraph(filePath);
if (graph == null)
{
Log.Error("Can not find graph file " + filePath);
return;
}
graph.ForceUpdateNodes();
_chunkGenerator = (ChunkGeneratorNode) graph.GetFirstNodeWithType(typeof(ChunkGeneratorNode));
if (_chunkGenerator == null) Log.Error("Can not find a chunk generator node in the graph.");
_camera = GameObject.Find("Main Camera");
if (_camera == null)
{
Log.Error("Can not camera with the name 'Main Camera'.");
return;
}
_camera.AddComponent<KeyboardControls>();
Debug.Log("OnStart");
}
void Update ()
{
if (_chunkGenerator != null && _camera != null)
{
_chunkGenerator.UpdateChunks();
_chunkGenerator.RequestChunks(_camera.transform.position);
}
}
}
Create A Graph Programmatically
Graphs can be created directly by a script. The following example creates a graph
with some standard nodes. You can also find the script in the behaviour collection
of GDI.
public class GraphByScriptBehaviour : MonoBehaviour
{
void Start ()
{
Graph.Graph graph = new Graph.Graph();
var operator01 = (NumberOperatorNode) graph.CreateNode<NumberOperatorNode>();
operator01.X = 200;
operator01.Y = 40;
operator01.SetMode(Operator.Add);
graph.AddNode(operator01);
var diplay01 = (NumberDisplayNode) graph.CreateNode<NumberDisplayNode>();
diplay01.X = 330;
diplay01.Y = 80;
graph.AddNode(diplay01);
graph.Link(
(InputSocket) diplay01.GetSocket(typeof(INumberConnection), typeof(InputSocket), 0),
(OutputSocket) operator01.GetSocket(typeof(INumberConnection), typeof(OutputSocket), 0));
var perlinNoise = graph.CreateNode<UnityPerlinNoiseNode>();
perlinNoise.X = 80;
perlinNoise.Y = 250;
graph.AddNode(perlinNoise);
var displayMap = graph.CreateNode<DisplayMapNode>();
displayMap.X = 300;
displayMap.Y = 280;
graph.AddNode(displayMap);
graph.Link(
(InputSocket) displayMap.GetSocket(typeof(INumberConnection), typeof(InputSocket), 0),
(OutputSocket) perlinNoise.GetSocket(typeof(INumberConnection), typeof(OutputSocket), 0));
var serializedJSON = graph.ToJson();
var deserializedGraph = Graph.Graph.FromJson(serializedJSON);
Launcher.Instance.AddGraph(deserializedGraph);
}
void Update () {}
}
Subscribe To Editor Events
You can subscribe to editor events using the static EventManager class. This events are
usually triggered by editing a graph in the editor window. The following example shows a
register method that that you can call in the OnEnable or OnStart method of Unity.
Take a look at the EventManager class to find the signatures of the methods you want to implement.
public void Register()
{
EventManager.OnCreateGraph += OnCreate;
EventManager.OnFocusGraph += OnFocus;
EventManager.OnCloseGraph += OnClose;
EventManager.OnLinkEdge += OnLink;
EventManager.OnUnLinkSockets += OnUnLink;
EventManager.OnUnLinkedSockets += OnUnLinked;
EventManager.OnAddedNode += OnNodeAdded;
EventManager.OnNodeRemoved += OnNodeRemoved;
EventManager.OnChangedNode += OnNodeChanged;
EventManager.OnFocusNode += OnFocusNode;
EventManager.OnEditorWindowOpen += OnWindowOpen;
EventManager.OnNodeRenamed += OnNodeRenamed;
}
public void OnLink(Graph graph, Edge edge)
{
Log.Info("OnLink: Node " + edge.Output.Parent.Id + " with Node " + edge.Input.Parent.Id);
}