using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using XRTool.Util; public class ArrowList : UnitySingleton { public GameObject arrow; public int num = 0; void Update() { //if (Input.GetKey(KeyCode.I)) //{ // Vector3[] v3List = GetCorners(1f); // for (int i = 0; i < v3List.Length; i++) // { // Debug.Log(v3List[i]); // } //} } public void AddArrow(float Valuex, float Valuey) { if (this.transform.childCount >= 5) { Destroy(transform.GetChild(0).gameObject); } ShowArrow(Valuex, Valuey); } public void ShowArrow(float Valuex, float Valuey) { if (transform.childCount > 0) { num++; if (num > 9) { num = 1; } } else { num = 1; } GameObject obj = Instantiate(arrow) as GameObject; //obj.GetComponentInChildren().text = Valuex + "," + Valuey; obj.transform.position = CameraView.PosChange(Valuex, 1 - Valuey, 1, RemoteRtc.Instance.cam.transform); obj.transform.SetParent(this.transform); NewJiantou newJiantou = obj.transform.GetComponent(); if (newJiantou) { newJiantou.Init(num); } if (TimerMgr.Instance) { TimerMgr.Instance.CreateTimer(() => { Destroy(obj); }, 60f); } } public void DeleteAll() { if (transform.childCount > 0) { for (int i = 0; i < transform.childCount; i++) { Destroy(transform.GetChild(i).gameObject); } } num = 0; } public Vector3 PosChange(float a, float b) { Vector3[] v3List = GetCorners(1f); Vector3 posX = Vector3.zero; float juliX = Vector3.Distance(v3List[1], v3List[0]); Vector3 vector = (v3List[1] - v3List[0]).normalized; //向量单位化 float rand = juliX * a;//随机距离 posX = vector * rand; //得到新坐标 posX += v3List[0]; Vector3 posY = Vector3.zero; float juliY = Vector3.Distance(v3List[2], v3List[0]); Vector3 vectorY = (v3List[0] - v3List[2]).normalized; //向量单位化 float randY = b * juliY;//随机距离 posY = vectorY * randY; //得到新坐标 posY += v3List[2]; return new Vector3(posX.x, posY.y, v3List[0].z); } Vector3[] GetCorners(float distance) { Vector3[] corners = new Vector3[4]; float halfFOV = (RemoteRtc.Instance.cam.fieldOfView * 0.5f) * Mathf.Deg2Rad; float aspect = RemoteRtc.Instance.cam.aspect; float height = distance * Mathf.Tan(halfFOV); float width = height * aspect; Transform tx = RemoteRtc.Instance.cam.transform; // UpperLeft corners[0] = tx.position - (tx.right * width); corners[0] += tx.up * height; corners[0] += tx.forward * distance; // UpperRight corners[1] = tx.position + (tx.right * width); corners[1] += tx.up * height; corners[1] += tx.forward * distance; // LowerLeft corners[2] = tx.position - (tx.right * width); corners[2] -= tx.up * height; corners[2] += tx.forward * distance; // LowerRight corners[3] = tx.position + (tx.right * width); corners[3] -= tx.up * height; corners[3] += tx.forward * distance; return corners; } }