Example #1
0
        public static NodeBase DeserializeNode(ByteBuffer bb, int nodeId)
        {
            NodeBase  resultNode = null;
            GraphInfo graphInfo  = GraphInfo.GetRootAsGraphInfo(bb);

            int nodeCount = graphInfo.NodesLength;

            for (int i = 0; i < nodeCount; i++)
            {
                NodeInfo?nodeInfo = graphInfo.Nodes(i);
                if (!nodeInfo.HasValue)
                {
                    continue;
                }

                if (nodeInfo.Value.NodeId != nodeId)
                {
                    continue;
                }

                string          nodeClassName = nodeInfo.Value.NodeClassTypeName;
                Func <NodeBase> nodeFactory;
                if (nodeFactoryDictionary.TryGetValue(nodeClassName, out nodeFactory))
                {
                    resultNode = nodeFactory();
                }

                if (resultNode == null)
                {
                    UnityEngine.Debug.LogErrorFormat("无法实例化节点 {0},是否添加了新节点,但是没有生成静态代码?", nodeClassName);
                    return(null);
                }

                Type nodeType = resultNode.GetType();

                resultNode.nodeId = nodeInfo.Value.NodeId;
                resultNode.isCreateSequenceNode = nodeType == typeof(CreateSequenceNode);

                //处理input
                //使用静态生成代码来处理
                int inputFieldCount = nodeInfo.Value.InputPortInfosLength;
                for (int j = 0; j < inputFieldCount; j++)
                {
                    NodeInputFieldInfo nodeInputFieldInfo = nodeInfo.Value.InputPortInfos(j).Value;
//                    WriteNodeInputVariableField(resultNode, nodeType, nodeInfo.Value.InputPortInfos(j).Value);
                    Action <NodeBase, NodeInputFieldInfo> parseFunction;
                    if (inputPortParseFuncDictionary.TryGetValue(nodeType, out parseFunction))
                    {
                        parseFunction(resultNode, nodeInputFieldInfo);
                    }
                    else
                    {
                        UnityEngine.Debug.LogErrorFormat("无法处理节点 {0} 的input port,是否添加了新节点,但是没有生成静态代码?", nodeClassName);
                    }
                }

                //处理flow out
                WriteFlowOutField(resultNode, nodeType, nodeInfo.Value);

                //处理outputPortFuncs
                //使用静态生成代码来处理
                Action <NodeBase> outputFuncsInitAction;
                if (nodeOutputInitFuncDictionary.TryGetValue(nodeType, out outputFuncsInitAction))
                {
                    outputFuncsInitAction(resultNode);
                }

                //还原EntranceNode或者CreateSequenceNode的右侧节点id
                EntranceNodeBase entranceNodeBase = resultNode as EntranceNodeBase;
                if (entranceNodeBase != null)
                {
                    int[] rightSideNodeIds = new int[nodeInfo.Value.RightSideNodeIdsLength];
                    for (int j = 0; j < rightSideNodeIds.Length; j++)
                    {
                        rightSideNodeIds[j] = nodeInfo.Value.RightSideNodeIds(j);
                    }

                    entranceNodeBase.rightSideNodeIds = rightSideNodeIds;
                }
                else
                {
                    CreateSequenceNode createSequenceNode = resultNode as CreateSequenceNode;
                    if (createSequenceNode != null)
                    {
                        int[] rightSideNodeIds = new int[nodeInfo.Value.RightSideNodeIdsLength];
                        for (int j = 0; j < rightSideNodeIds.Length; j++)
                        {
                            rightSideNodeIds[j] = nodeInfo.Value.RightSideNodeIds(j);
                        }

                        createSequenceNode.rightSideNodeIds = rightSideNodeIds;
                    }
                }
            }

            return(resultNode);
        }
Example #2
0
        public void FlowToNode(int nodeId)
        {
            //流向下一个节点,下一个节点必须在这个技能序列内
            NodeBase targetNode = GetNode(nodeId);

            if (targetNode == null)
            {
                return;
            }

#if UNITY_EDITOR
            if (runningNodeList.Contains(targetNode))
            {
                Debug.LogErrorFormat("节点{0}已经在运行了,属于重复运行。多实例运行要使用CreateSequence节点", nodeId);
            }
#endif

            //进入创建序列流程
            if (targetNode.isCreateSequenceNode)
            {
                if (childSequencePool == null)
                {
                    childSequencePool = new Dictionary <int, Queue <NodeSequence> >();
                }

                bool needCreateNewSequence = true;
                //尝试从池中取
                if (childSequencePool.ContainsKey(nodeId))
                {
                    Queue <NodeSequence> poolSequence = childSequencePool[nodeId];
                    if (poolSequence != null && poolSequence.Count > 0)
                    {
//                        Debug.Log("cache hit");
                        NodeSequence nodeSequence = poolSequence.Dequeue();
                        nodeSequence.Reset();
                        runningChildSequenceList.Add(nodeSequence);
                        nodeSequence.Start();

                        needCreateNewSequence = false;
                    }
                }

                if (needCreateNewSequence)
                {
                    CreateSequenceNode createSequenceNode = targetNode as CreateSequenceNode;
                    NodeSequence       newNodeSequence    = new NodeSequence(nodeId, createSequenceNode.rightSideNodeIds, GraphBehaviour, this);

                    runningChildSequenceList.Add(newNodeSequence);
                    newNodeSequence.Start();
                }
            }
            else
            {
                targetNode.OnEnter();
                //该节点直接在OnEnter就结束了,不用加入runningNodeList
                if (targetNode.isFinish)
                {
                    return;
                }

                if (!runningNodeList.Contains(targetNode))
                {
                    runningNodeList.Add(targetNode);
                }
            }
        }
Example #3
0
        /// <summary>
        /// 反射设置input 端口的参数
        /// 暂时不用
        /// </summary>
        /// <param name="nodeInstance"></param>
        /// <param name="nodeType"></param>
        /// <param name="inputFieldInfo"></param>
        public static void WriteNodeInputVariableField(NodeBase nodeInstance, Type nodeType, NodeInputFieldInfo inputFieldInfo)
        {
            string fieldName = inputFieldInfo.FieldName;

            inputVariableFieldInfo =
                nodeType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (inputVariableFieldInfo == null)
            {
                UnityEngine.Debug.LogErrorFormat("类 {0} 中没有找到 NodeInputVariable: {1}", nodeType.Namespace, fieldName);
                return;
            }

            string valueTypeName = inputFieldInfo.ValueTypeName;
            Type   valueType     = Type.GetType(valueTypeName);

            if (valueType == null)
            {
                valueType = Type.GetType(valueTypeName + ",UnityEngine");
                if (valueType == null)
                {
                    valueType = Type.GetType(valueTypeName + ",Assembly-CSharp");
                }
            }

            object nodeInputVariableInstance = Activator.CreateInstance(typeof(NodeInputVariable <>).MakeGenericType(valueType));

            inputVariableFieldInfo.SetValue(nodeInstance, nodeInputVariableInstance);

            if (inputFieldInfo.TargetNodeId >= 0 && inputFieldInfo.TargetPortId >= 0)
            {
                FieldInfo targetNodeIdFieldInfo =
                    nodeInputVariableInstance.GetType().GetField("targetNodeId", BindingFlags.Public | BindingFlags.Instance);
                targetNodeIdFieldInfo.SetValue(nodeInputVariableInstance, inputFieldInfo.TargetNodeId);

                FieldInfo targetPortIdFieldInfo =
                    nodeInputVariableInstance.GetType().GetField("targetPortId", BindingFlags.Public | BindingFlags.Instance);
                targetPortIdFieldInfo.SetValue(nodeInputVariableInstance, inputFieldInfo.TargetPortId);
            }
            else
            {
                FieldInfo targetNodeIdFieldInfo =
                    nodeInputVariableInstance.GetType().GetField("targetNodeId", BindingFlags.Public | BindingFlags.Instance);
                targetNodeIdFieldInfo.SetValue(nodeInputVariableInstance, -1);

                FieldInfo targetPortIdFieldInfo =
                    nodeInputVariableInstance.GetType().GetField("targetPortId", BindingFlags.Public | BindingFlags.Instance);
                targetPortIdFieldInfo.SetValue(nodeInputVariableInstance, -1);

                string valueString = inputFieldInfo.ValueString;

                //值类型
                if (valueType.IsValueType || valueType == typeof(string))
                {
                    FieldInfo valueFieldInfo =
                        nodeInputVariableInstance.GetType().GetField("value", BindingFlags.Public | BindingFlags.Instance);

                    if (valueType == typeof(int))
                    {
                        int value = int.Parse(valueString);
                        valueFieldInfo.SetValue(nodeInputVariableInstance, value);
                    }
                    else if (valueType == typeof(float))
                    {
                        float value = float.Parse(valueString);
                        valueFieldInfo.SetValue(nodeInputVariableInstance, value);
                    }
                    else if (valueType == typeof(string))
                    {
                        valueFieldInfo.SetValue(nodeInputVariableInstance, valueString);
                    }
                    else if (valueType == typeof(bool))
                    {
                        bool value = Boolean.Parse(valueString);
                        valueFieldInfo.SetValue(nodeInputVariableInstance, value);
                    }
                    else if (valueType.IsEnum)
                    {
                        object value = Enum.Parse(valueType, valueString);
                        valueFieldInfo.SetValue(nodeInputVariableInstance, value);
                    }
                }
                //引用类型
                else
                {
                    FieldInfo valueFieldInfo =
                        nodeInputVariableInstance.GetType().GetField("value", BindingFlags.Public | BindingFlags.Instance);
                    if (valueType == typeof(List <int>))
                    {
                        List <int> list = ParseIntList(valueString);
                        valueFieldInfo.SetValue(nodeInputVariableInstance, list);
                    }
                    else if (valueType == typeof(LayerMaskWrapper))
                    {
                        LayerMaskWrapper layerMaskWrapper = valueString;
                        valueFieldInfo.SetValue(nodeInputVariableInstance, layerMaskWrapper);
                    }
                    else if (valueType == typeof(VariableWrapper))
                    {
                        int variableId;
                        if (int.TryParse(valueString, out variableId))
                        {
                            VariableWrapper variableWrapper = variableId;
                            valueFieldInfo.SetValue(nodeInputVariableInstance, variableWrapper);
                        }
                    }
                }
            }
        }
Example #4
0
        public void Update(float deltaTime)
        {
            if (executeFinishNodeList.Count > 0)
            {
                for (int i = 0; i < executeFinishNodeList.Count; i++)
                {
                    NodeBase executeFinishNode = executeFinishNodeList[i];
                    executeFinishNode.OnExit();

                    if (runningNodeList.Contains(executeFinishNode))
                    {
                        runningNodeList.Remove(executeFinishNode);
                    }
                }

                executeFinishNodeList.Clear();
            }

            for (int i = 0; i < runningNodeList.Count; i++)
            {
                runningNodeList[i].OnUpdate(deltaTime);
            }

            if (runningChildSequenceList != null && runningChildSequenceList.Count > 0)
            {
                for (int i = runningChildSequenceList.Count - 1; i >= 0; i--)
                {
                    NodeSequence nodeSequence = runningChildSequenceList[i];
                    nodeSequence.Update(deltaTime);

                    if (nodeSequence.IsFinish)
                    {
                        nodeSequence.Reset();

                        if (childSequencePool == null)
                        {
                            childSequencePool = new Dictionary <int, Queue <NodeSequence> >();
                        }

                        //放回池中
                        if (childSequencePool.ContainsKey(nodeSequence.RootNode.nodeId))
                        {
                            Queue <NodeSequence> poolQueue = childSequencePool[nodeSequence.RootNode.nodeId];
                            if (poolQueue == null)
                            {
                                poolQueue = new Queue <NodeSequence>();
                            }

                            poolQueue.Enqueue(nodeSequence);
                        }
                        else
                        {
                            Queue <NodeSequence> poolQueue = new Queue <NodeSequence>();
                            poolQueue.Enqueue(nodeSequence);
                            childSequencePool.Add(nodeSequence.RootNode.nodeId, poolQueue);
                        }

                        runningChildSequenceList.RemoveAt(i);
                    }
                }
            }
        }