Beispiel #1
0
        /// <summary>
        /// 从XML中读取Dictionary
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="_XDoc"></param>
        /// <param name="nodeNames"></param>
        /// <returns></returns>
        public static Dictionary <string, T> ReadStringDictionaryFromXML <T>(this XDocument _XDoc, string[] nodeNames)
        {
            XElement _Root = _XDoc.Root;

            for (int i = 0; i < nodeNames.Length; i++)
            {
                if (_Root == null)
                {
                    //Debug.Log("结点不存在,返回空StringDictionary");
                    return(new Dictionary <string, T>());
                }

                _Root = _Root.Element(nodeNames[i]);
            }

            Dictionary <string, T> _dictionary = new Dictionary <string, T>();

            foreach (XElement el in _Root.Elements())
            {
                if (_dictionary.ContainsKey(el.Name.ToString()))
                {
                    Debug.Log("同一元素在XML中重复定义");
                }
                _dictionary.Add(el.Name.ToString(), (T)StringEx.GetValue(el.Value, typeof(T)));
            }
            return(_dictionary);
        }
Beispiel #2
0
        /// <summary>
        /// 将Dictionary<int, Dictionary<string, string>>转成Dictionary<int, T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dictionary"></param>
        /// <returns></returns>
        public static Dictionary <int, T> ParserStringDict2ClassDict <T>(Dictionary <int, Dictionary <string, string> > dictionary)
        {
            Dictionary <int, T> dataDic = new Dictionary <int, T>();

            PropertyInfo[] properties = typeof(T).GetProperties();
            foreach (KeyValuePair <int, Dictionary <string, string> > pair in dictionary)
            {
                T propInstance       = Activator.CreateInstance <T>();
                PropertyInfo[] array = properties;
                for (int i = 0; i < array.Length; i++)
                {
                    PropertyInfo propInfo = array[i];
                    if (propInfo.Name == "id")
                    {
                        //Key值为序号
                        propInfo.SetValue(propInstance, pair.Key, null);
                    }
                    else if (pair.Value.ContainsKey(propInfo.Name))
                    {
                        object propValue = StringEx.GetValue(pair.Value[propInfo.Name], propInfo.PropertyType);
                        propInfo.SetValue(propInstance, propValue, null);
                        HandleProperty(propInstance, propInfo);
                    }
                    else
                    {
                        Debug.unityLogger.LogError("Add New Value", propInfo.Name + "Not in the Xml");
                    }
                }
                dataDic.Add(pair.Key, propInstance);
            }
            return(dataDic);
        }
Beispiel #3
0
 private void Init()
 {
     if (EditorPrefs.HasKey(EditorPrefNames.AssetBundle.Paths))
     {
         pathList = StringEx.GetValue(EditorPrefs.GetString(EditorPrefNames.AssetBundle.Paths), typeof(List <string>)) as List <string>;
     }
     else
     {
         pathList = new List <string>();
     }
 }
Beispiel #4
0
        /// <summary>
        /// 从节点中读取Dictionary
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_root"></param>
        /// <returns></returns>
        public static Dictionary <string, T> ReadDictionaryFromElement <T>(this XElement _root)
        {
            Dictionary <string, T> _dictionary = new Dictionary <string, T>();

            foreach (XElement el in _root.Elements())
            {
                if (_dictionary.ContainsKey(el.Name.ToString()))
                {
                    Debug.Log("同一元素在XML中重复定义");
                }
                _dictionary.Add(el.Name.ToString(), (T)StringEx.GetValue(el.Value, typeof(T)));
            }
            return(_dictionary);
        }
Beispiel #5
0
        /// <summary>
        /// 数据应用到组件
        /// </summary>
        /// <param name="comp"></param>
        /// <param name="data"></param>
        public static void Apply(Component comp, JsonData data)
        {
            var fields = comp.GetType().GetFields();

            foreach (var field in fields)
            {
                //if (field.GetCustomAttributes(typeof(JsonLoadable), true).Length == 0)
                //    continue;

                if (!data.Keys.Contains(field.Name) || !data.Keys.Contains(field.Name + "#Type") ||
                    !data.Keys.Contains(field.Name + "#Assembly"))
                {
                    return;
                }

                Type type = AssemblyManager.GetAssemblyType(data[field.Name + "#Assembly"].ToString(),
                                                            data[field.Name + "#Type"].ToString());
                if (type == null)
                {
                    return;
                }

                if (StringEx.IsConvertableType(field.FieldType))
                {
                    object value = StringEx.GetValue(data[field.Name].ToString(), type);
                    field.SetValue(comp, value);
                }
                else if (field.FieldType == typeof(GameObject))
                {
                    object value = PathConvertToGameObject(comp.gameObject, data[field.Name].ToString());
                    field.SetValue(comp, value);
                }
                else if (typeof(Component).IsAssignableFrom(field.FieldType))
                {
                    object value = PathConvertToGameObject(comp.gameObject, data[field.Name].ToString())
                                   .GetComponent(field.FieldType);
                    field.SetValue(comp, value);
                }
                else
                {
                    Debug.LogError("不支持的序列化类型:" + type.Name);
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// 从XML中读取数值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_XDoc"></param>
        /// <param name="nodeNames"></param>
        /// <param name="_defValue"></param>
        /// <returns></returns>
        public static T ReadValueFromXML <T>(this XDocument _XDoc, string[] nodeNames, T _defValue = default(T))
        {
            XElement _Root = _XDoc.Root;

            for (int i = 0; i < nodeNames.Length; i++)
            {
                if (_Root == null)
                {
                    return(_defValue);
                }

                _Root = _Root.Element(nodeNames[i]);
            }
            if (_Root == null)
            {
                return(_defValue);
            }
            return((T)StringEx.GetValue(_Root.Value, typeof(T)));
        }
 private void SetChangedProp(XElement propEle, GameObject obj)
 {
     if (propEle == null || obj == null || !propEle.HasElements)
     {
         return;
     }
     foreach (XElement ele in propEle.Elements())
     {
         if (ele.Name.Equals("Transform"))
         {
             continue;
         }
         Component comp     = obj.GetComponent(ele.Name.LocalName);
         Type      compType = comp.GetType();
         foreach (XAttribute attr in ele.Attributes())
         {
             PropertyInfo propInfo = compType.GetProperty(attr.Name.LocalName);
             //Debug.Log("变量名为 " + attr.Name.LocalName + " 变量类型为 " + propInfo.PropertyType.Name + " 值为 " + attr.Value);
             propInfo.SetValue(comp, StringEx.GetValue(attr.Value, propInfo.PropertyType), null);
         }
     }
 }
 private void SetChangedField(XElement fieldEle, GameObject obj)
 {
     if (fieldEle == null || obj == null || !fieldEle.HasElements)
     {
         return;
     }
     foreach (XElement ele in fieldEle.Elements())
     {
         if (ele.Name.Equals("Transform"))
         {
             continue;
         }
         Component comp     = obj.GetComponent(ele.Name.LocalName);
         Type      compType = comp.GetType();
         foreach (XAttribute attr in ele.Attributes())
         {
             FieldInfo fieldInfo = compType.GetField(attr.Name.LocalName);
             Debug.Log("变量名为 " + attr.Name.LocalName + " 变量类型为 " + fieldInfo.FieldType.Name);
             fieldInfo.SetValue(comp, StringEx.GetValue(attr.Value, fieldInfo.FieldType));
         }
     }
 }
Beispiel #9
0
        /// <summary>
        /// 从节点中获取数据
        /// </summary>
        /// <param name="_el"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object ReadValueFromElement(this XElement _el, Type type)
        {
            object genericArgument;

            if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(List <>)))
            {
                System.Type     type2       = type.GetGenericArguments()[0];
                List <XElement> list        = new List <XElement>(_el.Elements());
                object          constructor = type.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                foreach (XElement el in list)
                {
                    genericArgument = el.ReadValueFromElement(type2);
                    type.GetMethod("Add").Invoke(constructor, new object[] { genericArgument });
                }
                return(constructor);
            }
            else if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Dictionary <,>)))
            {
                System.Type[] genericArguments           = type.GetGenericArguments();
                Dictionary <string, XElement> dictionary = _el.GetElementDict();
                var constructor = type.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                if (!genericArguments[0].Equals(typeof(string)))
                {
                    Debug.LogError("键值必须为字符串!");
                    return(null);
                }
                foreach (KeyValuePair <string, XElement> pair in dictionary)
                {
                    genericArgument = pair.Value.ReadValueFromElement(genericArguments[1]);
                    type.GetMethod("Add").Invoke(constructor, new object[] { pair.Key, genericArgument });
                }
                return(constructor);
            }
            else
            {
                return(StringEx.GetValue(_el.Value, type));
            }
        }
Beispiel #10
0
 /// <summary>
 /// 从节点中读取值
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="_el"></param>
 /// <param name="_defValue"></param>
 /// <returns></returns>
 public static T ReadValueFromElement <T>(this XElement _el, T _defValue = default(T))
 {
     return((T)StringEx.GetValue(_el.Value, typeof(T)));
 }