Route.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. private IEnumerator setMaterial()
  39. {
  40. yield return new WaitForSeconds(0.3f);
  41. // meshRenderer.material = material_High;
  42. material.SetTextureScale("_MainTex", new Vector2(1, transform.localScale.z/transform.localScale.x));
  43. }
  44. public void setHighLight( bool isHigh)
  45. {
  46. // material.
  47. if (isHigh)
  48. meshRenderer.material = material_High;
  49. else
  50. meshRenderer.material = material;
  51. }
  52. /// <summary>
  53. /// 设置Route 位置 长度 朝向 旋转
  54. /// </summary>
  55. /// <param name="endPos"></param>
  56. /// <param name="exitPos"></param>
  57. public void SetRoute(Vector3 endPos, Vector3 startPos, int Index)
  58. {
  59. this.GetComponent<MeshRenderer>().material = ListLineMat[Index];
  60. if (material == null)
  61. material = this.GetComponent<MeshRenderer>().material;
  62. this.EndPos = endPos;
  63. this.StartPos = startPos;
  64. // Debug.Log("Route " + endPos + " " + exitPos);
  65. transform.localScale += new Vector3(0, 0, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) / 12 - (transform.localScale.x * 2));
  66. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) * 1.5f));
  67. transform.localPosition = (StartPos + EndPos) / 2.0f;
  68. Point.localPosition = StartPos + new Vector3(0, transform.localPosition.y, 0);
  69. transform.LookAt(Point);
  70. }
  71. /// <summary>
  72. /// 设置Route 位置 长度 朝向 旋转
  73. /// </summary>
  74. /// <param name="endPos"></param>
  75. /// <param name="exitPos"></param>
  76. public void SetRoute(Vector3 endPos, Vector3 startPos, int Index , Transform point)
  77. {
  78. this.GetComponent<MeshRenderer>().material = ListLineMat[Index];
  79. if (material == null)
  80. material = this.GetComponent<MeshRenderer>().material;
  81. this.EndPos = endPos;
  82. this.StartPos = startPos;
  83. // Debug.Log("Route " + endPos + " " + exitPos);
  84. // transform.localScale += new Vector3(0, 0, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) / 12 - (transform.localScale.x * 2));
  85. // 根据世界场景的尺寸进行换算比例
  86. transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, Mathf.Abs(Vector3.Distance(EndPos, StartPos) / 4)- transform.localScale.x * 2);
  87. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) * 1.5f));
  88. transform.position = (StartPos + EndPos) / 2.0f;
  89. //transform.LookAt(point);
  90. StartCoroutine(setMaterial());
  91. }
  92. /// <summary>
  93. /// 3DMapRoute
  94. /// </summary>
  95. /// <param name="endPos"></param>
  96. /// <param name="exitPos"></param>
  97. public void SetMapRoute()
  98. {
  99. if (material == null)
  100. material = this.GetComponent<MeshRenderer>().material;
  101. material.SetTextureScale("_MainTex", new Vector2(1, 2f));
  102. }
  103. public void SetMapRoute(Vector3 endPos, Vector3 exitPos)
  104. {
  105. if (material == null)
  106. material = this.GetComponent<MeshRenderer>().material;
  107. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos)) * 0.7f));
  108. }
  109. }