using System; using System.Collections.Generic; using System.IO; using UnityEngine; #if FUNCELL_EDITOR namespace SceneEditor.Proxy.Editor #else namespace SceneEditor.Proxy.Plugin #endif { /// /// MapInfo数据的读操作 /// public static class MapInfoDataReader { public static Dictionary _mapInfoBodys = new Dictionary(); /// /// 读取场景对象数据,sceneName.bytes /// 读取二进制数据 /// /// /// /// public static MapInfoBody LoadSceneObjectData(String pathName, String fileName) { if (String.IsNullOrEmpty(fileName)) return null; string filePath = pathName + "/" + fileName; string mapName = fileName.Replace("_MapInfo.bytes", ""); try { var bytes = File.ReadAllBytes(filePath); if (bytes != null) { int readLen = 0; MapInfoHeader header = new MapInfoHeader(); header.Read(bytes, ref readLen); MapInfoBody body = new MapInfoBody(header); if (body.Read(bytes)) { if (!_mapInfoBodys.ContainsKey(mapName)) { _mapInfoBodys[mapName] = body; } return body; } } } catch (Exception e) { Debug.LogErrorFormat("LoadSceneObjectData failed! {0}", filePath); Debug.LogException(e); } return null; } ///// ///// 为世界地图读取MapInfo数据,sceneName.bytes ///// 文件是二进制数据 ///// ///// ///// ///// //public static MapInfoBody LoadSceneObjectData_ForWorldMap(String pathName, String sceneName) //{ // String[] dirs = sceneName.Split(new char[] { '/' }); // if (dirs.Length == 0) // { // return null; // } // String fileName = dirs[dirs.Length - 1]; // MemoryStream fs = null; // try // { // var bytes = File.ReadAllBytes(pathName + '/' + fileName); // if (bytes != null) // { // fs = new MemoryStream(bytes); // BinaryReader r = new BinaryReader(fs); // MapInfoHeader header = new MapInfoHeader(); // header.Read(r); // r.BaseStream.Seek(0, SeekOrigin.Begin); // // read header successed // MapInfoBody body = new MapInfoBody(header); // if (body.Read(r)) // { // return body; // } // } // } // catch (IOException e) // { // Debug.LogFormat("Save MapInfo data failed! " , e); // } // finally // { // if (fs != null) // { // fs.Close(); // } // } // return null; //} /// /// 使用ObjectSerializer,从xml文件中加载MapInfo数据 /// /// /// public static GameObject LoadScene(string mapName) { GameObject go = GameObject.Find(MapInfoConstDefine.SceneObjectsRootName); if (go == null) { go = new GameObject(); go.name = MapInfoConstDefine.SceneObjectsRootName; go.transform.position = Vector3.zero; go.transform.rotation = Quaternion.identity; go.transform.localScale = Vector3.one; } if(_mapInfoBodys.ContainsKey(mapName)) { MapInfoBody body = _mapInfoBodys[mapName]; //加载路点信息 Transform wayPointParent = go.transform.Find("[WayPoint]"); if (wayPointParent == null) { GameObject temp = new GameObject("[WayPoint]"); wayPointParent = temp.transform; wayPointParent.position = Vector3.zero; wayPointParent.rotation = Quaternion.identity; wayPointParent.localScale = Vector3.one; wayPointParent.parent = go.transform; } List wayPointDatas = body.mapWayPoints; WayPointPlanData.LoadWayPointData(wayPointParent.gameObject, wayPointDatas); //加载Trigger信息 Transform triggerParent = go.transform.Find("[Trigger]"); if(triggerParent == null) { GameObject temp = new GameObject("[Trigger]"); triggerParent = temp.transform; triggerParent.position = Vector3.zero; triggerParent.rotation = Quaternion.identity; triggerParent.localScale = Vector3.one; triggerParent.parent = go.transform; } List triggerDatas = body.mapTriggers; TriggerPlanData.LoadTriggerData(triggerParent.gameObject, triggerDatas); //加载动态阻挡 Transform dynamicBlockerParent = go.transform.Find("[DynamicBlocker]"); if (dynamicBlockerParent == null) { GameObject temp = new GameObject("[DynamicBlocker]"); dynamicBlockerParent = temp.transform; dynamicBlockerParent.position = Vector3.zero; dynamicBlockerParent.rotation = Quaternion.identity; dynamicBlockerParent.localScale = Vector3.one; dynamicBlockerParent.parent = go.transform; } List dynamicBlockerDatas = body.mapDynamicBlockers; TriggerPlanData.LoadDynamicBlockerData(dynamicBlockerParent.gameObject, dynamicBlockerDatas); //加载场景音乐 Transform soundParent = go.transform.Find("[Sound]"); if (soundParent == null) { GameObject temp = new GameObject("[Sound]"); soundParent = temp.transform; soundParent.position = Vector3.zero; soundParent.rotation = Quaternion.identity; soundParent.localScale = Vector3.one; soundParent.parent = go.transform; } List soundDatas = body.mapSounds; TriggerPlanData.LoadSoundData(dynamicBlockerParent.gameObject, soundDatas); } else { Debug.LogError(mapName + "=====not exist config file======"); } return go; } //获取int public static int GetInt(byte[] bytes, ref int readLen) { var result = BitConverter.ToInt32(bytes, readLen); readLen += 4; return result; } //获取int16 public static Int16 GetInt16(byte[] bytes, ref int readLen) { var result = BitConverter.ToInt16(bytes, readLen); readLen += 2; return result; } //获fload public static float GetFloat(byte[] bytes, ref int readLen) { var result = BitConverter.ToSingle(bytes, readLen); readLen += 4; return result; } //获取string public static string GetStringFixSize(byte[] bytes, int size, ref int readLen) { var result = System.Text.Encoding.UTF8.GetString(bytes, readLen, size).Trim((char)0); readLen += size; return result; } } }