RouteCtr.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class RouteCtr : MonoBehaviour
  5. {
  6. /// <summary>
  7. /// 路径子物体
  8. /// </summary>
  9. public GameObject RouteItem;
  10. /// <summary>
  11. /// 所有路线
  12. /// </summary>
  13. public List<GameObject> list_route;
  14. [SerializeField]
  15. private CalMap m_CalMap;
  16. private Dictionary<int, List<Vector3>> m_RoutePoints;
  17. private List<Vector3> m_CurrentPoints;
  18. public List<Vector3> CurrentPoints
  19. {
  20. get { return m_CurrentPoints; }
  21. }
  22. private void Start()
  23. {
  24. oninit();
  25. }
  26. public void oninit()
  27. {
  28. list_route = new List<GameObject>();
  29. m_RoutePoints = new Dictionary<int, List<Vector3>>();
  30. m_CurrentPoints = new List<Vector3>();
  31. }
  32. public List<GameObject> List_route
  33. {
  34. get { return list_route; }
  35. }
  36. /// <summary>
  37. /// 设置路线数据
  38. /// </summary>
  39. /// <param name="navLines"></param>
  40. public void SetRouteValue(List<NavLinesItem> navLines)
  41. {
  42. m_RoutePoints.Clear();
  43. for (int i = 0; i < navLines.Count; i++)
  44. {
  45. List<Vector3> points = new List<Vector3>();
  46. SettingLuJing(navLines[i], navLines[i].routerMod - 1, out points);
  47. // 小地图显示路径
  48. m_CalMap.SetLines(navLines[i]);
  49. m_RoutePoints.Add(i, points);
  50. }
  51. }
  52. /// <summary>
  53. /// 设置路径
  54. /// </summary>
  55. /// <param name="data"></param>
  56. /// <param name="routerMod">线段样式</param>
  57. private void SettingLuJing(NavLinesItem data, int routerMod, out List<Vector3> points)
  58. {
  59. points = new List<Vector3>();
  60. GameObject routes = new GameObject(data.name);
  61. routes.transform.parent = transform;
  62. routes.transform.localPosition = Vector3.zero;
  63. routes.transform.localEulerAngles = Vector3.zero;
  64. for (int j = 0; j < data.lines.Count; j++)
  65. {
  66. var line = data.lines[j];
  67. GameObject route = GameObject.Instantiate(RouteItem, routes.transform);
  68. route.name = line._index.ToString();
  69. Vector3 startPos = new Vector3((float)line.x1 / GameManager.Instance.WebMapSize.x * GameManager.Instance.MapSize.x, 0, -(float)line.y1 / GameManager.Instance.WebMapSize.y * GameManager.Instance.MapSize.y);
  70. Vector3 endPos = new Vector3((float)line.x2 / GameManager.Instance.WebMapSize.x * GameManager.Instance.MapSize.x, 0, -(float)line.y2 / GameManager.Instance.WebMapSize.y * GameManager.Instance.MapSize.y);
  71. route.GetComponent<Route>().SetRoute(endPos, startPos, routerMod);
  72. route.SetActive(true);
  73. points.Add(startPos);
  74. if (j == data.lines.Count - 1)
  75. {
  76. points.Add(endPos);
  77. }
  78. }
  79. list_route.Add(routes);
  80. routes.SetActive(false);
  81. }
  82. /// <summary>
  83. /// 选择路线
  84. /// </summary>
  85. /// <param name="Index"></param>
  86. public void SettingLuJing(int Index, bool isshow)
  87. {
  88. if (list_route == null || list_route.Count <= 0)
  89. {
  90. Debug.LogError(" 未初始化路径 或 所选的路径不存在 ");
  91. return;
  92. }
  93. list_route[Index].SetActive(isshow);
  94. m_CurrentPoints = isshow ? m_RoutePoints[Index] : m_RoutePoints[0];
  95. }
  96. }