LinePathUnit.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using Unity.Mathematics;
  5. using Unity.VisualScripting;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. using XCharts.Runtime;
  9. public class LinePathUnit : MonoBehaviour
  10. {
  11. public string startPos;
  12. public string endPos;
  13. public GameObject lineRenderer;
  14. void Start()
  15. {
  16. }
  17. public void SetPath(Vector3 start, Vector3 end)
  18. {
  19. this.transform.position = start;
  20. Vector3 direction = end - start;
  21. Vector3 up = Vector3.up; // (0,1,0)
  22. // 计算夹角
  23. float angle = Vector3.Angle(direction, up);
  24. // 使用叉积来判断方向
  25. Vector3 crossProduct = Vector3.Cross(direction, up);
  26. if (crossProduct.z < 0)
  27. {
  28. angle = 360 - angle;
  29. }
  30. //print(angle);
  31. float distance = Vector3.Distance(end, start) * 1.02f;
  32. this.transform.localScale = new Vector3(1, distance / 2, 1);
  33. this.transform.eulerAngles = new Vector3(1, 1, -angle);
  34. }
  35. public void SetColor(Color color,Color color1) {
  36. this.lineRenderer.GetComponent<MeshRenderer>().material.SetColor("_Color1",color);
  37. this.lineRenderer.GetComponent<MeshRenderer>().material.SetColor("_Color2", color1);
  38. }
  39. private void Update()
  40. {
  41. if (CameraManager.instance.secondCamera != null)
  42. {
  43. float dyScale = CameraManager.instance.secondCamera.GetComponent<CameraBirdSec>().currentDistance / 14;
  44. dyScale = math.clamp(dyScale, 0.5f, 5f);
  45. this.transform.localScale = new Vector3(dyScale, this.transform.localScale.y, dyScale);
  46. this.lineRenderer.GetComponent<MeshRenderer>().material.SetTextureScale("_MainTex", new Vector2(1, this.transform.localScale.y * 10 / this.transform.localScale.x));
  47. }
  48. }
  49. }