NodeInspector.Editor.GraphData.CanCreateGraphData C# (CSharp) Method

CanCreateGraphData() public static method

public static CanCreateGraphData ( ScriptableObject parentObject, FieldInfo fieldInfo, GraphData &graphData ) : bool
parentObject UnityEngine.ScriptableObject
fieldInfo System.Reflection.FieldInfo
graphData GraphData
return bool
        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;
        }

Usage Example

        bool CheckSelectedObject()
        {
            if (Selection.activeObject == null || !(Selection.activeObject is ScriptableObject))
            {
                return(false);
            }
            ScriptableObject so = Selection.activeObject as ScriptableObject;

            graphList = new Dictionary <string, GraphData>();
            foreach (FieldInfo fieldInfo in  so.GetType().GetFields())
            {
                GraphData data;
                if (GraphData.CanCreateGraphData(so, fieldInfo, out data))
                {
                    string uniqueName = data.PropertyName;
                    int    i          = 0;
                    while (graphList.Keys.Contains(uniqueName))
                    {
                        uniqueName = data.PropertyName + " [" + (++i) + "]";
                    }
                    graphList.Add(uniqueName, data);
                }
            }
            //return nodes.Count > 0;
            return(true);
        }