Пример #1
0
        //FlowNode
        public static UnityEditor.GenericMenu AppendFlowNodesMenu(this FlowGraph graph, UnityEditor.GenericMenu menu, string baseCategory, Vector2 pos, Port sourcePort, object dropInstance)
        {
            var infos       = EditorUtils.GetScriptInfosOfType(typeof(FlowNode));
            var generalized = new List <System.Type>();

            foreach (var _info in infos)
            {
                var info = _info;
                if (sourcePort != null)
                {
                    if (generalized.Contains(info.originalType))
                    {
                        continue;
                    }

                    if (sourcePort.IsValuePort())
                    {
                        if (info.originalType.IsGenericTypeDefinition)
                        {
                            var genericInfo = info.MakeGenericInfo(sourcePort.type);
                            if (genericInfo != null)
                            {
                                info = genericInfo;
                                generalized.Add(info.originalType);
                            }
                        }
                    }

                    var           definedInputTypesAtts  = info.type.RTGetAttributesRecursive <FlowNode.ContextDefinedInputsAttribute>();
                    var           definedOutputTypesAtts = info.type.RTGetAttributesRecursive <FlowNode.ContextDefinedOutputsAttribute>();
                    System.Type[] concreteInputTypes     = null;
                    if (definedInputTypesAtts.Length > 0)
                    {
                        concreteInputTypes = definedInputTypesAtts.Select(att => att.types).Aggregate((x, y) => { return(x.Union(y).ToArray()); });
                        concreteInputTypes = AlterTypesDefinition(concreteInputTypes, info.type);
                    }
                    System.Type[] concreteOutputTypes = null;
                    if (definedOutputTypesAtts.Length > 0)
                    {
                        concreteOutputTypes = definedOutputTypesAtts.Select(att => att.types).Aggregate((x, y) => { return(x.Union(y).ToArray()); });
                        concreteOutputTypes = AlterTypesDefinition(concreteOutputTypes, info.type);
                    }

                    if (sourcePort is ValueOutput || sourcePort is FlowOutput)
                    {
                        if (concreteInputTypes == null || !concreteInputTypes.Any(t => t != null && t.IsAssignableFrom(sourcePort.type)))
                        {
                            continue;
                        }
                    }

                    if (sourcePort is ValueInput || sourcePort is FlowInput)
                    {
                        if (concreteOutputTypes == null || !concreteOutputTypes.Any(t => t != null && sourcePort.type.IsAssignableFrom(t)))
                        {
                            continue;
                        }
                    }
                }
                var category = string.Join("/", new string[] { baseCategory, info.category, info.name }).TrimStart('/');
                menu.AddItem(new GUIContent(category, info.icon, info.description), false, () => { graph.AddFlowNode(info.type, pos, sourcePort, dropInstance); });
            }
            return(menu);
        }