From 4de58ad0dba55372d1689ed04b366c6f50f82c9b Mon Sep 17 00:00:00 2001 From: Thor Kramer Brigsted Date: Fri, 27 Oct 2017 14:06:39 +0200 Subject: [PATCH] Added T GetInputByFieldName(string fieldName, T fallback = default(); Now retrieving input values is much easier. As simple as Material material = GetInputByFieldName("material", this.material); --- Scripts/Node.cs | 21 ++++++++++++++++++++- Scripts/NodePort.cs | 8 ++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 503219e..dba0a01 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -48,6 +48,7 @@ public abstract class Node : ScriptableObject { else return GetInputByFieldName(fieldName); } + /// Returns output port which matches fieldName. Returns null if none found. public NodePort GetOutputByFieldName(string fieldName) { for (int i = 0; i < OutputCount; i++) { @@ -56,7 +57,7 @@ public abstract class Node : ScriptableObject { return null; } - /// Returns input port which matches. Returns null if none found. + /// Returns input port which matches fieldName. Returns null if none found. public NodePort GetInputByFieldName(string fieldName) { for (int i = 0; i < InputCount; i++) { if (inputs[i].fieldName == fieldName) return inputs[i]; @@ -64,6 +65,24 @@ public abstract class Node : ScriptableObject { return null; } + /// Return input value for a specified port. Returns fallback value if no ports are connected + /// Field name of requested input port + /// If no ports are connected, this value will be returned + public T GetInputByFieldName(string fieldName, T fallback = default(T)) { + NodePort port = GetInputByFieldName(fieldName); + if (port != null && port.IsConnected) return port.GetInputValue(); + else return fallback; + } + + /// Return all input values for a specified port. Returns fallback value if no ports are connected + /// Field name of requested input port + /// If no ports are connected, this value will be returned + public T[] GetInputsByFieldName(string fieldName, T[] fallback = default(T[])) { + NodePort port = GetInputByFieldName(fieldName); + if (port != null && port.IsConnected) return port.GetInputValues(); + else return fallback; + } + /// Returns a value based on requested port output. Should be overridden before used. /// The requested port. public virtual object GetValue(NodePort port) { diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 63f8110..93845cc 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -82,17 +82,17 @@ public class NodePort { /// Return the output value of the first connected port. Returns null if none found or invalid. /// - public T GetInputValue() where T : class { - return GetInputValue() as T; + public T GetInputValue() { + return (T)GetInputValue(); } /// Return the output values of all connected ports. /// - public T[] GetInputValues() where T : class { + public T[] GetInputValues() { object[] objs = GetInputValues(); T[] ts = new T[objs.Length]; for (int i = 0; i < objs.Length; i++) { - ts[i] = objs[i] as T; + ts[i] = (T)objs[i]; } return ts; }