Scene_Map.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using CrossgateToolkit;
  4. using UnityEngine;
  5. using UnityEngine.Tilemaps;
  6. public class Scene_Map : MonoBehaviour
  7. {
  8. [SerializeField,Header("地图渲染用Camera")]
  9. private Camera MapCamera;
  10. [SerializeField,Header("地图地面TileMap")]
  11. private Tilemap TileMap_Ground;
  12. [SerializeField,Header("地图物件TileMap")]
  13. private Tilemap TileMap_Object;
  14. [SerializeField,Header("地图Grid")]
  15. private Grid MapGrid;
  16. [SerializeField, Header("地图背景音乐")]
  17. private AudioSource MapBGM;
  18. [SerializeField,Header("小地图TileMap")]
  19. private Tilemap TileMap_MiniMap;
  20. [SerializeField,Header("小地图TileSprite")]
  21. private Sprite MiniMapSprite;
  22. // 地图数据
  23. private MapInfo mapInfo;
  24. // 地图音乐
  25. private uint mapBGMID;
  26. // 地图图像数据
  27. private Dictionary<uint,GraphicDetail> GroundGraphicDetails;
  28. private Dictionary<uint,GraphicDetail> ObjectGraphicDetails;
  29. // Start is called before the first frame update
  30. void Start()
  31. {
  32. // 地图渲染相机需要注意调整排序方式
  33. MapCamera.transparencySortMode = TransparencySortMode.CustomAxis;
  34. MapCamera.transparencySortAxis = new Vector3(0, 1, -0.1f);
  35. LoadMap();
  36. }
  37. private void Awake()
  38. {
  39. Util.Init();
  40. }
  41. private void LoadMap()
  42. {
  43. // 获取地图信息,此处以编号为1000的法兰城地图为例
  44. mapInfo = Map.GetMap(1000);
  45. // 地图的数据已经过重新排序处理,无论加载的是服务端地图或是客户端地图,均按照场景中所示排序规则进行处理
  46. // 图集合批处理
  47. BakeMapGraphics();
  48. // 绘制地图,此处直接绘制全图
  49. // 实际使用中推荐以卡马克卷轴算法进行局部绘制,以提高性能
  50. DrawMapGround();
  51. DrawMapObject();
  52. // 调整位置
  53. int x = mapInfo.Width / 2 * 32 - mapInfo.Height / 2 * 32;
  54. int y = -mapInfo.Height / 2 * 24 - mapInfo.Width / 2 * 24;
  55. MapGrid.GetComponent<RectTransform>().localPosition = new Vector3(x, y, 0);
  56. // 绘制小地图
  57. DrawMiniMap();
  58. }
  59. // 对地图图像进行合批处理
  60. private void BakeMapGraphics()
  61. {
  62. // 以合批图集的形式缓存地图图像数据
  63. // 此处仅以一次性合批图集方式进行示范
  64. // 如若针对服务端动态地图方式,则需要动态合批图集,可参考Scene_Graphic.cs中的GetDynamicBatchGraphic()方法进行处理
  65. List<GraphicInfoData> graphicInfoDatas;
  66. // 获取地面数据并进行合批
  67. graphicInfoDatas = new List<GraphicInfoData>();
  68. for (var i = 0; i < mapInfo.GroundDatas.Count; i++)
  69. {
  70. MapBlockData mapBlockData = mapInfo.GroundDatas[i];
  71. if(mapBlockData==null || mapBlockData.GraphicInfo==null) continue;
  72. graphicInfoDatas.Add(mapBlockData.GraphicInfo);
  73. }
  74. GroundGraphicDetails = GraphicData.BakeGraphics(graphicInfoDatas, true, 0, -1, false, 2048);
  75. // 获取物件数据并进行合批
  76. graphicInfoDatas = new List<GraphicInfoData>();
  77. for (var i = 0; i < mapInfo.ObjectDatas.Count; i++)
  78. {
  79. MapBlockData mapBlockData = mapInfo.ObjectDatas[i];
  80. if(mapBlockData==null || mapBlockData.GraphicInfo==null) continue;
  81. graphicInfoDatas.Add(mapBlockData.GraphicInfo);
  82. }
  83. ObjectGraphicDetails = GraphicData.BakeGraphics(graphicInfoDatas, true, 0, -1, false, 2048);
  84. }
  85. // 绘制地图地面
  86. private void DrawMapGround()
  87. {
  88. int width = mapInfo.Width;
  89. int height = mapInfo.Height;
  90. List<Vector3Int> drawPositions = new List<Vector3Int>();
  91. List<Tile> drawTiles = new List<Tile>();
  92. for(int x = 0; x < width; x++)
  93. {
  94. for(int y = 0; y < height; y++)
  95. {
  96. MapBlockData mapBlockData = mapInfo.GroundDatas[y * width + x];
  97. if(mapBlockData==null || mapBlockData.GraphicInfo==null) continue;
  98. GraphicDetail graphicDetail = GroundGraphicDetails[mapBlockData.MapSerial];
  99. Tile groundTile = Tile.CreateInstance(typeof(Tile)) as Tile;
  100. groundTile.sprite = graphicDetail.Sprite;
  101. drawPositions.Add(new Vector3Int(x, y, 0));
  102. drawTiles.Add(groundTile);
  103. }
  104. }
  105. TileMap_Ground.SetTiles(drawPositions.ToArray(), drawTiles.ToArray());
  106. }
  107. // 绘制地图物件
  108. private void DrawMapObject()
  109. {
  110. int width = mapInfo.Width;
  111. int height = mapInfo.Height;
  112. List<Vector3Int> drawPositions = new List<Vector3Int>();
  113. List<Tile> drawTiles = new List<Tile>();
  114. for(int x = 0; x < width; x++)
  115. {
  116. for(int y = 0; y < height; y++)
  117. {
  118. MapBlockData mapBlockData = mapInfo.ObjectDatas[y * width + x];
  119. if(mapBlockData==null || mapBlockData.GraphicInfo==null) continue;
  120. if (mapBlockData.GraphicInfo.Serial < 1000)
  121. {
  122. // 小于1000的编号为音频编号
  123. if (mapBGMID == 0)
  124. {
  125. mapBGMID = mapBlockData.MapSerial;
  126. // 播放背景音乐
  127. Audio.Play(MapBGM, Audio.Type.BGM, (int)mapBGMID);
  128. }
  129. }
  130. GraphicDetail graphicDetail = ObjectGraphicDetails[mapBlockData.MapSerial];
  131. Tile objectTile = Tile.CreateInstance(typeof(Tile)) as Tile;
  132. objectTile.sprite = graphicDetail.Sprite;
  133. // 注意排序的修正,mapBlockData提供了基础的Z轴修正,如需更精确的效果请自行计算处理
  134. drawPositions.Add(new Vector3Int(x, y, 0));
  135. drawTiles.Add(objectTile);
  136. }
  137. }
  138. TileMap_Object.SetTiles(drawPositions.ToArray(), drawTiles.ToArray());
  139. }
  140. // 绘制小地图
  141. private void DrawMiniMap()
  142. {
  143. // CGTool提供了简单的处理方式
  144. // 可以直接通过获取图档数据的主色调来处理小地图
  145. List<Vector3Int> drawPositions = new List<Vector3Int>();
  146. List<Tile> drawTiles = new List<Tile>();
  147. for(int x = 0; x < mapInfo.Width; x++)
  148. {
  149. for(int y = 0; y < mapInfo.Height; y++)
  150. {
  151. MapBlockData mapBlockData = mapInfo.GroundDatas[y * mapInfo.Width + x];
  152. MapBlockData mapObjectData = mapInfo.ObjectDatas[y * mapInfo.Width + x];
  153. if(mapBlockData==null && mapObjectData==null) continue;
  154. Tile tile = Tile.CreateInstance(typeof(Tile)) as Tile;
  155. tile.sprite = MiniMapSprite;
  156. if (mapObjectData == null)
  157. {
  158. // 没有地面物件时使用地面主色
  159. tile.color = GroundGraphicDetails[mapBlockData.MapSerial].PrimaryColor;
  160. }
  161. else
  162. {
  163. // 有地面物件时使用物件主色,此处只做演示,正常情况下需要根据建筑物的占地尺寸对相应区块进行填充以提高小地图的视觉效果
  164. tile.color = ObjectGraphicDetails[mapObjectData.MapSerial].PrimaryColor;
  165. }
  166. drawPositions.Add(new Vector3Int(x, y, 0));
  167. drawTiles.Add(tile);
  168. }
  169. }
  170. TileMap_MiniMap.SetTiles(drawPositions.ToArray(), drawTiles.ToArray());
  171. }
  172. }