ArrowList.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using XRTool.Util;
  6. public class ArrowList : UnitySingleton<ArrowList>
  7. {
  8. public GameObject arrow;
  9. public int num = 0;
  10. void Update()
  11. {
  12. //if (Input.GetKey(KeyCode.I))
  13. //{
  14. // Vector3[] v3List = GetCorners(1f);
  15. // for (int i = 0; i < v3List.Length; i++)
  16. // {
  17. // Debug.Log(v3List[i]);
  18. // }
  19. //}
  20. }
  21. public void AddArrow(float Valuex, float Valuey)
  22. {
  23. if (this.transform.childCount >= 5)
  24. {
  25. Destroy(transform.GetChild(0).gameObject);
  26. }
  27. ShowArrow(Valuex, Valuey);
  28. }
  29. public void ShowArrow(float Valuex, float Valuey)
  30. {
  31. if (transform.childCount > 0)
  32. {
  33. num++;
  34. if (num > 9)
  35. {
  36. num = 1;
  37. }
  38. }
  39. else
  40. {
  41. num = 1;
  42. }
  43. GameObject obj = Instantiate(arrow) as GameObject;
  44. //obj.GetComponentInChildren<Text>().text = Valuex + "," + Valuey;
  45. obj.transform.position = CameraView.PosChange(Valuex, 1 - Valuey, 1, GHZRTCCamera.Instance.cam.transform);
  46. obj.transform.SetParent(this.transform);
  47. NewJiantou newJiantou = obj.transform.GetComponent<NewJiantou>();
  48. if (newJiantou)
  49. {
  50. newJiantou.Init(num);
  51. }
  52. if (TimerMgr.Instance)
  53. {
  54. TimerMgr.Instance.CreateTimer(() => { Destroy(obj); }, 60f);
  55. }
  56. }
  57. public void DeleteAll()
  58. {
  59. if (transform.childCount > 0)
  60. {
  61. for (int i = 0; i < transform.childCount; i++)
  62. {
  63. Destroy(transform.GetChild(i).gameObject);
  64. }
  65. }
  66. num = 0;
  67. }
  68. public Vector3 PosChange(float a, float b)
  69. {
  70. Vector3[] v3List = GetCorners(1f);
  71. Vector3 posX = Vector3.zero;
  72. float juliX = Vector3.Distance(v3List[1], v3List[0]);
  73. Vector3 vector = (v3List[1] - v3List[0]).normalized; //向量单位化
  74. float rand = juliX * a;//随机距离
  75. posX = vector * rand; //得到新坐标
  76. posX += v3List[0];
  77. Vector3 posY = Vector3.zero;
  78. float juliY = Vector3.Distance(v3List[2], v3List[0]);
  79. Vector3 vectorY = (v3List[0] - v3List[2]).normalized; //向量单位化
  80. float randY = b * juliY;//随机距离
  81. posY = vectorY * randY; //得到新坐标
  82. posY += v3List[2];
  83. return new Vector3(posX.x, posY.y, v3List[0].z);
  84. }
  85. Vector3[] GetCorners(float distance)
  86. {
  87. Vector3[] corners = new Vector3[4];
  88. float halfFOV = (GHZRTCCamera.Instance.cam.fieldOfView * 0.5f) * Mathf.Deg2Rad;
  89. float aspect = GHZRTCCamera.Instance.cam.aspect;
  90. float height = distance * Mathf.Tan(halfFOV);
  91. float width = height * aspect;
  92. Transform tx = GHZRTCCamera.Instance.cam.transform;
  93. // UpperLeft
  94. corners[0] = tx.position - (tx.right * width);
  95. corners[0] += tx.up * height;
  96. corners[0] += tx.forward * distance;
  97. // UpperRight
  98. corners[1] = tx.position + (tx.right * width);
  99. corners[1] += tx.up * height;
  100. corners[1] += tx.forward * distance;
  101. // LowerLeft
  102. corners[2] = tx.position - (tx.right * width);
  103. corners[2] -= tx.up * height;
  104. corners[2] += tx.forward * distance;
  105. // LowerRight
  106. corners[3] = tx.position + (tx.right * width);
  107. corners[3] -= tx.up * height;
  108. corners[3] += tx.forward * distance;
  109. return corners;
  110. }
  111. }