RouteCtr.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. public Dictionary<string, List<Vector3>> lineoldlist = new Dictionary<string, List<Vector3>>();
  53. public Material[] mats;
  54. /// <summary>
  55. /// 设置路径
  56. /// </summary>
  57. /// <param name="data"></param>
  58. /// <param name="routerMod">线段样式</param>
  59. private void SettingLuJing(NavLinesItem data, int routerMod, out List<Vector3> points)
  60. {
  61. points = new List<Vector3>();
  62. GameObject routes = new GameObject(data.name);
  63. routes.transform.parent = transform;
  64. routes.transform.localPosition = Vector3.zero;
  65. routes.transform.localEulerAngles = Vector3.zero;
  66. LineRenderer linerender = routes.AddComponent<LineRenderer>();
  67. routes.AddComponent<LineEffect>();
  68. linerender.positionCount = data.lines.Count*2;
  69. linerender.startWidth = 0.05f;// (,0.35f);
  70. linerender.endWidth = 0.05f;// (,0.35f);
  71. linerender.material = mats[routerMod];
  72. if(!lineoldlist.ContainsKey(data.name))
  73. {
  74. lineoldlist.Add(data.name, new List<Vector3>());
  75. }
  76. else
  77. {
  78. lineoldlist[data.name] =new List<Vector3>();
  79. }
  80. for (int j = 0; j < data.lines.Count; j++)
  81. {
  82. var line = data.lines[j];
  83. /*
  84. GameObject route = GameObject.Instantiate(RouteItem, routes.transform);
  85. route.name = line._index.ToString();*/
  86. Vector3 startPos = new Vector3((float)line.x1 / GameManager.Instance.WebMapSize.x * GameManager.Instance.MapSize.x, 0, (GameManager.Instance.WebMapSize.y - (float)line.y1) / GameManager.Instance.WebMapSize.y * GameManager.Instance.MapSize.y);
  87. lineoldlist[data.name].Add(startPos);
  88. linerender.SetPosition(j*2, transform.TransformPoint(startPos));
  89. Vector3 endPos = new Vector3((float)line.x2 / GameManager.Instance.WebMapSize.x * GameManager.Instance.MapSize.x, 0, (GameManager.Instance.WebMapSize.y - (float)line.y2) / GameManager.Instance.WebMapSize.y * GameManager.Instance.MapSize.y);
  90. /* route.GetComponent<Route>().SetRoute(endPos, startPos, routerMod);
  91. route.SetActive(true);
  92. points.Add(startPos);
  93. if (j == data.lines.Count - 1)
  94. {
  95. points.Add(endPos);
  96. }*/
  97. lineoldlist[data.name].Add(endPos);
  98. linerender.SetPosition(j*2+1, transform.TransformPoint(endPos));
  99. }
  100. list_route.Add(routes);
  101. //routes.SetActive(false);
  102. }
  103. float MaxL;
  104. private void Update()
  105. {
  106. for (int i = 0; i < list_route.Count; i++)
  107. {
  108. if(lineoldlist[list_route[i].name]!=null)
  109. {
  110. LineRenderer lr = list_route[i].GetComponent<LineRenderer>();
  111. /*
  112. if(lineoldlist[list_route[i].name].Count>=3)
  113. {
  114. List<Vector3> v3 = new List<Vector3>();
  115. for (int j = 2; j < lineoldlist[list_route[i].name].Count; j++)
  116. {
  117. v3.Add(lineoldlist[list_route[i].name][j - 2]);
  118. v3.AddRange( DrawCurve(GetPos(lineoldlist[list_route[i].name][j - 1], lineoldlist[list_route[i].name][j - 2]), lineoldlist[list_route[i].name][j - 1], GetPos(lineoldlist[list_route[i].name][j - 1], lineoldlist[list_route[i].name][i])));
  119. v3.Add(lineoldlist[list_route[i].name][j]);
  120. }
  121. lr.positionCount = v3.Count;
  122. for (int z = 0; z < v3.Count; z++)
  123. {
  124. lr.SetPosition(z, transform.TransformPoint(v3[z]));
  125. }
  126. MaxL = 0;
  127. for (int m = 1; m < v3.Count; m++)
  128. {
  129. MaxL += Vector3.Distance(v3[m - 1], v3[m]);
  130. }
  131. lr.materials[0].SetTextureScale("_MainTex", new Vector2(MaxL * 10 , 1));
  132. }
  133. else
  134. {*/
  135. for (int j = 0; j < lineoldlist[list_route[i].name].Count; j++)
  136. {
  137. lr.SetPosition(j, transform.TransformPoint(lineoldlist[list_route[i].name][j]));
  138. }
  139. MaxL = 0;
  140. for (int m = 1; m < lineoldlist[list_route[i].name].Count; m++)
  141. {
  142. MaxL += Vector3.Distance(lineoldlist[list_route[i].name][m - 1], lineoldlist[list_route[i].name][m]);
  143. }
  144. lr.materials[0].SetTextureScale("_MainTex", new Vector2(MaxL * 10, 1));
  145. //}
  146. }
  147. }
  148. }
  149. public Vector3 GetPos(Vector3 post1, Vector3 post2)
  150. {
  151. Vector3 pos = Vector3.zero;
  152. float dis = Vector3.Distance(post1, post2);//计算距离
  153. Vector3 vector = (post2 - post1).normalized; //向量单位化
  154. float rand = 0.2f;//随机距离
  155. pos = vector * rand; //得到新坐标
  156. pos += post1; //使新的坐标点在 post1与 post2之间
  157. return pos;
  158. }
  159. private int _segmentNum = 3;
  160. List<Vector3> DrawCurve(Vector3 pos1, Vector3 pos2, Vector3 pos3)
  161. {
  162. List<Vector3> listPos = new List<Vector3>();
  163. for (int i = 1; i <= _segmentNum; i++)
  164. {
  165. float t = i / (float)_segmentNum;
  166. int nodeIndex = 0;
  167. Vector3 pixel = CalculateCubicBezierPoint(t, pos1,
  168. pos2, pos3);
  169. listPos.Add(pixel);
  170. }
  171. return listPos;
  172. }
  173. Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
  174. {
  175. float u = 1 - t;
  176. float tt = t * t;
  177. float uu = u * u;
  178. Vector3 p = uu * p0;
  179. p += 2 * u * t * p1;
  180. p += tt * p2;
  181. return p;
  182. }
  183. /// <summary>
  184. /// 选择路线
  185. /// </summary>
  186. /// <param name="Index"></param>
  187. public void SettingLuJing(int Index, bool isshow)
  188. {
  189. if (list_route == null || list_route.Count <= 0)
  190. {
  191. Debug.LogError(" 未初始化路径 或 所选的路径不存在 ");
  192. return;
  193. }
  194. list_route[Index].SetActive(isshow);
  195. m_CurrentPoints = isshow ? m_RoutePoints[Index] : m_RoutePoints[0];
  196. }
  197. }