Route.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Route : MonoBehaviour
  5. {
  6. private Vector2 Interval = new Vector2(0, 0.02f);
  7. public List<Material> ListLineMat;
  8. /// <summary>
  9. /// 旋转方向
  10. /// </summary>
  11. public Transform Point;
  12. public Vector3 EndPos;
  13. public Vector3 StartPos;
  14. private Material material;
  15. private Vector2 offset;
  16. void Start()
  17. {
  18. material = this.GetComponent<MeshRenderer>().material;
  19. offset = Vector2.zero;
  20. }
  21. void Update()
  22. {
  23. offset -= Interval;
  24. material.SetTextureOffset("_MainTex", offset);
  25. }
  26. /// <summary>
  27. /// 设置Route 位置 长度 朝向 旋转
  28. /// </summary>
  29. /// <param name="endPos"></param>
  30. /// <param name="exitPos"></param>
  31. public void SetRoute(Vector3 endPos, Vector3 startPos, int Index)
  32. {
  33. this.GetComponent<MeshRenderer>().material = ListLineMat[Index];
  34. if (material == null)
  35. material = this.GetComponent<MeshRenderer>().material;
  36. this.EndPos = endPos;
  37. this.StartPos = startPos;
  38. // Debug.Log("Route " + endPos + " " + exitPos);
  39. transform.localScale += new Vector3(0, 0, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) / 12 - (transform.localScale.x * 2));
  40. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) * 1.5f));
  41. transform.localPosition = (StartPos + EndPos) / 2.0f;
  42. Point.localPosition = StartPos + new Vector3(0, transform.localPosition.y, 0);
  43. transform.LookAt(Point);
  44. }
  45. /// <summary>
  46. /// 3DMapRoute
  47. /// </summary>
  48. /// <param name="endPos"></param>
  49. /// <param name="exitPos"></param>
  50. public void SetMapRoute()
  51. {
  52. if (material == null)
  53. material = this.GetComponent<MeshRenderer>().material;
  54. material.SetTextureScale("_MainTex", new Vector2(1, 2f));
  55. }
  56. public void SetMapRoute(Vector3 endPos, Vector3 exitPos)
  57. {
  58. if (material == null)
  59. material = this.GetComponent<MeshRenderer>().material;
  60. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos)) * 0.7f));
  61. }
  62. }