/// <summary> /// 通过读取Map文件获得阻挡信息 /// </summary> private static void ReadInfoFromFile() { MapObjDataManager manager = GameObject.FindObjectOfType(typeof(MapObjDataManager)) as MapObjDataManager; string filePath = Application.dataPath + "/Resources/Map/" + manager.MapName.ToString() + ".map"; FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); int MapId = br.ReadInt32(); int MapWidth = br.ReadInt32(); int MapHeight = br.ReadInt32(); int MapLength = MapWidth * MapHeight; GameObject cubeParent = new GameObject("CubeParent"); cubeParent.transform.position = Vector3.zero; //MapLength = 20000; //GameObject obj_parent = UnityEngine.Object.Instantiate(Resources.Load("Cube/Cubebb"), Vector3.zero, Quaternion.identity) as GameObject; for (int index = 0; index < MapLength; index++) { byte info = br.ReadByte(); if (info == 1) { InstanceCubeByMapIndex(index, cubeParent, manager); } } fs.Close(); br.Close(); }
//#region mapHeader private static void GetMapDataHeader(MapObjDataManager manager) { SceneMapData.SMapDataHeader head = new SceneMapData.SMapDataHeader(); head.un32MapID = (UInt32)(manager.MapName); head.mapWidth = (UInt32)(manager.MapWidth); head.mapHeight = (UInt32)(manager.MapHeight); SceneMapData.Instance.mapDataStruct.sHeader = head; Debug.Log("MapData.Instance.headData.mapWidth " + SceneMapData.Instance.mapDataStruct.sHeader.mapWidth + " xx " + SceneMapData.Instance.mapDataStruct.sHeader.mapHeight); }
// // #endregion // #region map block private static void GetMapBlockData(MapObjDataManager manager) { int width = manager.MapWidth; int height = manager.MapHeight; byte [] abBlockData; //SceneMapData.Instance.MapData.abBlockData = new byte[width * height]; CollsionDetection.SetCollisionBuf(width, height, SceneMapData.layerName, out abBlockData); SceneMapData.Instance.mapDataStruct.BlockData = abBlockData; }
/// <summary> /// 创建阻挡格子 /// </summary> /// <param name="index"></param> /// <param name="obj_parent"></param> private static void InstanceCubeByMapIndex(int index, GameObject obj_parent, MapObjDataManager manager) { int row = (int)(index / manager.MapWidth); float posZ = row + 0.5f; float posX = (index - row * manager.MapWidth) + 0.5f; GameObject tobj = GameObject.CreatePrimitive(PrimitiveType.Cube); tobj.transform.position = new Vector3(posX, 100.0f, posZ); tobj.transform.parent = obj_parent.transform; }
// static byte[]headerBytes; // static byte[]blockBytes; // static byte[]objectrBytes; private static void ImportInfoToFile() { MapObjDataManager manager = GameObject.FindObjectOfType(typeof(MapObjDataManager)) as MapObjDataManager; GetMapDataHeader(manager); GetMapBlockData(manager); string filePath = Application.dataPath + "/Resources/Map/" + manager.MapName.ToString() + ".map"; CMemBuffer buf = Export(); int un32DataSize = buf.GetWritePos(); FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs); bw.Write(buf.GetMemBuffer(), 0, un32DataSize); Debug.LogError("write = " + un32DataSize); fs.Close(); bw.Close(); }