//--------------------------------------------------------------------------------- // static Deserialize //--------------------------------------------------------------------------------- public static T Deserialize <T>(string message) where T : new() { #if UNITY_FLASH && !UNITY_EDITOR Debug.LogError("XMLSerializer do not work with Flash !"); return(default(T)); #else XMLInStream stream = new XMLInStream(message); return((T)DeserializeObject(stream, typeof(T))); #endif }
//--------------------------------------------------------------------------------- // DeserializeListElement //--------------------------------------------------------------------------------- private static object DeserializeListElement(XMLInStream stream, Type type) { switch (type.ToString()) { case "System.String": string resS; stream.Content(out resS); return(resS); case "System.Single": float resF; stream.Content(out resF); return(resF); case "System.Int32": int resI; stream.Content(out resI); return(resI); case "System.Boolean": bool resB; stream.Content(out resB); return(resB); case "UnityEngine.Vector3": Vector3 resV; stream.Content(out resV); return(resV); case "UnityEngine.Quaternion": Quaternion resQ; stream.Content(out resQ); return(resQ); case "UnityEngine.Color": Color resC; stream.Content(out resC); return(resC); case "UnityEngine.Rect": Rect resR; stream.Content(out resR); return(resR); case "UnityEngine.Vector2": Vector2 resV2; stream.Content(out resV2); return(resV2); default: object created = DeserializeObject(stream, type); return(created); } }
//------------------------------------------------------------------------------------------------------------- // //------------------------------------------------------------------------------------------------------------- IEnumerator LoadAssetPatchInfo(string url) { WWW www = new WWW(url); // Wait for download to complete yield return(www); if (www.size == 0) { dfInterfaceManager.Instance.UpdatePatchStateString("패치정보를 찾을 수 없습니다."); dfGameManager.Instance.PatchEnd(); yield break; } XMLInStream inStream = new XMLInStream(www.text); int assetBundleCount = 0; inStream.List("assetbundleinfo", delegate(XMLInStream countStream) { countStream.Content("count", out assetBundleCount); }); inStream.List("assetbundle", delegate(XMLInStream bundleInfoStream) { AssetBundleInfo bundleInfo = new AssetBundleInfo(); bundleInfoStream.Content("bundlename", out bundleInfo._bundleName); bundleInfoStream.Content("objectname", out bundleInfo._objectName); bundleInfoStream.Content("objecttype", out bundleInfo._objectType); bundleInfoStream.Content("version", out bundleInfo._version); _assetBundelInfoList.Add(bundleInfo); }); if (assetBundleCount != _assetBundelInfoList.Count) { //출력 - 패치정보가 올바르지 않습니다. yield break; } if (_assetBundelInfoList.Count > 0) { StartCoroutine(LoadBundleCoroutine()); } }
private static object DeserializeListElement(XMLInStream stream, Type type) { //IL_0125: Unknown result type (might be due to invalid IL or missing references) //IL_0136: Unknown result type (might be due to invalid IL or missing references) //IL_0147: Unknown result type (might be due to invalid IL or missing references) //IL_0158: Unknown result type (might be due to invalid IL or missing references) //IL_0169: Unknown result type (might be due to invalid IL or missing references) switch (type.ToString()) { case "System.String": stream.Content(out string value9); return(value9); case "System.Single": stream.Content(out float value8); return(value8); case "System.Int32": stream.Content(out int value7); return(value7); case "System.Boolean": stream.Content(out bool value6); return(value6); case "UnityEngine.Vector3": stream.Content(out Vector3 value5); return(value5); case "UnityEngine.Quaternion": stream.Content(out Quaternion value4); return(value4); case "UnityEngine.Color": stream.Content(out Color value3); return(value3); case "UnityEngine.Rect": stream.Content(out Rect value2); return(value2); case "UnityEngine.Vector2": stream.Content(out Vector2 value); return(value); default: return(DeserializeObject(stream, type)); } }
//--------------------------------------------------------------------------------- // DeserializeObject //--------------------------------------------------------------------------------- private static object DeserializeObject(XMLInStream stream, Type type) { object data = Activator.CreateInstance(type); System.Reflection.FieldInfo[] fieldInfo = type.GetFields(); foreach (System.Reflection.FieldInfo info in fieldInfo) { switch (info.FieldType.ToString()) { case "System.String": string resS; if (stream.Has(info.Name)) { stream.Content(info.Name, out resS); info.SetValue(data, resS); } break; case "System.Single": float resF; if (stream.Has(info.Name)) { stream.Content(info.Name, out resF); info.SetValue(data, resF); } break; case "System.Int32": int resI; if (stream.Has(info.Name)) { stream.Content(info.Name, out resI); info.SetValue(data, resI); } break; case "System.Boolean": bool resB; if (stream.Has(info.Name)) { stream.Content(info.Name, out resB); info.SetValue(data, resB); } break; case "UnityEngine.Vector3": Vector3 resV; if (stream.Has(info.Name)) { stream.Content(info.Name, out resV); info.SetValue(data, resV); } break; case "UnityEngine.Quaternion": Quaternion resQ; if (stream.Has(info.Name)) { stream.Content(info.Name, out resQ); info.SetValue(data, resQ); } break; case "UnityEngine.Color": Color resC; if (stream.Has(info.Name)) { stream.Content(info.Name, out resC); info.SetValue(data, resC); } break; case "UnityEngine.Rect": Rect resR; if (stream.Has(info.Name)) { stream.Content(info.Name, out resR); info.SetValue(data, resR); } break; case "UnityEngine.Vector2": Vector2 resV2; if (stream.Has(info.Name)) { stream.Content(info.Name, out resV2); info.SetValue(data, resV2); } break; default: if (stream.Has(info.Name)) { if (info.FieldType.IsEnum) // Enum { string e; stream.Content(info.Name, out e); info.SetValue(data, Enum.Parse(info.FieldType, e)); } else if (info.FieldType.IsGenericType) // can only be a List { Type containedType = info.FieldType.GetGenericArguments()[0]; Type typeList = typeof(List <>); Type actualType = typeList.MakeGenericType(containedType); MethodInfo addMethod = actualType.GetMethod("Add"); object list = Activator.CreateInstance(actualType); stream.Start(info.Name).List("item", delegate(XMLInStream stream2){ object o = DeserializeListElement(stream2, containedType); addMethod.Invoke(list, new object[] { o }); }).End(); info.SetValue(data, list); } else if (info.FieldType.IsArray) // Array { Type containedType = info.FieldType.GetElementType(); Type typeList = typeof(List <>); Type actualType = typeList.MakeGenericType(containedType); MethodInfo addMethod = actualType.GetMethod("Add"); MethodInfo toArrayMethod = actualType.GetMethod("ToArray"); object list = Activator.CreateInstance(actualType); stream.Start(info.Name).List("item", delegate(XMLInStream stream2){ object o = DeserializeListElement(stream2, containedType); addMethod.Invoke(list, new object[] { o }); }).End(); object array = toArrayMethod.Invoke(list, new object[] {}); info.SetValue(data, array); } else // Embedded Object { stream.Start(info.Name); object created = DeserializeObject(stream, info.FieldType); stream.End(); info.SetValue(data, created); } } break; } } return(data); }
void ExampleAddressBook() { // Let's serialize a simple address book XMLOutStream outStream = new XMLOutStream(); outStream.Start("book") .Content("version", 1) .Start("entry") .Content("name", "Mike") .Content("age", 24) .Attribute("already-met", true) .Attribute("married", true) .End() .Start("entry") .Content("name", "John") .Content("age", 32) .Attribute("already-met", false) .End() .End(); string serialized = outStream.Serialize(); // serialized outputs this XML structure: // // // <book> // <entry already-met="true" married=true> // <name>Mike</name> // <age>24</age> // </entry> // <entry already-met="false"> // <name>John</name> // <age>32</age> // </entry> // </book> // // // Deserialize it XMLInStream inStream = new XMLInStream(serialized); // the XML root (here 'book' is automatically entered to parse the content) int version; List <AddressBookEntry> entries = new List <AddressBookEntry>(); inStream.Content("version", out version) .List("entry", delegate(XMLInStream entryStream){ string name; int age; bool alreadyMet; bool married = false; entryStream.Content("name", out name) .Content("age", out age) .Attribute("already-met", out alreadyMet) .AttributeOptional("married", ref married); entries.Add(new AddressBookEntry(name, age, alreadyMet, married)); }); // Now version and entries are set Debug.Log("SERIALIZED XML STRING: " + serialized); string result = ""; foreach (AddressBookEntry entry in entries) { result += entry.ToString() + " "; } Debug.Log("XML DESERIALIZATION of " + entries.Count + " entries: " + result); }
void ExampleAddressBook() { // Let's serialize a simple address book XMLOutStream outStream = new XMLOutStream(); outStream.Start("book") .Content("version", 1) .Start("entry") .Content("name", "Mike") .Content("age", 24) .Attribute("already-met", true) .Attribute("married", true) .End() .Start("entry") .Content("name", "John") .Content("age", 32) .Attribute("already-met", false) .End() .End(); string serialized = outStream.Serialize(); // serialized outputs this XML structure: // // // <book> // <entry already-met="true" married=true> // <name>Mike</name> // <age>24</age> // </entry> // <entry already-met="false"> // <name>John</name> // <age>32</age> // </entry> // </book> // // // Deserialize it XMLInStream inStream = new XMLInStream(serialized); // the XML root (here 'book' is automatically entered to parse the content) int version; List<AddressBookEntry> entries = new List<AddressBookEntry>(); inStream.Content("version", out version) .List("entry", delegate(XMLInStream entryStream){ string name; int age; bool alreadyMet; bool married = false; entryStream.Content("name", out name) .Content("age", out age) .Attribute("already-met", out alreadyMet) .AttributeOptional("married", ref married); entries.Add(new AddressBookEntry(name, age, alreadyMet, married)); }); // Now version and entries are set Debug.Log("SERIALIZED XML STRING: " + serialized); string result = ""; foreach(AddressBookEntry entry in entries) result += entry.ToString() + " "; Debug.Log("XML DESERIALIZATION of " + entries.Count + " entries: " + result); }
public static Object InstantiateSkeletonUnit(SkeletonDataAsset skeletonDataAsset, Skin skin = null) { if (!File.Exists("Assets/ExternalRes/Unit/Unit.json")) { return(null); } JSONNode jsonConfig = null; using (FileStream fs = new FileStream("Assets/ExternalRes/Unit/Unit.json", FileMode.Open)) { string sr = new StreamReader(fs).ReadToEnd(); JSONParser parser = new JSONParser(); jsonConfig = parser.Parse(new FlashCompatibleTextReader(sr)); } //读取配置文件 Dictionary <string, UnitConfig> UnitConfigs = new Dictionary <string, UnitConfig>(); TextAsset textAsset = AssetDatabase.LoadAssetAtPath("Assets/ExternalRes/Config/UnitConfig.xml", typeof(TextAsset)) as TextAsset; XMLInStream stream = new XMLInStream(textAsset.text); stream.List("item", delegate(XMLInStream itemStream) { UnitConfig ufg = new UnitConfig(itemStream); if (!UnitConfigs.ContainsKey(ufg.ResourceIcon)) { UnitConfigs.Add(ufg.ResourceIcon, ufg); } }); string path = AssetDatabase.GetAssetPath(skeletonDataAsset); string fpath = path.Replace("_SkeletonData", "_Controller").Replace(".asset", ".controller"); string mpath = path.Replace("_SkeletonData", "_Controller").Replace(".asset", ".controller.meta"); if (File.Exists(fpath)) { File.Delete(fpath); File.Delete(mpath); Debug.Log("删除旧的Controller:" + fpath); } fpath = path.Replace("_SkeletonData", "").Replace(".asset", ".prefab"); mpath = path.Replace("_SkeletonData", "").Replace(".asset", ".prefab.meta"); if (File.Exists(fpath)) { File.Delete(fpath); File.Delete(mpath); Debug.Log("删除旧的Prefab:" + fpath); } skeletonDataAsset.controller = null; // 创建状态机 SkeletonBaker.GenerateMecanimAnimationClips(skeletonDataAsset, jsonConfig); // 创建Animator Object GameObject go = GenerateAnimatorObject(skeletonDataAsset, skin); // 伙伴RectTransform go.AddComponent <RectTransform>(); RectTransform rt = go.GetComponent <RectTransform>(); rt.pivot = new Vector2(0.5f, 0); // 伙伴UnitSoundBehaviour UnitSoundBehaviour usb = go.AddComponent <UnitSoundBehaviour>(); //伙伴UnitIdleChangeBehaviour go.AddComponent <UnitIdleChangeBehaviour>(); // 创建Audio Object string name = path.Replace("Assets/ExternalRes/Unit/", "").Replace("_SkeletonData.asset", ""); name = name.Substring(name.IndexOf("/") + 1); if (UnitConfigs.ContainsKey(name)) { usb.SetSoundSource(AutoPrefab.GenerateAudioObject(go, UnitConfigs[name])); } // 创建Prefab string dataPath = AssetDatabase.GetAssetPath(skeletonDataAsset); string prefabPath = dataPath.Replace("_SkeletonData", "").Replace(".asset", ".prefab"); Object prefab = AutoPrefab.GenerateUnitPrefab(go, prefabPath); // 销毁Animator Object Object.DestroyImmediate(go); // 设置asset bundle name AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(prefab)).assetBundleName = "units/" + name; return(prefab); }
static void BuildAllUnits() { DirectoryInfo dInfo = new DirectoryInfo("Assets/ExternalRes/Unit"); if (!dInfo.Exists) { Debug.Log("不存在目录"); return; } DirectoryInfo assetDire = new DirectoryInfo("."); //读取配置文件 Dictionary <string, UnitConfig> UnitConfigs = new Dictionary <string, UnitConfig>(); TextAsset textAsset = AssetDatabase.LoadAssetAtPath("Assets/ExternalRes/Config/UnitConfig.xml", typeof(TextAsset)) as TextAsset; XMLInStream stream = new XMLInStream(textAsset.text); stream.List("item", delegate(XMLInStream itemStream) { UnitConfig ufg = new UnitConfig(itemStream); if (!UnitConfigs.ContainsKey(ufg.ResourceIcon)) { UnitConfigs.Add(ufg.ResourceIcon, ufg); } }); DirectoryInfo[] unitsDires = dInfo.GetDirectories(); for (int i = 0, n = unitsDires.Length; i < n; i++) { dInfo = unitsDires[i]; FileInfo[] fileInfoArr = dInfo.GetFiles("*_SkeletonData.asset"); for (int j = 0, m = fileInfoArr.Length; j < m; j++) { FileInfo fInfo = fileInfoArr[j]; //Debug.Log(fInfo.FullName); //Debug.Log(fInfo.Name); string relativePath = fInfo.FullName.Replace(assetDire.FullName, ""); relativePath = relativePath.Remove(0, 1); //Debug.Log("relativePath=" + relativePath); Object o = AssetDatabase.LoadAssetAtPath(relativePath, typeof(Object)); if (o == null) { Debug.LogWarning("null"); } string guid = AssetDatabase.AssetPathToGUID(relativePath); string skinName = EditorPrefs.GetString(guid + "_lastSkin", ""); if (!UnitConfigs.ContainsKey(dInfo.Name)) { Debug.LogFormat("{0},没有相关配置,忽略", dInfo.Name); continue; } Debug.LogFormat("正在处理 name={0}", dInfo.Name); UnitConfig ufg = UnitConfigs[dInfo.Name]; try { if (ufg.isEnemy) { InstantiateSkeletonEnemy((SkeletonDataAsset)o, skinName); } else { InstantiateSkeletonUnit((SkeletonDataAsset)o, skinName); } } catch (System.Exception e) { Debug.LogFormat("{0} 处理失败,重新处理。msg={1}", dInfo.Name, e.Message); j--; } SceneView.RepaintAll(); } } }
static private List <AssetBundleBuild> BuildUnitAssetBundles(BuildAssetBundleOptions opt, BuildTarget biuldTarget, Dictionary <string, AssetBundleInfo> assetInfoDict, string outputPath) { Debug.Log("處理資源: Unit"); //讀取UnitConfig配置文件,只有這個文件配置的Unit才會記錄在reslist.json裡 string text = File.ReadAllText(string.Format("{0}/Config/UnitConfig.xml", ASSET_BUNDLE_SRC_DIR)); XMLInStream stream = new XMLInStream(text); Dictionary <string, UnitConfig> UnitConfigs = new Dictionary <string, UnitConfig>(); stream.List("item", delegate(XMLInStream itemStream) { UnitConfig ufg = new UnitConfig(itemStream); //注意這裡用的是ResourceIcon字段 if (!UnitConfigs.ContainsKey(ufg.ResourceIcon)) { UnitConfigs.Add(ufg.ResourceIcon, ufg); } }); List <AssetBundleBuild> ret = new List <AssetBundleBuild>(); //生成prefab的AssetBundle foreach (var pair in UnitConfigs) { DirectoryInfo dInfo = new DirectoryInfo(string.Format("{0}/Unit/{1}", ASSET_BUNDLE_SRC_DIR, pair.Key)); //這裡注意轉成小寫 string assetbundlename = "unit/" + pair.Key.ToLower(); if (!dInfo.Exists) { // 如果UnitConfig配置裡沒有的字段,需要從reslist.json裡清理掉 if (assetInfoDict.ContainsKey(assetbundlename)) { assetInfoDict.Remove(assetbundlename); } continue; } AssetBundleBuild abb = new AssetBundleBuild(); abb.assetBundleName = assetbundlename; string[] assetNames = { string.Format("{0}/Unit/{1}/{2}.prefab", ASSET_BUNDLE_SRC_DIR, pair.Key, pair.Key) }; abb.assetNames = assetNames; ret.Add(abb); } //生成UnitImg的AssetBundle foreach (var pair in UnitConfigs) { //夥伴圖1 string p1 = string.Format("{0}/UnitImg1/{1}.png", ASSET_BUNDLE_SRC_DIR, pair.Key); //夥伴圖2 string p2 = string.Format("{0}/UnitImg2/{1}.png", ASSET_BUNDLE_SRC_DIR, pair.Key); FileInfo img1Info = new FileInfo(p1); FileInfo img2Info = new FileInfo(p2); string assetbundlename = string.Format("unitimg/{0}img", pair.Key.ToLower()); if (!img1Info.Exists && !img2Info.Exists) { continue; } AssetBundleBuild abb = new AssetBundleBuild(); abb.assetBundleName = assetbundlename; string[] assetNames = { p1, p2 }; abb.assetNames = assetNames; ret.Add(abb); } return(ret); }
public static T Deserialize <T>(string message) where T : new() { XMLInStream stream = new XMLInStream(message); return((T)DeserializeObject(stream, typeof(T))); }
private static object DeserializeObject(XMLInStream stream, Type type) { //IL_01ec: Unknown result type (might be due to invalid IL or missing references) //IL_021f: Unknown result type (might be due to invalid IL or missing references) //IL_0252: Unknown result type (might be due to invalid IL or missing references) //IL_0285: Unknown result type (might be due to invalid IL or missing references) //IL_02b8: Unknown result type (might be due to invalid IL or missing references) object obj = Activator.CreateInstance(type); FieldInfo[] fields = type.GetFields(); FieldInfo[] array = fields; Type containedType; MethodInfo addMethod; object list; Type containedType2; MethodInfo addMethod2; object list2; foreach (FieldInfo fieldInfo in array) { switch (fieldInfo.FieldType.ToString()) { case "System.String": if (stream.Has(fieldInfo.Name)) { stream.Content(fieldInfo.Name, out string value12); fieldInfo.SetValue(obj, value12); } break; case "System.Single": if (stream.Has(fieldInfo.Name)) { stream.Content(fieldInfo.Name, out float value7); fieldInfo.SetValue(obj, value7); } break; case "System.Int32": if (stream.Has(fieldInfo.Name)) { stream.Content(fieldInfo.Name, out int value9); fieldInfo.SetValue(obj, value9); } break; case "System.Boolean": if (stream.Has(fieldInfo.Name)) { stream.Content(fieldInfo.Name, out bool value5); fieldInfo.SetValue(obj, value5); } break; case "UnityEngine.Vector3": if (stream.Has(fieldInfo.Name)) { stream.Content(fieldInfo.Name, out Vector3 value10); fieldInfo.SetValue(obj, value10); } break; case "UnityEngine.Quaternion": if (stream.Has(fieldInfo.Name)) { stream.Content(fieldInfo.Name, out Quaternion value8); fieldInfo.SetValue(obj, value8); } break; case "UnityEngine.Color": if (stream.Has(fieldInfo.Name)) { stream.Content(fieldInfo.Name, out Color value6); fieldInfo.SetValue(obj, value6); } break; case "UnityEngine.Rect": if (stream.Has(fieldInfo.Name)) { stream.Content(fieldInfo.Name, out Rect value4); fieldInfo.SetValue(obj, value4); } break; case "UnityEngine.Vector2": if (stream.Has(fieldInfo.Name)) { stream.Content(fieldInfo.Name, out Vector2 value11); fieldInfo.SetValue(obj, value11); } break; default: if (stream.Has(fieldInfo.Name)) { if (fieldInfo.FieldType.IsEnum) { stream.Content(fieldInfo.Name, out string value); fieldInfo.SetValue(obj, Enum.Parse(fieldInfo.FieldType, value)); } else if (fieldInfo.FieldType.IsGenericType) { containedType = fieldInfo.FieldType.GetGenericArguments()[0]; Type typeFromHandle = typeof(List <>); Type type2 = typeFromHandle.MakeGenericType(containedType); addMethod = type2.GetMethod("Add"); list = Activator.CreateInstance(type2); stream.Start(fieldInfo.Name).List("item", delegate(XMLInStream stream2) { object obj3 = DeserializeListElement(stream2, containedType); addMethod.Invoke(list, new object[1] { obj3 }); }).End(); fieldInfo.SetValue(obj, list); } else if (fieldInfo.FieldType.IsArray) { containedType2 = fieldInfo.FieldType.GetElementType(); Type typeFromHandle2 = typeof(List <>); Type type3 = typeFromHandle2.MakeGenericType(containedType2); addMethod2 = type3.GetMethod("Add"); MethodInfo method = type3.GetMethod("ToArray"); list2 = Activator.CreateInstance(type3); stream.Start(fieldInfo.Name).List("item", delegate(XMLInStream stream2) { object obj2 = DeserializeListElement(stream2, containedType2); addMethod2.Invoke(list2, new object[1] { obj2 }); }).End(); object value2 = method.Invoke(list2, new object[0]); fieldInfo.SetValue(obj, value2); } else { stream.Start(fieldInfo.Name); object value3 = DeserializeObject(stream, fieldInfo.FieldType); stream.End(); fieldInfo.SetValue(obj, value3); } } break; } } return(obj); }