TestRoute.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. private Material material;
  15. private Vector2 offset;
  16. void Start()
  17. {
  18. material = this.GetComponent<MeshRenderer>().material;
  19. offset = Vector2.zero;
  20. // Test();
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. offset -= Interval;
  26. material.SetTextureOffset("_MainTex", offset);
  27. }
  28. /// <summary>
  29. /// 设置Route 位置 长度 朝向 旋转
  30. /// </summary>
  31. /// <param name="endPos"></param>
  32. /// <param name="exitPos"></param>
  33. public void SetRoute( Vector3 endPos, Vector3 exitPos )
  34. {
  35. if (material == null)
  36. material = this.GetComponent<MeshRenderer>().material;
  37. this.endPos = endPos;
  38. this.exitPos = exitPos;
  39. //GameObject obj = new GameObject("A");
  40. //obj.transform.position = endPos;
  41. //obj = new GameObject("B");
  42. //obj.transform.position = exitPos;
  43. //Vector3 middle = (endPos - exitPos) / 2;
  44. //transform.position = endPos - middle;
  45. transform.localScale += new Vector3(0, 0, Mathf.Abs( Vector3.Distance(endPos,exitPos) )/20f);
  46. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos))*1.5f));
  47. //Debug.Log((exitPos - endPos).normalized);
  48. // transform.rotation = Quaternion.LookRotation(transform.position, (endPos - exitPos).normalized);
  49. Vector3 pos = endPos + (exitPos - endPos).normalized * (Vector3.Distance(endPos, exitPos));
  50. transform.position = (exitPos+endPos)/2.0f + new Vector3(0, -2f, 0);
  51. Debug.Log("Route " + endPos + " " + exitPos +" "+ transform.position);
  52. point.position = endPos + new Vector3(0, transform.position.y, 0);
  53. transform.LookAt(point);
  54. //transform.eulerAngles = new Vector3(0, transform.eulerAngles.y+182 , 0);
  55. }
  56. public void SetRoute(Vector3 endPos, Vector3 exitPos ,int Index)
  57. {
  58. this.GetComponent<MeshRenderer>().material = list_material[Index];
  59. if (material == null)
  60. material = this.GetComponent<MeshRenderer>().material;
  61. this.endPos = endPos;
  62. this.exitPos = exitPos;
  63. Debug.Log("Route " + endPos + " " + exitPos);
  64. //GameObject obj = new GameObject("A");
  65. //obj.transform.position = endPos;
  66. //obj = new GameObject("B");
  67. //obj.transform.position = exitPos;
  68. //Vector3 middle = (endPos - exitPos) / 2;
  69. //transform.position = endPos - middle;
  70. transform.localScale += new Vector3(0, 0, Mathf.Abs(Vector3.Distance(endPos, exitPos)) / 20f);
  71. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos)) * 1.5f));
  72. //Debug.Log((exitPos - endPos).normalized);
  73. // transform.rotation = Quaternion.LookRotation(transform.position, (endPos - exitPos).normalized);
  74. Vector3 pos = endPos + (exitPos - endPos).normalized * (Vector3.Distance(endPos, exitPos));
  75. transform.position = (exitPos + endPos) / 2.0f + new Vector3(0, -2f, 0);
  76. point.position = endPos + new Vector3(0, transform.position.y, 0);
  77. transform.LookAt(point);
  78. //transform.eulerAngles = new Vector3(0, transform.eulerAngles.y+182 , 0);
  79. }
  80. /// <summary>
  81. /// 3DMapRoute
  82. /// </summary>
  83. /// <param name="endPos"></param>
  84. /// <param name="exitPos"></param>
  85. public void SetMapRoute()
  86. {
  87. if (material == null)
  88. material = this.GetComponent<MeshRenderer>().material;
  89. //this.endPos = endPos;
  90. //this.exitPos = exitPos;
  91. //transform.localScale += new Vector3(0, 0, Mathf.Abs(Vector3.Distance(endPos, exitPos)) / 80f);
  92. //material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos)) * 1.5f));
  93. ////Vector3 pos = endPos + (exitPos - endPos).normalized * (Vector3.Distance(endPos, exitPos));
  94. ////transform.localPosition = (exitPos + endPos) / 2.0f + new Vector3(0, -2f, 0);
  95. ////Debug.Log("Route " + endPos + " " + exitPos + " " + transform.localPosition);
  96. //point.position = endPos + new Vector3(0, transform.position.y, 0);
  97. //transform.LookAt(point);
  98. //material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos)) * 0.8f));
  99. material.SetTextureScale("_MainTex", new Vector2(1, 2f));
  100. }
  101. public void SetMapRoute( Vector3 endPos , Vector3 exitPos)
  102. {
  103. if (material == null)
  104. material = this.GetComponent<MeshRenderer>().material;
  105. //this.endPos = endPos;
  106. //this.exitPos = exitPos;
  107. //transform.localScale += new Vector3(0, 0, Mathf.Abs(Vector3.Distance(endPos, exitPos)) / 80f);
  108. material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos)) * 0.7f));
  109. ////Vector3 pos = endPos + (exitPos - endPos).normalized * (Vector3.Distance(endPos, exitPos));
  110. ////transform.localPosition = (exitPos + endPos) / 2.0f + new Vector3(0, -2f, 0);
  111. ////Debug.Log("Route " + endPos + " " + exitPos + " " + transform.localPosition);
  112. //point.position = endPos + new Vector3(0, transform.position.y, 0);
  113. //transform.LookAt(point);
  114. //material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos)) * 0.8f));
  115. //material.SetTextureScale("_MainTex", new Vector2(1, 2f));
  116. }
  117. /*
  118. * TEST
  119. *
  120. *
  121. *
  122. */
  123. public Transform A;
  124. public Transform B;
  125. public void Test()
  126. {
  127. SetRoute(A.position, B.position);
  128. }
  129. }