public object[] Import(string path) { if (string.IsNullOrEmpty(path)) { return(null); } if (!path.EndsWith(".xml")) { path += ".xml"; } string allPath = string.Concat(ApplicationPath.PersistentRootPath, path); if (!File.Exists(allPath)) { UnityEngine.Debug.LogWarning("<<XmlLocalData , Import>> Cant find file . path is " + allPath); return(null); } XmlDocument doc = new XmlDocument(); doc.LoadXml(File.ReadAllText(allPath)); XmlNode rootNode = doc.SelectSingleNode("data"); List <object> deserialieList = new List <object>(); Dictionary <string, object> dataDic = new Dictionary <string, object>(); foreach (XmlNode childNode in rootNode.ChildNodes) { dataDic.Clear(); readXml(childNode, dataDic); string nodeName = childNode.Name; Type classType = Type.GetType(nodeName); object objInstance = InjectDataBinder.BindingData(classType, dataDic); deserialieList.Add(objInstance); } return(deserialieList.ToArray()); }
public object[] Import(string path) { if (string.IsNullOrEmpty(path)) { return(null); } if (!path.EndsWith(".json")) { path += ".json"; } string txt = FileGameUtil.ReadText(path); if (string.IsNullOrEmpty(txt)) { return(null); } List <object> instanceList = new List <object>(); ArrayList dataJson = MiniJson.jsonDecode(txt) as ArrayList; foreach (object obj in dataJson) { Hashtable itemTab = obj as Hashtable; Type classType = Type.GetType(Convert.ToString(itemTab["type"])); Hashtable dataTab = itemTab["data"] as Hashtable; Dictionary <string, object> reflectDic = CopyTo(dataTab); object instance = InjectDataBinder.BindingData(classType, reflectDic); if (instance != null) { instanceList.Add(instance); } } return(instanceList.ToArray()); }
public bool Export <T>(string path, object[] objArr) where T : Attribute { if (objArr == null || objArr.Length <= 0 || string.IsNullOrEmpty(path)) { return(false); } if (!path.EndsWith(".json")) { path += ".json"; } StringBuilder sb = new StringBuilder(); sb.Append("["); foreach (object obj in objArr) { sb.AppendFormat("{{\"type\":\"{0}\",", obj.GetType().Name); sb.Append("\"data\":{"); Dictionary <string, object> reflectDic = InjectDataBinder.ReflectAllData <T>(obj); saveJson(sb, reflectDic); sb.Append("}},"); } if (sb[sb.Length - 1] == ',') { sb.Remove(sb.Length - 1, 1); } sb.Append("]"); FileGameUtil.WriteText(path, sb.ToString()); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif return(true); }
public bool Export <T>(string path, object[] objArr) where T : Attribute { if (objArr == null || objArr.Length <= 0 || string.IsNullOrEmpty(path)) { return(false); } if (!path.EndsWith(".xml")) { path += ".xml"; } string allPath = string.Concat(ApplicationPath.PersistentRootPath, path); ApplicationPath.CreateDirectory(allPath); XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", "yes"); XmlElement rootEle = doc.CreateElement("data"); foreach (object obj in objArr) { Dictionary <string, object> dataDic = InjectDataBinder.ReflectAllData <T>(obj); XmlElement element = doc.CreateElement(obj.GetType().Name); this.saveXml(doc, element, dataDic); rootEle.AppendChild(element); } doc.AppendChild(dec); doc.AppendChild(rootEle); doc.Save(allPath); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif return(true); }