using System; using UnityEngine; using UnityEngine.UI; namespace Nxr.Internal { public class NibiruRemindBoxBase : MonoBehaviour { //private GameObject box; [NonSerialized] public GameObject remindbox; private static bool isClose = false; private Text defaultText; private GameObject cameraObject; private GameObject tagImage; float time = 0; float timeEnd; public bool Showing() { return remindbox != null; } /// /// Initialization /// public bool Init(float timeend) { NibiruShutDownBox.Instance.ReleaseDestory(); ReleaseDestory(); time = 0; timeEnd = timeend; cameraObject = GameObject.Find("MainCamera"); remindbox = Instantiate(Resources.Load("RemindBox/RemindBox")); remindbox.GetComponent().worldCamera = cameraObject.GetComponent(); // 保证UI显示在视角正前方 Vector3 forward = cameraObject.transform.forward * 3; remindbox.transform.localPosition = cameraObject.transform.position + new Vector3(forward.x, forward.y, forward.z); remindbox.transform.localRotation = cameraObject.transform.rotation; defaultText = remindbox.GetComponentInChildren(); return true; } /// /// Create the component of Image。 /// /// /// public void Create(string name, Vector3 position, Vector2 size) { GameObject image = new GameObject(name, typeof(Image)); //判断是否是滑块 if (name.Equals("VolumeTag")) { tagImage = image; } image.GetComponent().sprite = Resources.Load("RemindBox/" + name); image.transform.SetParent(remindbox.transform); image.GetComponent().sizeDelta = size; image.GetComponent().rectTransform.localPosition = position; image.GetComponent().rectTransform.localScale = new Vector3(1f, 1f, 1f); image.GetComponent().rectTransform.localRotation = Quaternion.identity; image.GetComponent().raycastTarget = false; } /// /// Create the component of Text。 /// /// /// /// public void Create(string name, string context, Vector3 position, Vector2 size) { GameObject text = new GameObject(name); text.AddComponent(); Text mText = text.GetComponent(); mText.font = defaultText.font; text.transform.SetParent(remindbox.transform); text.GetComponent().sizeDelta = size; mText.alignment = defaultText.alignment; mText.rectTransform.localPosition = position; mText.rectTransform.localRotation = Quaternion.identity; mText.text = context; mText.fontSize = 50; mText.rectTransform.localScale = new Vector3(0.3f, 0.3f, 1f); mText.raycastTarget = false; } /// /// Create the component of Button。 /// /// /// /// public void Create(string name, Vector3 position, Vector2 size, NibiruRemindBoxEvent.RemindBoxEvent action) { GameObject quad = (GameObject) Instantiate(Resources.Load("RemindBox/Quad"), remindbox.transform); quad.name = name; quad.transform.localPosition = position; quad.transform.localRotation = Quaternion.identity; quad.transform.localScale = new Vector3(size.x, size.y, 1); quad.GetComponent().handleRemindBox = action; quad.GetComponent().material.color = new Color(0, 0, 0, 0f); // 怀疑是unity的bug,导致碰撞信息失效 quad.GetComponent().enabled = false; quad.GetComponent().enabled = true; } /// /// Create tag and reset time. /// /// /// public void Create(Vector3 position, Vector2 size) { if (time >= timeEnd) { Image[] contents = remindbox.GetComponentsInChildren(); foreach (Image child in contents) { child.color = new Color(255, 255, 255, 1); } Text[] context = remindbox.GetComponentsInChildren(); foreach (Text child in context) { child.color = new Color(255, 255, 255, 1); } } time = 0; if (tagImage != null) { Destroy(tagImage); } Create("VolumeTag", position, size); } /// /// Close /// public void Close() { isClose = true; } public void ReleaseDestory() { if (remindbox != null) { Destroy(remindbox); } isClose = false; } /// /// FadeOut /// /// public void FadeOut(GameObject remindbox) { if (remindbox != null) { Image[] contents = remindbox.GetComponentsInChildren(); foreach (Image child in contents) { child.color = new Color(255, 255, 255, child.color.a - 0.01f); } Text[] context = remindbox.GetComponentsInChildren(); foreach (Text child in context) { child.color = new Color(255, 255, 255, child.color.a - 0.01f); } MeshRenderer[] meshRenderer = remindbox.GetComponentsInChildren(); foreach (MeshRenderer mr in meshRenderer) { mr.material.color = new Color(mr.material.color.r, mr.material.color.g, mr.material.color.b, mr.material.color.a - 0.01f); } if (context[0].color.a <= 0) { ReleaseDestory(); time = 0; //清除原点选中效果 NxrReticle mNxrReticle = NxrViewer.Instance.GetNxrReticle(); if (mNxrReticle != null) { mNxrReticle.OnGazeExit(null, null); } } } } void Update() { if (isClose) { time += Time.deltaTime; if (time >= timeEnd) { FadeOut(remindbox); } } } } }