TestRoute.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class TestRoute : MonoBehaviour
  5. {
  6. public Vector2 Interval = new Vector2(0, -0.02f);
  7. /// <summary>
  8. /// 旋转方向
  9. /// </summary>
  10. public Transform point;
  11. public List<Material> list_material;
  12. public Vector3 endPos;
  13. public Vector3 exitPos;
  14. public Transform A;
  15. public Transform B;
  16. private Material material;
  17. private Vector2 offset;
  18. void Start()
  19. {
  20. material = this.GetComponent<MeshRenderer>().material;
  21. offset = Vector2.zero;
  22. // Test();
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. offset -= Interval;
  28. material.SetTextureOffset("_MainTex", offset);
  29. }
  30. /// <summary>
  31. /// 设置Route 位置 长度 朝向 旋转
  32. /// </summary>
  33. /// <param name="endPos"></param>
  34. /// <param name="exitPos"></param>
  35. public void SetRoute( Vector3 endPos, Vector3 exitPos )
  36. {
  37. if (material == null)
  38. material = this.GetComponent<MeshRenderer>().material;
  39. this.endPos = endPos;
  40. this.exitPos = exitPos;
  41. transform.localScale += new Vector3(0, 0, Mathf.Abs( Vector3.Distance(endPos,exitPos) )/12f - (transform.localScale.x * 2));
  42. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos))*1.5f));
  43. Vector3 pos = endPos + (exitPos - endPos).normalized * (Vector3.Distance(endPos, exitPos));
  44. transform.position = (exitPos+endPos)/2.0f + new Vector3(0, -2f, 0);
  45. Debug.Log("Route " + endPos + " " + exitPos +" "+ transform.position);
  46. point.position = endPos + new Vector3(0, transform.position.y, 0);
  47. transform.LookAt(point);
  48. }
  49. public void SetRoute(Vector3 endPos, Vector3 exitPos ,int Index)
  50. {
  51. this.GetComponent<MeshRenderer>().material = list_material[Index];
  52. if (material == null)
  53. material = this.GetComponent<MeshRenderer>().material;
  54. this.endPos = endPos;
  55. this.exitPos = exitPos;
  56. // Debug.Log("Route " + endPos + " " + exitPos);
  57. transform.localScale += new Vector3(0, 0, Mathf.Abs(Vector3.Distance(endPos, exitPos))/12 -(transform.localScale.x*2));
  58. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos)) * 1.5f));
  59. Vector3 pos = endPos + (exitPos - endPos).normalized * (Vector3.Distance(endPos, exitPos));
  60. transform.position = (exitPos + endPos) / 2.0f + new Vector3(0, -2f, 0);
  61. point.position = endPos + new Vector3(0, transform.position.y, 0);
  62. transform.LookAt(point);
  63. }
  64. /// <summary>
  65. /// 3DMapRoute
  66. /// </summary>
  67. /// <param name="endPos"></param>
  68. /// <param name="exitPos"></param>
  69. public void SetMapRoute()
  70. {
  71. if (material == null)
  72. material = this.GetComponent<MeshRenderer>().material;
  73. material.SetTextureScale("_MainTex", new Vector2(1, 2f));
  74. }
  75. public void SetMapRoute( Vector3 endPos , Vector3 exitPos)
  76. {
  77. if (material == null)
  78. material = this.GetComponent<MeshRenderer>().material;
  79. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos)) * 0.7f));
  80. }
  81. public void Test()
  82. {
  83. SetRoute(A.position, B.position);
  84. }
  85. }