123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class RouteCtr : MonoBehaviour
- {
- /// <summary>
- /// 路径子物体
- /// </summary>
- public GameObject RouteItem;
- /// <summary>
- /// 所有路线
- /// </summary>
- public List<GameObject> list_route;
- [SerializeField]
- private CalMap m_CalMap;
- private Dictionary<int, List<Vector3>> m_RoutePoints;
- private List<Vector3> m_CurrentPoints;
- public List<Vector3> CurrentPoints
- {
- get { return m_CurrentPoints; }
- }
- private void Start()
- {
- oninit();
- }
- public void oninit()
- {
- list_route = new List<GameObject>();
- m_RoutePoints = new Dictionary<int, List<Vector3>>();
- m_CurrentPoints = new List<Vector3>();
- }
- public List<GameObject> List_route
- {
- get { return list_route; }
- }
- /// <summary>
- /// 设置路线数据
- /// </summary>
- /// <param name="navLines"></param>
- public void SetRouteValue(List<NavLinesItem> navLines)
- {
- m_RoutePoints.Clear();
- for (int i = 0; i < navLines.Count; i++)
- {
- List<Vector3> points = new List<Vector3>();
- SettingLuJing(navLines[i], navLines[i].routerMod - 1, out points);
- // 小地图显示路径
- m_CalMap.SetLines(navLines[i]);
- m_RoutePoints.Add(i, points);
- }
- }
- public Dictionary<string, List<Vector3>> lineoldlist = new Dictionary<string, List<Vector3>>();
- public Material[] mats;
- /// <summary>
- /// 设置路径
- /// </summary>
- /// <param name="data"></param>
- /// <param name="routerMod">线段样式</param>
- private void SettingLuJing(NavLinesItem data, int routerMod, out List<Vector3> points)
- {
- points = new List<Vector3>();
- GameObject routes = new GameObject(data.name);
- routes.transform.parent = transform;
- routes.transform.localPosition = Vector3.zero;
- routes.transform.localEulerAngles = Vector3.zero;
- LineRenderer linerender = routes.AddComponent<LineRenderer>();
- routes.AddComponent<LineEffect>();
- 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<Vector3>());
- }
- else
- {
- lineoldlist[data.name] =new List<Vector3>();
- }
- 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<Route>().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<LineRenderer>();
- /*
- if(lineoldlist[list_route[i].name].Count>=3)
- {
- List<Vector3> v3 = new List<Vector3>();
- 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<Vector3> DrawCurve(Vector3 pos1, Vector3 pos2, Vector3 pos3)
- {
- List<Vector3> listPos = new List<Vector3>();
- 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;
- }
- /// <summary>
- /// 选择路线
- /// </summary>
- /// <param name="Index"></param>
- 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];
- }
- }
|