public static bool CanCreateGraphData(ScriptableObject parentObject, FieldInfo fieldInfo, out GraphData graphData) { graphData = null; Type fieldValueType = fieldInfo.FieldType; if (fieldValueType.IsGenericType && (fieldValueType.GetGenericTypeDefinition() == typeof(List<>)) && typeof(ScriptableObject).IsAssignableFrom( fieldValueType.GetGenericArguments()[0])){ object[] attributes = fieldInfo.GetCustomAttributes(false); if (attributes == null || attributes.Length == 0){ return false; } GraphAttribute attribute = attributes .ToList().First((arg) => arg.GetType() == typeof(GraphAttribute)) as GraphAttribute; if (attribute != null){ object fieldValue = fieldInfo.GetValue(parentObject); if (fieldValue == null){ var newList = Activator.CreateInstance(fieldValueType); fieldInfo.SetValue(parentObject, newList); fieldValue = newList; } SerializedObject serializedObject = new SerializedObject(parentObject); graphData = new GraphData(); graphData.ItemBaseType = fieldValueType.GetGenericArguments()[0]; graphData.ItemList = fieldValue as IList; graphData.PropertyName = fieldInfo.Name; graphData.ParentObject = parentObject; graphData.SerializedItemList = serializedObject.FindProperty(fieldInfo.Name); if (string.IsNullOrEmpty(graphData.PropertyName)){ graphData.PropertyName = fieldInfo.Name; } graphData.StartNode = null; if (!string.IsNullOrEmpty(attribute.StartNode)){ graphData.StartNode = serializedObject.FindProperty(attribute.StartNode); if (graphData.StartNode == null){ Debug.LogError("Cant find property with name " + attribute.StartNode +" for this graph"); } else if (false){ //fixme through reflexion get field type graphData.StartNode = null ; Debug.LogError("Start node type is not assignable from graph node type"); } } graphData.SetDefaultStartNodeIfNothingSelected(); return true; } } return false; }
void OnGUI() { if (!CheckSelectedObject()){ EditorGUILayout.HelpBox("Please select scriptable object instance which has a [Graph] attribute", MessageType.Info,true); return; } if (Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed"){ Event.current.Use(); return; } if (Event.current.type == EventType.Used){ return; } bool firsDrawCallOnThisGraph = CurrentGraph != lastUsedGraph; JointHighlight = (JointHighlightHolder) GUIUtility.GetStateObject(typeof(JointHighlightHolder) ,GUIUtility.GetControlID(FocusType.Passive)); List<Node> nodes = new List<Node>(); OnGUIToolBar(); scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.Width(contentWidth), GUILayout.Height(contentHeight)); if (connectionCollection != null){ foreach (Connection connectionGUI in connectionCollection.allConnections) { connectionGUI.OnGUI(); } } foreach(var graphData in CurrentGraph.ItemList){ Node nodeGUI = Node.GetInstance((ScriptableObjectNode)graphData, this); nodes.Add(nodeGUI); } BeginWindows(); foreach (Node node in nodes){ node.OnGUI(); } EndWindows(); connectionCollection = new ConnectionsCollection (nodes, this); HandleDraggConnections(nodes, connectionCollection.LastDraggedConnection); EditorGUILayout.EndScrollView(); UpdateContentSize(nodes); if (firsDrawCallOnThisGraph ){ Repaint(); } lastUsedGraph = CurrentGraph; }
public static bool CanCreateGraphData(ScriptableObject parentObject, FieldInfo fieldInfo, out GraphData graphData) { graphData = null; Type fieldValueType = fieldInfo.FieldType; if (fieldValueType.IsGenericType && (fieldValueType.GetGenericTypeDefinition() == typeof(List <>)) && typeof(ScriptableObject).IsAssignableFrom(fieldValueType.GetGenericArguments()[0])) { object[] attributes = fieldInfo.GetCustomAttributes(false); if (attributes == null || attributes.Length == 0) { return(false); } GraphAttribute attribute = attributes .ToList().First((arg) => arg.GetType() == typeof(GraphAttribute)) as GraphAttribute; if (attribute != null) { object fieldValue = fieldInfo.GetValue(parentObject); if (fieldValue == null) { var newList = Activator.CreateInstance(fieldValueType); fieldInfo.SetValue(parentObject, newList); fieldValue = newList; } SerializedObject serializedObject = new SerializedObject(parentObject); graphData = new GraphData(); graphData.ItemBaseType = fieldValueType.GetGenericArguments()[0]; graphData.ItemList = fieldValue as IList; graphData.PropertyName = fieldInfo.Name; graphData.ParentObject = parentObject; graphData.SerializedItemList = serializedObject.FindProperty(fieldInfo.Name); if (string.IsNullOrEmpty(graphData.PropertyName)) { graphData.PropertyName = fieldInfo.Name; } graphData.StartNode = null; if (!string.IsNullOrEmpty(attribute.StartNode)) { graphData.StartNode = serializedObject.FindProperty(attribute.StartNode); if (graphData.StartNode == null) { Debug.LogError("Cant find property with name " + attribute.StartNode + " for this graph"); } else if (false) //fixme through reflexion get field type { graphData.StartNode = null; Debug.LogError("Start node type is not assignable from graph node type"); } } graphData.SetDefaultStartNodeIfNothingSelected(); return(true); } } return(false); }