From 49b25e755b70ec412feaaf0b898d6f7e09d2bea6 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 28 Jun 2022 09:40:37 +0800 Subject: +node example --- .../RuntimeMathGraph/Scripts/RuntimeMathGraph.cs | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs (limited to 'Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs') diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs new file mode 100644 index 00000000..f1b39b9b --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/RuntimeMathGraph/Scripts/RuntimeMathGraph.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; +using XNode.Examples.MathNodes; + +namespace XNode.Examples.RuntimeMathNodes { + public class RuntimeMathGraph : MonoBehaviour, IPointerClickHandler { + [Header("Graph")] + public MathGraph graph; + [Header("Prefabs")] + public XNode.Examples.RuntimeMathNodes.UGUIMathNode runtimeMathNodePrefab; + public XNode.Examples.RuntimeMathNodes.UGUIVector runtimeVectorPrefab; + public XNode.Examples.RuntimeMathNodes.UGUIDisplayValue runtimeDisplayValuePrefab; + public XNode.Examples.RuntimeMathNodes.Connection runtimeConnectionPrefab; + [Header("References")] + public UGUIContextMenu graphContextMenu; + public UGUIContextMenu nodeContextMenu; + public UGUITooltip tooltip; + + public ScrollRect scrollRect { get; private set; } + private List nodes; + + private void Awake() { + // Create a clone so we don't modify the original asset + graph = graph.Copy() as MathGraph; + scrollRect = GetComponentInChildren(); + graphContextMenu.onClickSpawn -= SpawnNode; + graphContextMenu.onClickSpawn += SpawnNode; + } + + private void Start() { + SpawnGraph(); + } + + public void Refresh() { + Clear(); + SpawnGraph(); + } + + public void Clear() { + for (int i = nodes.Count - 1; i >= 0; i--) { + Destroy(nodes[i].gameObject); + } + nodes.Clear(); + } + + public void SpawnGraph() { + if (nodes != null) nodes.Clear(); + else nodes = new List(); + + for (int i = 0; i < graph.nodes.Count; i++) { + Node node = graph.nodes[i]; + + UGUIMathBaseNode runtimeNode = null; + if (node is XNode.Examples.MathNodes.MathNode) { + runtimeNode = Instantiate(runtimeMathNodePrefab); + } else if (node is XNode.Examples.MathNodes.Vector) { + runtimeNode = Instantiate(runtimeVectorPrefab); + } else if (node is XNode.Examples.MathNodes.DisplayValue) { + runtimeNode = Instantiate(runtimeDisplayValuePrefab); + } + runtimeNode.transform.SetParent(scrollRect.content); + runtimeNode.node = node; + runtimeNode.graph = this; + nodes.Add(runtimeNode); + } + } + + public UGUIMathBaseNode GetRuntimeNode(Node node) { + for (int i = 0; i < nodes.Count; i++) { + if (nodes[i].node == node) { + return nodes[i]; + } else { } + } + return null; + } + + public void SpawnNode(Type type, Vector2 position) { + Node node = graph.AddNode(type); + node.name = type.Name; + node.position = position; + Refresh(); + } + + public void OnPointerClick(PointerEventData eventData) { + if (eventData.button != PointerEventData.InputButton.Right) + return; + + graphContextMenu.OpenAt(eventData.position); + } + } +} \ No newline at end of file -- cgit v1.1-26-g67d0