RecastPathComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Collections.Generic;
  2. using ET;
  3. using UnityEngine;
  4. namespace ET
  5. {
  6. public class RecastPathAwakeSystem: AwakeSystem<RecastPathComponent>
  7. {
  8. public override void Awake(RecastPathComponent self)
  9. {
  10. self.Awake();
  11. }
  12. }
  13. public class RecastPathComponent: Entity
  14. {
  15. /// <summary>
  16. /// 5v5地图的Nav数据路径
  17. /// </summary>
  18. public const string Moba5V5MapNavDataPath = "../Config/RecastNavData/solo_navmesh.bin";
  19. /// <summary>
  20. /// 寻路处理者(可用于拓展多线程,参考A*插件)
  21. /// key为地图id,value为具体处理者
  22. /// </summary>
  23. public Dictionary<int, RecastPathProcessor> m_RecastPathProcessorDic = new Dictionary<int, RecastPathProcessor>();
  24. /// <summary>
  25. /// 初始化寻路引擎
  26. /// </summary>
  27. public void Awake()
  28. {
  29. RecastInterface.Init();
  30. //TODO 先直接在这里强行初始化地图
  31. LoadMapNavData(10001, Moba5V5MapNavDataPath.ToCharArray());
  32. // // 读取体素数据
  33. // VoxelFile = new VoxelFile();
  34. }
  35. /// <summary>
  36. /// 寻路
  37. /// </summary>
  38. public void SearchPath(int mapId, Vector3 from, Vector3 to, List<Vector3> result)
  39. {
  40. GetRecastPathProcessor(mapId).CalculatePath(from, to, result);
  41. }
  42. public RecastPathProcessor GetRecastPathProcessor(int mapId)
  43. {
  44. if (this.m_RecastPathProcessorDic.TryGetValue(mapId, out var recastPathProcessor))
  45. {
  46. return recastPathProcessor;
  47. }
  48. else
  49. {
  50. Log.Error($"未找到地图id为{mapId}的recastPathProcessor");
  51. return null;
  52. }
  53. }
  54. /// <summary>
  55. /// 加载一个Map的数据
  56. /// </summary>
  57. public void LoadMapNavData(int mapId, char[] navDataPath)
  58. {
  59. if (m_RecastPathProcessorDic.ContainsKey(mapId))
  60. {
  61. Log.Warning($"已存在Id为{mapId}的地图Nav数据,请勿重复加载!");
  62. return;
  63. }
  64. if (RecastInterface.LoadMap(mapId, navDataPath))
  65. {
  66. RecastPathProcessor recastPathProcessor = this.AddChild<RecastPathProcessor>();
  67. recastPathProcessor.MapId = mapId;
  68. m_RecastPathProcessorDic[mapId] = recastPathProcessor;
  69. Log.Debug($"加载Id为{mapId}的地图Nav数据成功!");
  70. }
  71. }
  72. /// <summary>
  73. /// 卸载地图数据
  74. /// </summary>
  75. /// <param name="mapId">地图Id</param>
  76. public void UnLoadMapNavData(int mapId)
  77. {
  78. if (!m_RecastPathProcessorDic.ContainsKey(mapId))
  79. {
  80. Log.Warning($"不存在Id为{mapId}的地图Nav数据,无法进行卸载!");
  81. return;
  82. }
  83. m_RecastPathProcessorDic[mapId].Dispose();
  84. m_RecastPathProcessorDic.Remove(mapId);
  85. if (RecastInterface.FreeMap(mapId))
  86. {
  87. Log.Debug($"地图: {mapId} 释放成功");
  88. }
  89. else
  90. {
  91. Log.Debug($"地图: {mapId} 释放失败");
  92. }
  93. }
  94. public override void Dispose()
  95. {
  96. if (this.IsDisposed)
  97. {
  98. return;
  99. }
  100. base.Dispose();
  101. RecastInterface.Fini();
  102. }
  103. }
  104. }