diff --git a/Example/ExampleNodeGraph.asset b/Example/ExampleNodeGraph.asset index 202aa01..6aab2a7 100644 Binary files a/Example/ExampleNodeGraph.asset and b/Example/ExampleNodeGraph.asset differ diff --git a/Scripts/Editor/NodeEditor.cs b/Scripts/Editor/NodeEditor.cs index 053569c..a6c1c84 100644 --- a/Scripts/Editor/NodeEditor.cs +++ b/Scripts/Editor/NodeEditor.cs @@ -1,9 +1,9 @@ -using System.Collections.Generic; -using UnityEngine; -using UnityEditor; -using System.Reflection; +using System; +using System.Collections.Generic; using System.Linq; -using System; +using System.Reflection; +using UnityEditor; +using UnityEngine; /// Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. public class NodeEditor { @@ -12,7 +12,7 @@ public class NodeEditor { /// Draws the node GUI. /// Port handle positions need to be returned to the NodeEditorWindow - public virtual void OnNodeGUI(out Dictionary portPositions) { + public virtual void OnNodeGUI(out Dictionary portPositions) { DrawDefaultHeaderGUI(); DrawDefaultNodePortsGUI(out portPositions); DrawDefaultNodeBodyGUI(); @@ -75,12 +75,16 @@ public class NodeEditor { Vector2 handlePoint = rect.center; switch (port.direction) { - case NodePort.IO.Input: handlePoint.x = rect.xMin; break; - case NodePort.IO.Output: handlePoint.x = rect.xMax; break; + case NodePort.IO.Input: + handlePoint.x = rect.xMin; + break; + case NodePort.IO.Output: + handlePoint.x = rect.xMax; + break; } Color col = GUI.color; - Rect handleRect = new Rect(handlePoint.x-8,handlePoint.y-8,16,16); + Rect handleRect = new Rect(handlePoint.x - 8, handlePoint.y - 8, 16, 16); GUI.color = new Color(0.29f, 0.31f, 0.32f); GUI.DrawTexture(handleRect, NodeEditorResources.dotOuter); GUI.color = NodeEditorPreferences.GetTypeColor(port.type); @@ -90,7 +94,7 @@ public class NodeEditor { } private static FieldInfo[] GetInspectorFields(Node node) { - return node.GetType().GetFields().Where(f => f.IsPublic || f.GetCustomAttributes(typeof(SerializeField),false) != null).ToArray(); + return node.GetType().GetFields().Where(f => f.IsPublic || f.GetCustomAttributes(typeof(SerializeField), false) != null).ToArray(); } private void DrawFieldInfoDrawerGUI(FieldInfo fieldInfo) { @@ -111,68 +115,56 @@ public class NodeEditor { rect.width *= 0.5f; EditorGUI.LabelField(rect, fieldPrettyName); rect.x += rect.width; - fieldValue = EditorGUI.IntField(rect, (int)fieldValue); - } - else if (fieldType == typeof(bool)) { - fieldValue = EditorGUILayout.Toggle(fieldPrettyName, (bool)fieldValue); - } - else if (fieldType.IsEnum) { + fieldValue = EditorGUI.IntField(rect, (int) fieldValue); + } else if (fieldType == typeof(bool)) { + fieldValue = EditorGUILayout.Toggle(fieldPrettyName, (bool) fieldValue); + } else if (fieldType.IsEnum) { Rect rect = EditorGUILayout.GetControlRect(); rect.width *= 0.5f; EditorGUI.LabelField(rect, fieldPrettyName); rect.x += rect.width; - fieldValue = EditorGUI.EnumPopup(rect, (Enum)fieldValue); - } - else if (fieldType == typeof(string)) { + fieldValue = EditorGUI.EnumPopup(rect, (Enum) fieldValue); + } else if (fieldType == typeof(string)) { if (fieldName == "name") return; //Ignore 'name' TextAreaAttribute textAreaAttrib; if (NodeEditorUtilities.GetAttrib(fieldAttribs, out textAreaAttrib)) { - fieldValue = EditorGUILayout.TextArea(fieldValue != null ? (string)fieldValue : ""); - } - else - fieldValue = EditorGUILayout.TextField(fieldPrettyName, fieldValue != null ? (string)fieldValue : ""); - } - else if (fieldType == typeof(Rect)) { + fieldValue = EditorGUILayout.TextArea(fieldValue != null ? (string) fieldValue : ""); + } else + fieldValue = EditorGUILayout.TextField(fieldPrettyName, fieldValue != null ? (string) fieldValue : ""); + } else if (fieldType == typeof(Rect)) { if (fieldName == "rect") return; //Ignore 'rect' - fieldValue = EditorGUILayout.RectField(fieldPrettyName, (Rect)fieldValue); - } - else if (fieldType == typeof(float)) { + fieldValue = EditorGUILayout.RectField(fieldPrettyName, (Rect) fieldValue); + } else if (fieldType == typeof(float)) { Rect rect = EditorGUILayout.GetControlRect(); rect.width *= 0.5f; EditorGUI.LabelField(rect, fieldPrettyName); rect.x += rect.width; - fieldValue = EditorGUI.FloatField(rect, (float)fieldValue); - } - else if (fieldType == typeof(Vector2)) { - fieldValue = EditorGUILayout.Vector2Field(fieldPrettyName, (Vector2)fieldValue); - } - else if (fieldType == typeof(Vector3)) { - fieldValue = EditorGUILayout.Vector3Field(new GUIContent(fieldPrettyName), (Vector3)fieldValue); - } - else if (fieldType == typeof(Vector4)) { - fieldValue = EditorGUILayout.Vector4Field(fieldPrettyName, (Vector4)fieldValue); - } - else if (fieldType == typeof(Color)) { + fieldValue = EditorGUI.FloatField(rect, (float) fieldValue); + } else if (fieldType == typeof(Vector2)) { + fieldValue = EditorGUILayout.Vector2Field(fieldPrettyName, (Vector2) fieldValue); + } else if (fieldType == typeof(Vector3)) { + fieldValue = EditorGUILayout.Vector3Field(new GUIContent(fieldPrettyName), (Vector3) fieldValue); + } else if (fieldType == typeof(Vector4)) { + fieldValue = EditorGUILayout.Vector4Field(fieldPrettyName, (Vector4) fieldValue); + } else if (fieldType == typeof(Color)) { Rect rect = EditorGUILayout.GetControlRect(); rect.width *= 0.5f; EditorGUI.LabelField(rect, fieldPrettyName); rect.x += rect.width; - fieldValue = EditorGUI.ColorField(rect, (Color)fieldValue); - } - else if (fieldType == typeof(AnimationCurve)) { + fieldValue = EditorGUI.ColorField(rect, (Color) fieldValue); + } else if (fieldType == typeof(AnimationCurve)) { Rect rect = EditorGUILayout.GetControlRect(); rect.width *= 0.5f; EditorGUI.LabelField(rect, fieldPrettyName); rect.x += rect.width; - AnimationCurve curve = fieldValue != null ? (AnimationCurve)fieldValue : new AnimationCurve(); + AnimationCurve curve = fieldValue != null ? (AnimationCurve) fieldValue : new AnimationCurve(); curve = EditorGUI.CurveField(rect, curve); if (fieldValue != curve) fieldInfo.SetValue(target, curve); - } - else if (fieldType.IsSubclassOf(typeof(UnityEngine.Object)) || fieldType == typeof(UnityEngine.Object)) { + } else if (fieldType.IsSubclassOf(typeof(UnityEngine.Object)) || fieldType == typeof(UnityEngine.Object)) { if (fieldName == "graph") return; //Ignore 'graph' - fieldValue = EditorGUILayout.ObjectField(fieldPrettyName, (UnityEngine.Object)fieldValue, fieldType, true); + fieldValue = EditorGUILayout.ObjectField(fieldPrettyName, (UnityEngine.Object) fieldValue, fieldType, true); } if (EditorGUI.EndChangeCheck()) { fieldInfo.SetValue(target, fieldValue); @@ -198,4 +190,4 @@ public class CustomNodeEditorAttribute : Attribute { _inspectedType = inspectedType; _contextMenuName = contextMenuName; } -} +} \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 313fbf4..aeed708 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -1,8 +1,8 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; -using UnityEngine; using UnityEditor; -using System; +using UnityEngine; public partial class NodeEditorWindow { @@ -25,7 +25,7 @@ public partial class NodeEditorWindow { private Rect nodeRects; - public void Controls() { + public void Controls() { wantsMouseMove = true; Event e = Event.current; @@ -45,18 +45,15 @@ public partial class NodeEditorWindow { if (!draggedOutput.IsConnectedTo(hoveredPort)) { draggedOutputTarget = hoveredPort; } - } - else { + } else { draggedOutputTarget = null; } Repaint(); - } - else if (IsDraggingNode) { + } else if (IsDraggingNode) { draggedNode.rect.position = WindowToGridPosition(e.mousePosition) + dragOffset; Repaint(); } - } - else if (e.button == 1) { + } else if (e.button == 1) { panOffset += e.delta * zoom; isPanning = true; } @@ -71,8 +68,7 @@ public partial class NodeEditorWindow { if (IsHoveringPort) { if (hoveredPort.IsOutput) { draggedOutput = hoveredPort; - } - else { + } else { if (hoveredPort.IsConnected) { NodePort output = hoveredPort.Connection; hoveredPort.Disconnect(output); @@ -80,8 +76,7 @@ public partial class NodeEditorWindow { draggedOutputTarget = hoveredPort; } } - } - else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) { + } else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) { draggedNode = hoveredNode; dragOffset = hoveredNode.rect.position - WindowToGridPosition(e.mousePosition); } @@ -100,12 +95,10 @@ public partial class NodeEditorWindow { draggedOutputTarget = null; EditorUtility.SetDirty(graph); Repaint(); - } - else if (IsDraggingNode) { + } else if (IsDraggingNode) { draggedNode = null; } - } - else if (e.button == 1) { + } else if (e.button == 1) { if (!isPanning) ShowContextMenu(); isPanning = false; } @@ -188,4 +181,4 @@ public partial class NodeEditorWindow { Rect windowRect = new Rect(nodePos, new Vector2(node.rect.size.x / zoom, 30 / zoom)); return windowRect.Contains(mousePos); } -} +} \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index a573800..b17de1b 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -1,14 +1,14 @@ -using UnityEngine; -using UnityEditor; -using System; +using System; using System.Collections.Generic; +using UnityEditor; +using UnityEngine; /// Contains GUI methods public partial class NodeEditorWindow { private void OnGUI() { Event e = Event.current; - Matrix4x4 m = GUI.matrix; + Matrix4x4m = GUI.matrix; Controls(); DrawGrid(position, zoom, panOffset); @@ -21,13 +21,11 @@ public partial class NodeEditorWindow { public static void BeginZoomed(Rect rect, float zoom) { GUI.EndClip(); - + GUIUtility.ScaleAroundPivot(Vector2.one / zoom, rect.size * 0.5f); Vector4 padding = new Vector4(0, 22, 0, 0); padding *= zoom; - GUI.BeginClip(new Rect( - -((rect.width * zoom) - rect.width) * 0.5f, - -(((rect.height * zoom) - rect.height) * 0.5f) + (22 * zoom), + GUI.BeginClip(new Rect(-((rect.width * zoom) - rect.width) * 0.5f, -(((rect.height * zoom) - rect.height) * 0.5f) + (22 * zoom), rect.width * zoom, rect.height * zoom)); } @@ -36,7 +34,7 @@ public partial class NodeEditorWindow { GUIUtility.ScaleAroundPivot(Vector2.one * zoom, rect.size * 0.5f); Vector3 offset = new Vector3( (((rect.width * zoom) - rect.width) * 0.5f), - (((rect.height * zoom) - rect.height) * 0.5f) + (-22 * zoom)+22, + (((rect.height * zoom) - rect.height) * 0.5f) + (-22 * zoom) + 22, 0); GUI.matrix = Matrix4x4.TRS(offset, Quaternion.identity, Vector3.one); } @@ -48,7 +46,7 @@ public partial class NodeEditorWindow { Vector2 center = rect.size / 2f; Texture2D gridTex = NodeEditorResources.gridTexture; Texture2D crossTex = NodeEditorResources.crossTexture; - + // Offset from origin in tile units float xOffset = -(center.x * zoom + panOffset.x) / gridTex.width; float yOffset = ((center.y - rect.size.y) * zoom + panOffset.y) / gridTex.height; @@ -63,7 +61,7 @@ public partial class NodeEditorWindow { // Draw tiled background GUI.DrawTextureWithTexCoords(rect, gridTex, new Rect(tileOffset, tileAmount)); - GUI.DrawTextureWithTexCoords(rect, crossTex, new Rect(tileOffset + new Vector2(0.5f,0.5f), tileAmount)); + GUI.DrawTextureWithTexCoords(rect, crossTex, new Rect(tileOffset + new Vector2(0.5f, 0.5f), tileAmount)); } public static bool DropdownButton(string name, float width) { @@ -78,8 +76,7 @@ public partial class NodeEditorWindow { if (hoveredNode != null) { Node node = hoveredNode; contextMenu.AddItem(new GUIContent("Remove"), false, () => graph.RemoveNode(node)); - } - else { + } else { for (int i = 0; i < nodeTypes.Length; i++) { Type type = nodeTypes[i]; Type editorType = GetNodeEditor(type).GetType(); @@ -150,7 +147,7 @@ public partial class NodeEditorWindow { } BeginZoomed(position, zoom); - + foreach (Node node in graph.nodes) { NodeEditor nodeEditor = GetNodeEditor(node.GetType()); nodeEditor.target = node; @@ -159,8 +156,8 @@ public partial class NodeEditorWindow { Vector2 nodePos = GridToWindowPositionNoClipped(node.rect.position); //GUIStyle style = (node == selectedNode) ? (GUIStyle)"flow node 0 on" : (GUIStyle)"flow node 0"; - - GUILayout.BeginArea(new Rect(nodePos,new Vector2(nodeEditor.GetWidth(), 4000))); + + GUILayout.BeginArea(new Rect(nodePos, new Vector2(nodeEditor.GetWidth(), 4000))); GUIStyle style = NodeEditorResources.styles.nodeBody; GUILayout.BeginVertical(new GUIStyle(style)); @@ -193,4 +190,4 @@ public partial class NodeEditorWindow { if (onValidate != null && nodeHash != selectedNode.GetHashCode()) onValidate.Invoke(selectedNode, null); } } -} +} \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorPreferences.cs b/Scripts/Editor/NodeEditorPreferences.cs index 377936f..9d392b3 100644 --- a/Scripts/Editor/NodeEditorPreferences.cs +++ b/Scripts/Editor/NodeEditorPreferences.cs @@ -10,91 +10,89 @@ public static class NodeEditorPreferences { private static bool prefsLoaded = false; private static Dictionary typeColors; - private static Dictionary generatedTypeColors; + private static Dictionary generatedTypeColors; - [PreferenceItem ("Node Editor")] - private static void PreferencesGUI () { + [PreferenceItem("Node Editor")] + private static void PreferencesGUI() { if (!prefsLoaded) LoadPrefs(); - EditorGUILayout.LabelField("Type colors", EditorStyles.boldLabel); - - string[] typeKeys = new string[typeColors.Count]; - typeColors.Keys.CopyTo(typeKeys, 0); + EditorGUILayout.LabelField("Type colors", EditorStyles.boldLabel); + string[] typeKeys = new string[typeColors.Count]; + typeColors.Keys.CopyTo(typeKeys, 0); foreach (var key in typeKeys) { - EditorGUILayout.BeginHorizontal (); + EditorGUILayout.BeginHorizontal(); Color col = typeColors[key]; - col = EditorGUILayout.ColorField (key, col); - typeColors[key] = col; - if (!GUILayout.Toggle(true, "")) { - typeColors.Remove(key); - SavePrefs(); - } - EditorGUILayout.EndHorizontal (); + col = EditorGUILayout.ColorField(key, col); + typeColors[key] = col; + if (!GUILayout.Toggle(true, "")) { + typeColors.Remove(key); + SavePrefs(); + } + EditorGUILayout.EndHorizontal(); + } + if (GUI.changed) { + SavePrefs(); } - if(GUI.changed) { - SavePrefs(); - } - - string[] generatedTypeKeys = new string[generatedTypeColors.Count]; - generatedTypeColors.Keys.CopyTo(generatedTypeKeys,0); - foreach (var key in generatedTypeKeys) { - EditorGUILayout.BeginHorizontal (); + string[] generatedTypeKeys = new string[generatedTypeColors.Count]; + generatedTypeColors.Keys.CopyTo(generatedTypeKeys, 0); + foreach (var key in generatedTypeKeys) { + EditorGUILayout.BeginHorizontal(); Color col = generatedTypeColors[key]; - EditorGUI.BeginDisabledGroup(true); - col = EditorGUILayout.ColorField (key, col); - EditorGUI.EndDisabledGroup(); - if (GUILayout.Toggle(false, "")) { - typeColors.Add(key,generatedTypeColors[key]); - generatedTypeColors.Remove(key); - SavePrefs(); - } - EditorGUILayout.EndHorizontal (); + EditorGUI.BeginDisabledGroup(true); + col = EditorGUILayout.ColorField(key, col); + EditorGUI.EndDisabledGroup(); + if (GUILayout.Toggle(false, "")) { + typeColors.Add(key, generatedTypeColors[key]); + generatedTypeColors.Remove(key); + SavePrefs(); + } + EditorGUILayout.EndHorizontal(); } } - private static void LoadPrefs() { - generatedTypeColors = new Dictionary(); - typeColors = GetTypeColors(); + private static void LoadPrefs() { + generatedTypeColors = new Dictionary(); + typeColors = GetTypeColors(); prefsLoaded = true; - } - - private static void SavePrefs() { - if (!prefsLoaded) return; - string s = ""; - foreach (var item in typeColors) { - s+= item.Key+","+ColorUtility.ToHtmlStringRGB(item.Value)+ ","; - } - EditorPrefs.SetString("unec_typecolors",s); - } - - public static void SetDefaultTypeColors () { - EditorPrefs.SetString ("unec_typecolors", "int,2568CA,string,CE743A,bool,00FF00"); } - public static Dictionary GetTypeColors () { - if (prefsLoaded) return typeColors; - if (!EditorPrefs.HasKey ("unec_typecolors")) SetDefaultTypeColors (); - string[] data = EditorPrefs.GetString("unec_typecolors").Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries); - Dictionary dict = new Dictionary (); + private static void SavePrefs() { + if (!prefsLoaded) return; + string s = ""; + foreach (var item in typeColors) { + s += item.Key + "," + ColorUtility.ToHtmlStringRGB(item.Value) + ","; + } + EditorPrefs.SetString("unec_typecolors", s); + } + + public static void SetDefaultTypeColors() { + EditorPrefs.SetString("unec_typecolors", "int,2568CA,string,CE743A,bool,00FF00"); + } + + public static Dictionary GetTypeColors() { + if (prefsLoaded) return typeColors; + if (!EditorPrefs.HasKey("unec_typecolors")) SetDefaultTypeColors(); + string[] data = EditorPrefs.GetString("unec_typecolors").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + Dictionary dict = new Dictionary(); for (int i = 0; i < data.Length; i += 2) { Color col; - if (ColorUtility.TryParseHtmlString("#"+data[i+1], out col)) { + if (ColorUtility.TryParseHtmlString("#" + data[i + 1], out col)) { dict.Add(data[i], col); } } - return dict; - } + return dict; + } - /// Return color based on type - public static Color GetTypeColor(System.Type type) { - if (!prefsLoaded) LoadPrefs(); + /// Return color based on type + public static Color GetTypeColor(System.Type type) { + if (!prefsLoaded) LoadPrefs(); if (type == null) return Color.gray; if (typeColors.ContainsKey(type.Name)) return typeColors[type.Name]; if (generatedTypeColors.ContainsKey(type.Name)) return generatedTypeColors[type.Name]; - UnityEngine.Random.InitState (type.Name.GetHashCode ()); - generatedTypeColors.Add(type.Name, new Color (UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value)); + UnityEngine.Random.InitState(type.Name.GetHashCode()); + generatedTypeColors.Add(type.Name, new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value)); return generatedTypeColors[type.Name]; - } + } } \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorReflection.cs b/Scripts/Editor/NodeEditorReflection.cs index 2b8c46c..e6a18e6 100644 --- a/Scripts/Editor/NodeEditorReflection.cs +++ b/Scripts/Editor/NodeEditorReflection.cs @@ -1,14 +1,14 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; using System.Reflection; -using System.Linq; -using System; using UnityEngine; /// Contains reflection-related info public partial class NodeEditorWindow { [NonSerialized] private static Dictionary customNodeEditor; public static Type[] nodeTypes { get { return _nodeTypes != null ? _nodeTypes : _nodeTypes = GetNodeTypes(); } } - [NonSerialized] private static Type[] _nodeTypes = null; + [NonSerialized] private static Type[] _nodeTypes = null; public static NodeEditor GetNodeEditor(Type node) { if (customNodeEditor == null) CacheCustomNodeEditors(); @@ -21,7 +21,7 @@ public partial class NodeEditorWindow { return GetDerivedTypes(typeof(Node)); } - public static void CacheCustomNodeEditors(){ + public static void CacheCustomNodeEditors() { customNodeEditor = new Dictionary(); customNodeEditor.Add(typeof(Node), new NodeEditor()); //Get all classes deriving from NodeEditor via reflection @@ -41,10 +41,10 @@ public partial class NodeEditorWindow { return assembly.GetTypes().Where(t => !t.IsAbstract && baseType.IsAssignableFrom(t) - ).ToArray(); + ).ToArray(); } public static object ObjectFromType(Type type) { return Activator.CreateInstance(type); } -} +} \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorResources.cs b/Scripts/Editor/NodeEditorResources.cs index efc5e95..0b3bf3b 100644 --- a/Scripts/Editor/NodeEditorResources.cs +++ b/Scripts/Editor/NodeEditorResources.cs @@ -1,6 +1,6 @@ -using UnityEngine; +using System; using UnityEditor; -using System; +using UnityEngine; public static class NodeEditorResources { //Unec textures @@ -48,12 +48,12 @@ public static class NodeEditorResources { nodeBody = new GUIStyle(); nodeBody.normal.background = NodeEditorResources.nodeBody; nodeBody.border = new RectOffset(32, 32, 32, 32); - nodeBody.padding = new RectOffset(10, 10, 2, 2); + nodeBody.padding = new RectOffset(10, 10, 2, 2); } } public static Texture2D GenerateGridTexture() { - Texture2D tex = new Texture2D(64,64); + Texture2D tex = new Texture2D(64, 64); Color[] cols = new Color[64 * 64]; for (int y = 0; y < 64; y++) { for (int x = 0; x < 64; x++) { @@ -88,4 +88,4 @@ public static class NodeEditorResources { tex.Apply(); return tex; } -} +} \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorUtilities.cs b/Scripts/Editor/NodeEditorUtilities.cs index 4d6f904..51124d2 100644 --- a/Scripts/Editor/NodeEditorUtilities.cs +++ b/Scripts/Editor/NodeEditorUtilities.cs @@ -1,9 +1,9 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; -using UnityEngine; -using System.Reflection; using System.Linq; -using System; +using System.Reflection; +using UnityEngine; /// A set of editor-only utilities and extensions for UnityNodeEditorBase public static class NodeEditorUtilities { @@ -43,11 +43,11 @@ public static class NodeEditorUtilities { public static bool IsCastableTo(this Type from, Type to) { if (to.IsAssignableFrom(from)) return true; var methods = from.GetMethods(BindingFlags.Public | BindingFlags.Static) - .Where( - m => m.ReturnType == to && - (m.Name == "op_Implicit" || - m.Name == "op_Explicit") - ); + .Where( + m => m.ReturnType == to && + (m.Name == "op_Implicit" || + m.Name == "op_Explicit") + ); return methods.Count() > 0; } -} +} \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorWindow.cs b/Scripts/Editor/NodeEditorWindow.cs index aaa3aa1..34e627d 100644 --- a/Scripts/Editor/NodeEditorWindow.cs +++ b/Scripts/Editor/NodeEditorWindow.cs @@ -1,19 +1,19 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; -using UnityEngine; +using System.IO; using UnityEditor; -using System.IO; using UnityEditor.Callbacks; -using System; +using UnityEngine; [InitializeOnLoad] public partial class NodeEditorWindow : EditorWindow { /// Stores node positions for all nodePorts. public Dictionary portConnectionPoints { get { return _portConnectionPoints; } } private Dictionary _portConnectionPoints = new Dictionary(); - public NodeGraph graph; + public NodeGraph graph; public Vector2 panOffset { get { return _panOffset; } set { _panOffset = value; Repaint(); } } - private Vector2 _panOffset; + private Vector2 _panOffset; public float zoom { get { return _zoom; } set { _zoom = Mathf.Clamp(value, 1f, 5f); Repaint(); } } private float _zoom = 1; @@ -36,8 +36,7 @@ public partial class NodeEditorWindow : EditorWindow { if (AssetDatabase.Contains(graph)) { EditorUtility.SetDirty(graph); AssetDatabase.SaveAssets(); - } - else SaveAs(); + } else SaveAs(); } public void SaveAs() { @@ -61,7 +60,7 @@ public partial class NodeEditorWindow : EditorWindow { } public Vector2 GridToWindowPosition(Vector2 gridPosition) { - return (position.size * 0.5f) + (panOffset / zoom) + (gridPosition/zoom); + return (position.size * 0.5f) + (panOffset / zoom) + (gridPosition / zoom); } public Rect GridToWindowRect(Rect gridRect) { @@ -81,7 +80,6 @@ public partial class NodeEditorWindow : EditorWindow { selectedNode = node; } - [OnOpenAsset(0)] public static bool OnOpen(int instanceID, int line) { NodeGraph nodeGraph = EditorUtility.InstanceIDToObject(instanceID) as NodeGraph; diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 60db807..69f39e3 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -1,8 +1,8 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; -using UnityEngine; using System.Linq; -using System; +using UnityEngine; /// Base class for all nodes [Serializable] @@ -10,7 +10,7 @@ public abstract class Node : ScriptableObject { /// Name of the node [NonSerialized] public NodeGraph graph; - [SerializeField] public Rect rect = new Rect(0,0,200,200); + [SerializeField] public Rect rect = new Rect(0, 0, 200, 200); /// Input s. It is recommended not to modify these at hand. Instead, see [SerializeField] public List inputs = new List(); /// Output s. It is recommended not to modify these at hand. Instead, see @@ -99,17 +99,15 @@ public abstract class Node : ScriptableObject { [AttributeUsage(AttributeTargets.Field, AllowMultiple = true)] public class InputAttribute : Attribute { - public InputAttribute() { - } + public InputAttribute() { } } [AttributeUsage(AttributeTargets.Field, AllowMultiple = true)] public class OutputAttribute : Attribute { - public OutputAttribute() { - } + public OutputAttribute() { } } private void GetPorts() { NodeDataCache.UpdatePorts(this, inputs, outputs); } -} +} \ No newline at end of file diff --git a/Scripts/NodeDataCache.cs b/Scripts/NodeDataCache.cs index f14fb9f..93a0bd5 100644 --- a/Scripts/NodeDataCache.cs +++ b/Scripts/NodeDataCache.cs @@ -1,9 +1,9 @@ using System.Collections; using System.Collections.Generic; -using UnityEngine; -using System.Reflection; using System.Linq; +using System.Reflection; using UnityEditor; +using UnityEngine; /// Precaches reflection data in editor so we won't have to do it runtime public static class NodeDataCache { @@ -28,7 +28,7 @@ public static class NodeDataCache { else outputPorts.Add(new NodePort(portDataCache[nodeType][i], node)); } - for (int i = inputs.Count-1; i >= 0; i--) { + for (int i = inputs.Count - 1; i >= 0; i--) { int index = inputPorts.FindIndex(x => inputs[i].fieldName == x.fieldName); //If input nodeport does not exist, remove it if (index == -1) inputs.RemoveAt(i); @@ -60,7 +60,7 @@ public static class NodeDataCache { System.Type[] nodeTypes = assembly.GetTypes().Where(t => !t.IsAbstract && baseType.IsAssignableFrom(t) - ).ToArray(); + ).ToArray(); for (int i = 0; i < nodeTypes.Length; i++) { CachePorts(nodeTypes[i]); @@ -112,4 +112,4 @@ public static class NodeDataCache { this.Add(keys[i], values[i]); } } -} +} \ No newline at end of file diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs index 4c1d798..9a34667 100644 --- a/Scripts/NodeGraph.cs +++ b/Scripts/NodeGraph.cs @@ -1,6 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using UnityEngine; -using System; /// Base class for all node graphs [Serializable] @@ -45,8 +45,7 @@ public abstract class NodeGraph : ScriptableObject, ISerializationCallbackReceiv nodes.Clear(); } - public void OnBeforeSerialize() { - } + public void OnBeforeSerialize() { } public void OnAfterDeserialize() { /*for (int i = 0; i < nodes.Count; i++) { @@ -61,5 +60,4 @@ public abstract class NodeGraph : ScriptableObject, ISerializationCallbackReceiv nodes[i].VerifyConnections(); } }*/ -} - +} \ No newline at end of file diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 1980935..0afdb11 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -1,8 +1,8 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; -using UnityEngine; -using System; using System.Reflection; +using UnityEngine; [Serializable] public class NodePort { @@ -20,7 +20,6 @@ public class NodePort { public string fieldName { get { return _fieldName; } } - [SerializeField] public Node node; [SerializeField] private string _fieldName; [SerializeField] public Type type; @@ -71,7 +70,7 @@ public class NodePort { if (direction == port.direction) { Debug.LogWarning("Cannot connect two " + (direction == IO.Input ? "input" : "output") + " connections"); return; } connections.Add(new PortConnection(port)); if (port.connections == null) port.connections = new List(); - port.connections.Add(new PortConnection(this)); + port.connections.Add(new PortConnection(this)); node.OnCreateConnection(this, port); port.node.OnCreateConnection(this, port); } @@ -111,7 +110,7 @@ public class NodePort { } public void ClearConnections() { - while(connections.Count > 0) { + while (connections.Count > 0) { Disconnect(connections[0].Port); } } @@ -130,7 +129,7 @@ public class NodePort { } private NodePort GetPort() { - + for (int i = 0; i < node.OutputCount; i++) { if (node.outputs[i].fieldName == fieldName) return node.outputs[i]; } @@ -140,4 +139,4 @@ public class NodePort { return null; } } -} +} \ No newline at end of file