RouteCtr.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. private 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. list_route = new List<GameObject>();
  25. m_RoutePoints = new Dictionary<int, List<Vector3>>();
  26. m_CurrentPoints = new List<Vector3>();
  27. }
  28. public List<GameObject> List_route
  29. {
  30. get { return list_route; }
  31. }
  32. /// <summary>
  33. /// 设置路线数据
  34. /// </summary>
  35. /// <param name="navLines"></param>
  36. public void SetRouteValue(List<NavLinesItem> navLines)
  37. {
  38. m_RoutePoints.Clear();
  39. for (int i = 0; i < navLines.Count; i++)
  40. {
  41. List<Vector3> points = new List<Vector3>();
  42. SettingLuJing(navLines[i], navLines[i].routerMod - 1, out points);
  43. // 小地图显示路径
  44. m_CalMap.SetLines(navLines[i]);
  45. m_RoutePoints.Add(i, points);
  46. }
  47. }
  48. /// <summary>
  49. /// 设置路径
  50. /// </summary>
  51. /// <param name="data"></param>
  52. /// <param name="routerMod">线段样式</param>
  53. private void SettingLuJing(NavLinesItem data, int routerMod, out List<Vector3> points)
  54. {
  55. points = new List<Vector3>();
  56. GameObject routes = new GameObject(data.name);
  57. routes.transform.parent = transform;
  58. routes.transform.localPosition = Vector3.zero;
  59. routes.transform.localEulerAngles = Vector3.zero;
  60. for (int j = 0; j < data.lines.Count; j++)
  61. {
  62. var line = data.lines[j];
  63. GameObject route = GameObject.Instantiate(RouteItem, routes.transform);
  64. route.name = line._index.ToString();
  65. 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);
  66. 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);
  67. route.GetComponent<Route>().SetRoute(endPos, startPos, routerMod);
  68. route.SetActive(true);
  69. points.Add(startPos);
  70. if (j == data.lines.Count - 1)
  71. {
  72. points.Add(endPos);
  73. }
  74. }
  75. list_route.Add(routes);
  76. routes.SetActive(false);
  77. }
  78. /// <summary>
  79. /// 选择路线
  80. /// </summary>
  81. /// <param name="Index"></param>
  82. public void SettingLuJing(int Index, bool isshow)
  83. {
  84. if (list_route == null || list_route.Count <= 0)
  85. {
  86. Debug.LogError(" 未初始化路径 或 所选的路径不存在 ");
  87. return;
  88. }
  89. list_route[Index].SetActive(isshow);
  90. m_CurrentPoints = isshow ? m_RoutePoints[Index] : m_RoutePoints[0];
  91. }
  92. }