ArrowList.cs 4.6 KB

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