using System.Collections; using System.Collections.Generic; using UnityEngine; public class RouteCtr : MonoBehaviour { /// /// 路径子物体 /// public GameObject RouteItem; /// /// 所有路线 /// public List list_route; [SerializeField] private CalMap m_CalMap; private Dictionary> m_RoutePoints; private List m_CurrentPoints; public List CurrentPoints { get { return m_CurrentPoints; } } private void Start() { oninit(); } public void oninit() { list_route = new List(); m_RoutePoints = new Dictionary>(); m_CurrentPoints = new List(); } public List List_route { get { return list_route; } } /// /// 设置路线数据 /// /// public void SetRouteValue(List navLines) { m_RoutePoints.Clear(); for (int i = 0; i < navLines.Count; i++) { List points = new List(); SettingLuJing(navLines[i], navLines[i].routerMod - 1, out points); // 小地图显示路径 m_CalMap.SetLines(navLines[i]); m_RoutePoints.Add(i, points); } } public Dictionary> lineoldlist = new Dictionary>(); public Material[] mats; /// /// 设置路径 /// /// /// 线段样式 private void SettingLuJing(NavLinesItem data, int routerMod, out List points) { points = new List(); GameObject routes = new GameObject(data.name); routes.transform.parent = transform; routes.transform.localPosition = Vector3.zero; routes.transform.localEulerAngles = Vector3.zero; LineRenderer linerender = routes.AddComponent(); routes.AddComponent(); linerender.positionCount = data.lines.Count*2; linerender.startWidth = 0.05f;// (,0.35f); linerender.endWidth = 0.05f;// (,0.35f); linerender.material = mats[routerMod]; if(!lineoldlist.ContainsKey(data.name)) { lineoldlist.Add(data.name, new List()); } else { lineoldlist[data.name] =new List(); } for (int j = 0; j < data.lines.Count; j++) { var line = data.lines[j]; /* GameObject route = GameObject.Instantiate(RouteItem, routes.transform); route.name = line._index.ToString();*/ 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); lineoldlist[data.name].Add(startPos); linerender.SetPosition(j*2, transform.TransformPoint(startPos)); 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); /* route.GetComponent().SetRoute(endPos, startPos, routerMod); route.SetActive(true); points.Add(startPos); if (j == data.lines.Count - 1) { points.Add(endPos); }*/ lineoldlist[data.name].Add(endPos); linerender.SetPosition(j*2+1, transform.TransformPoint(endPos)); } list_route.Add(routes); //routes.SetActive(false); } float MaxL; private void Update() { for (int i = 0; i < list_route.Count; i++) { if(lineoldlist[list_route[i].name]!=null) { LineRenderer lr = list_route[i].GetComponent(); /* if(lineoldlist[list_route[i].name].Count>=3) { List v3 = new List(); for (int j = 2; j < lineoldlist[list_route[i].name].Count; j++) { v3.Add(lineoldlist[list_route[i].name][j - 2]); 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]))); v3.Add(lineoldlist[list_route[i].name][j]); } lr.positionCount = v3.Count; for (int z = 0; z < v3.Count; z++) { lr.SetPosition(z, transform.TransformPoint(v3[z])); } MaxL = 0; for (int m = 1; m < v3.Count; m++) { MaxL += Vector3.Distance(v3[m - 1], v3[m]); } lr.materials[0].SetTextureScale("_MainTex", new Vector2(MaxL * 10 , 1)); } else {*/ for (int j = 0; j < lineoldlist[list_route[i].name].Count; j++) { lr.SetPosition(j, transform.TransformPoint(lineoldlist[list_route[i].name][j])); } MaxL = 0; for (int m = 1; m < lineoldlist[list_route[i].name].Count; m++) { MaxL += Vector3.Distance(lineoldlist[list_route[i].name][m - 1], lineoldlist[list_route[i].name][m]); } lr.materials[0].SetTextureScale("_MainTex", new Vector2(MaxL * 10, 1)); //} } } } public Vector3 GetPos(Vector3 post1, Vector3 post2) { Vector3 pos = Vector3.zero; float dis = Vector3.Distance(post1, post2);//计算距离 Vector3 vector = (post2 - post1).normalized; //向量单位化 float rand = 0.2f;//随机距离 pos = vector * rand; //得到新坐标 pos += post1; //使新的坐标点在 post1与 post2之间 return pos; } private int _segmentNum = 3; List DrawCurve(Vector3 pos1, Vector3 pos2, Vector3 pos3) { List listPos = new List(); for (int i = 1; i <= _segmentNum; i++) { float t = i / (float)_segmentNum; int nodeIndex = 0; Vector3 pixel = CalculateCubicBezierPoint(t, pos1, pos2, pos3); listPos.Add(pixel); } return listPos; } Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2) { float u = 1 - t; float tt = t * t; float uu = u * u; Vector3 p = uu * p0; p += 2 * u * t * p1; p += tt * p2; return p; } /// /// 选择路线 /// /// public void SettingLuJing(int Index, bool isshow) { if (list_route == null || list_route.Count <= 0) { Debug.LogError(" 未初始化路径 或 所选的路径不存在 "); return; } list_route[Index].SetActive(isshow); m_CurrentPoints = isshow ? m_RoutePoints[Index] : m_RoutePoints[0]; } }