Esempio n. 1
0
 public void Init(FSMStateNode node, List <FSMVisualCondition> conditions, Port port, int selectedPortIndex)
 {
     fsmNode                = node;
     this.selectedPort      = port;
     this.selectedPortIndex = selectedPortIndex;
     this.conditions        = conditions;
 }
        /// <summary>
        /// 添加节点
        /// </summary>
        public FSMStateNode AddNode(Vector2 position)
        {
            var node = new FSMStateNode(this);

            node.SetPosition(new Rect(position, defaultNodeSize));
            node.RefreshExpandedState();
            node.RefreshPorts();

            AddElement(node);
            return(node);
        }
        private void Save()
        {
            // 新建的FSMAsset,还未保存
            if (!currentFSMAsset)
            {
                currentFSMAsset = CreateInstance <FSMData>();
                var path = OpenSavePanel();
                AssetDatabase.CreateAsset(currentFSMAsset, path);
                AssetDatabase.Refresh();
            }

            currentFSMAsset.NodeDatas = new List <FSMStateNodeData>();
            currentFSMAsset.PortDatas = new List <FSMPortData>();
            graphView.nodes.ForEach(tempNode =>
            {
                FSMStateNode node         = tempNode as FSMStateNode;
                var fsmStateNodeData      = new FSMStateNodeData();
                fsmStateNodeData.GUID     = node.viewDataKey;
                fsmStateNodeData.Position = node.GetPosition().position;

                int portIndex = 0;
                foreach (Port port in node.outputContainer.Children())
                {
                    var portData           = new FSMPortData();
                    var edge               = port.connections.First();
                    portData.FromNodeGUID  = node.viewDataKey;
                    portData.ToNodeGUID    = edge.input.node.viewDataKey;
                    portData.Index         = portIndex;
                    portData.ConditionGUID = node.PortIndexToConditions[portIndex];
                    portIndex++;

                    currentFSMAsset.PortDatas.Add(portData);
                }
                ;

                currentFSMAsset.NodeDatas.Add(fsmStateNodeData);
            });

            EditorUtility.SetDirty(currentFSMAsset);
            AssetDatabase.SaveAssets();
        }
        private void Load(FSMData asset)
        {
            if (!asset)
            {
                return;
            }
            currentFSMAsset = asset;
            if (currentFSMAsset.NodeDatas == null || currentFSMAsset.NodeDatas.Count == 0)
            {
                return;
            }

            // 清除Node和Edge
            foreach (var edge in graphView.edges.ToList())
            {
                graphView.RemoveElement(edge);
            }
            foreach (var node in graphView.nodes.ToList())
            {
                graphView.RemoveElement(node);
            }

            foreach (var data in currentFSMAsset.NodeDatas)
            {
                var node = graphView.AddNode(data.Position);
                node.viewDataKey = data.GUID;
                node.SetPosition(new Rect(data.Position, new Vector2(200f, 150f)));
            }

            if (currentFSMAsset.PortDatas == null || currentFSMAsset.PortDatas.Count == 0)
            {
                return;
            }
            foreach (var portData in currentFSMAsset.PortDatas)
            {
                FSMStateNode fromNode = null;
                FSMStateNode toNode   = null;
                foreach (FSMStateNode tempNode in graphView.nodes.ToList())
                {
                    if (tempNode.viewDataKey == portData.FromNodeGUID)
                    {
                        fromNode = tempNode;
                    }
                    if (tempNode.viewDataKey == portData.ToNodeGUID)
                    {
                        toNode = tempNode;
                    }
                }
                if (fromNode == null || toNode == null)
                {
                    continue;
                }
                var fromPort = fromNode.AddCondition();
                if (!string.IsNullOrEmpty(portData.ConditionGUID))
                {
                    fromNode.SetCondition(portData.Index, fromPort, portData.ConditionGUID);
                }
                graphView.Add(fromPort.ConnectTo(toNode.InputPort));
            }
            ;
        }
 public void Init(FSMStateNode node, List <FSMVisualState> stateTypes)
 {
     fsmNode     = node;
     this.states = stateTypes;
 }