public static void Connect(int parentID, int childID)
        {
            BTNode parent = BTEditorManager.GetNodeByID(parentID);
            BTNode child  = BTEditorManager.GetNodeByID(childID);

            if (parent != null && parent.CanConnectChild && child != null)
            {
                parent.ConnectChild(child);
                // SortChildren(parent);
                Dirty();
            }
            else
            {
                Debug.LogWarning(string.Format("{0} can't accept child {1}", parent, child));
            }
        }
        public static void Add(int parentID, Vector2 position, System.Type nodeType)
        {
            BTNode node = null;

            if (nodeType != null)
            {
                node = SelectTree.CreateNode(nodeType);
            }
            else
            {
                throw new System.NotImplementedException(string.Format("Create {0} node failure", nodeType));
            }

            // GUID
            node.GUID = System.Guid.NewGuid().ToString();

            SelectTree.nodeDic.Add(node.GetInstanceID(), node);

            // Editor Position
            BTNode parent = GetNodeByID(parentID);

            if (parent != null && parent.CanConnectChild)
            {
                if (parent.ChildCount > 0)
                {
                    BTNode lastSibling = parent.Children[parent.ChildCount - 1];
                    node.editorPosition = lastSibling.editorPosition + new Vector2(GridRenderer.step.x * 10, 0);
                }
                else
                {
                    node.editorPosition = new Vector2(parent.editorPosition.x, parent.editorPosition.y + GridRenderer.step.y * 10);
                }
                parent.ConnectChild(node);
                // SortChildren(parent);
            }
            else
            {
                float x       = position.x;
                float y       = position.y;
                float xOffset = x % GridRenderer.step.x;
                float yOffset = y % GridRenderer.step.y;
                node.editorPosition = new Vector2(x - xOffset, y - yOffset);
            }

            Dirty();
        }
예제 #3
0
		private BTNode DeserializeSubTree(XmlElement el, BehaviorTree bt)
		{
			BTNode node = null;
			if (el.Name == "BaseRoot")
			{
				if (!bt.nodeDic.ContainsKey(baseRootID))
				{
					node = bt.CreateNode<BaseRoot>();
					baseRootID = node.GetInstanceID();
				}
				else
				{
					return null;
				}
			}
			else if (el.Name == "Root")
			{
				if (!bt.nodeDic.ContainsKey(rootID))
				{
					node = bt.CreateNode<Root>();
					rootID = node.GetInstanceID();
				}
				else
				{
					return null;
				}
			}
			else if (!string.IsNullOrEmpty(el.Name))
			{
				string className = string.Empty;
				string script = el.GetAttribute("script");
				if (string.IsNullOrEmpty(script))
				{
					className = el.Name;
					// 原数据格式兼容处理
					if (className.Equals("ConditionVariable"))
					{
						className = "Condition";
					}
				}
				else
				{
					// 原数据格式兼容处理
					string[] names = script.Split(new char[]{ '.' });
					if (names.Length > 0)
					{
						className = names[names.Length - 1];
					}
				}

				if (BTEditorManager.NodeTypes.ContainsKey(className))
				{
					System.Type type = BTEditorManager.NodeTypes[className];
					node = bt.CreateNode(type);
				}
				else
				{
					Debug.LogError("Can't find the class type, name=" + className);
				}
			}
			else
			{
				throw new System.NotImplementedException(string.Format("{0} deserialization not implemented", el.Name));
			}

			// 解析参数数据
			if (node != null)
			{
				// 基类通用数据
				node.comment = el.GetAttribute("comment");
				string replaceShowName = el.GetAttribute("replaceShowName");
				if (!string.IsNullOrEmpty(replaceShowName))
				{
					node.replaceShowName = bool.Parse(replaceShowName);
				}

				float x = float.Parse(el.GetAttribute("editorx"));
				float y = float.Parse(el.GetAttribute("editory"));
				node.editorPosition = new Vector2(x, y);
				node.GUID = el.GetAttribute("guid");
				node.debugId = el.GetAttribute("debugId");

				// 本类数据
				// 原数据格式兼容处理
				Dictionary<string, string> parameters = new Dictionary<string, string>();
				foreach (XmlNode paramNode in el.ChildNodes)
				{
					XmlElement paramEl = paramNode as XmlElement;
					if (paramEl != null && paramEl.Name == "param")
					{
						string key = paramEl.GetAttribute("key");
						if (!string.IsNullOrEmpty(key))
						{
							parameters[key] = paramEl.GetAttribute("value");
						}
					}
				}

				FieldInfo[] fieldInfos = node.GetType().GetFields(BindingFlags.Public
					| BindingFlags.Instance | BindingFlags.DeclaredOnly);
				foreach (var field in fieldInfos)
				{
					string value;
					if (parameters.ContainsKey(field.Name))
					{
						value = parameters[field.Name];
					}
					else
					{
						value = el.GetAttribute(field.Name);
					}

					if (!string.IsNullOrEmpty(value))
					{
						// 枚举类型特殊处理,防止数据不存在
						if (field.FieldType.BaseType == typeof(System.Enum))
						{
							if (System.Enum.IsDefined(field.FieldType, value))
							{
								field.SetValue(node, System.Enum.Parse(field.FieldType, value));
							}
						}
						else
						{
							field.SetValue(node, TypeDescriptor.GetConverter(field.FieldType).ConvertFrom(value));
						}
					}
				}

				bt.nodeDic.Add(node.GetInstanceID(), node);

				foreach (XmlNode xmlNode in el.ChildNodes)
				{
					XmlElement childEl = xmlNode as XmlElement;
					if (childEl == null || childEl.Name == "param")
					{
						continue;
					}

					BTNode child = DeserializeSubTree(childEl, bt);
					if (child != null)
					{
						node.ConnectChild(child);
					}
				}
			}

			return node;
		}