示例#1
0
        public BTAction CreateAction(string actionName)
        {
            BehaveAction behave = m_BehaveActions.Find(ret => ret.name.Equals(actionName));

            System.Type actionType = (behave != null ? behave.type : null);

            if (actionType == null)
            {
                Debug.LogError("Can't find action : " + "[" + actionName + "]");
                return(null);
            }
            BTAction action = System.Activator.CreateInstance(actionType) as BTAction;

            action.SetName(actionName);
            return(action);
        }
示例#2
0
        static void InitData(string btPath, BTAction action, XmlElement e)
        {
            if (!s_Fields.ContainsKey(action.Name))
            {
                s_Fields.Add(action.Name, new List <FieldInfo>());
            }

            if (action != null && e != null)
            {
                //foreach (FieldInfo field in action.GetType().GetFields())
                //{
                //    foreach (object attribute in field.GetCustomAttributes(true))
                //    {
                //        if (typeof(BehaveAttribute).IsInstanceOfType(attribute))
                //        {
                //            string error = SetValue(field, action, e);
                //            if (error != "")
                //            {
                //                //Debug.LogError("Action : " + action.Name + "["+ error +"]");
                //            }
                //            continue;
                //        }
                //    }
                //}

                //Profiler.BeginSample("Resolver");
                object[] members = action.GetType().GetCustomAttributes(true);
                for (int i = 0; i < members.Length; i++)
                {
                    if (typeof(BehaveAction).IsInstanceOfType(members[i]))
                    {
                        BehaveAction behave = members[i] as BehaveAction;
                        XmlNodeList  nodes  = e.GetElementsByTagName("Data");
                        for (int j = 0; j < nodes.Count; j++)
                        {
                            XmlElement d = nodes[j] as XmlElement;
                            if (behave.dataType != null)
                            {
                                object      obj      = System.Activator.CreateInstance(behave.dataType);
                                string      dataName = XmlUtil.GetAttributeString(d, "Name");
                                FieldInfo[] fields   = obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
                                for (int k = 0; k < fields.Length; k++)
                                {
                                    FieldInfo field = fields[k];
                                    object[]  attrs = field.GetCustomAttributes(true);
                                    for (int n = 0; n < attrs.Length; n++)
                                    {
                                        if (typeof(BehaveAttribute).IsInstanceOfType(attrs[n]))
                                        {
                                            if (s_Fields[action.Name].Find(ret => ret.Name == field.Name) == null)
                                            {
                                                s_Fields[action.Name].Add(field);
                                            }

                                            string error = SetValue(field, obj, d);
                                            if (error != "")
                                            {
                                                Debug.LogError(btPath + "-" + "Action:" + action.Name + "-" + "Data : " + dataName + "-" + "[" + error + "]");
                                            }
                                            continue;
                                        }
                                    }
                                }
                                action.AddData(dataName, obj);
                            }
                        }
                    }
                }
                //Profiler.EndSample();
            }
        }