Route.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.05f);
  7. public List<Material> ListLineMat;
  8. /// <summary>
  9. /// 旋转方向
  10. /// </summary>
  11. public Transform Point;
  12. public Vector3 EndPos;
  13. public Vector3 StartPos;
  14. public Material material_High;
  15. private Material material;
  16. private MeshRenderer meshRenderer;
  17. private Vector2 offset;
  18. void Start()
  19. {
  20. meshRenderer = this.GetComponent<MeshRenderer>();
  21. material = meshRenderer.material;
  22. offset = Vector2.zero;
  23. StartCoroutine(SetOffset());
  24. }
  25. void Update()
  26. {
  27. }
  28. private IEnumerator SetOffset()
  29. {
  30. while(true)
  31. {
  32. yield return new WaitForSeconds(0.04f);
  33. offset -= Interval;
  34. material.SetTextureOffset("_MainTex", offset);
  35. material_High.SetTextureOffset("_MainTex", offset);
  36. }
  37. }
  38. public void setHighLight( bool isHigh)
  39. {
  40. // material.
  41. if (isHigh)
  42. meshRenderer.material = material_High;
  43. else
  44. meshRenderer.material = material;
  45. }
  46. /// <summary>
  47. /// 设置Route 位置 长度 朝向 旋转
  48. /// </summary>
  49. /// <param name="endPos"></param>
  50. /// <param name="exitPos"></param>
  51. public void SetRoute(Vector3 endPos, Vector3 startPos, int Index)
  52. {
  53. this.GetComponent<MeshRenderer>().material = ListLineMat[Index];
  54. if (material == null)
  55. material = this.GetComponent<MeshRenderer>().material;
  56. this.EndPos = endPos;
  57. this.StartPos = startPos;
  58. // Debug.Log("Route " + endPos + " " + exitPos);
  59. transform.localScale += new Vector3(0, 0, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) / 12 - (transform.localScale.x * 2));
  60. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) * 1.5f));
  61. transform.localPosition = (StartPos + EndPos) / 2.0f;
  62. Point.localPosition = StartPos + new Vector3(0, transform.localPosition.y, 0);
  63. transform.LookAt(Point);
  64. }
  65. /// <summary>
  66. /// 设置Route 位置 长度 朝向 旋转
  67. /// </summary>
  68. /// <param name="endPos"></param>
  69. /// <param name="exitPos"></param>
  70. public void SetRoute(Vector3 endPos, Vector3 startPos, int Index , Transform point)
  71. {
  72. this.GetComponent<MeshRenderer>().material = ListLineMat[Index];
  73. if (material == null)
  74. material = this.GetComponent<MeshRenderer>().material;
  75. this.EndPos = endPos;
  76. this.StartPos = startPos;
  77. // Debug.Log("Route " + endPos + " " + exitPos);
  78. transform.localScale += new Vector3(0, 0, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) / 12 - (transform.localScale.x * 2));
  79. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) * 1.5f));
  80. transform.position = (StartPos + EndPos) / 2.0f;
  81. //transform.LookAt(point);
  82. }
  83. /// <summary>
  84. /// 3DMapRoute
  85. /// </summary>
  86. /// <param name="endPos"></param>
  87. /// <param name="exitPos"></param>
  88. public void SetMapRoute()
  89. {
  90. if (material == null)
  91. material = this.GetComponent<MeshRenderer>().material;
  92. material.SetTextureScale("_MainTex", new Vector2(1, 2f));
  93. }
  94. public void SetMapRoute(Vector3 endPos, Vector3 exitPos)
  95. {
  96. if (material == null)
  97. material = this.GetComponent<MeshRenderer>().material;
  98. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos)) * 0.7f));
  99. }
  100. }