123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 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;
- }
- /// <summary>
- /// Initialization
- /// </summary>
- public bool Init(float timeend)
- {
- NibiruShutDownBox.Instance.ReleaseDestory();
- ReleaseDestory();
- time = 0;
- timeEnd = timeend;
- cameraObject = GameObject.Find("MainCamera");
- remindbox = Instantiate(Resources.Load<GameObject>("RemindBox/RemindBox"));
- remindbox.GetComponent<Canvas>().worldCamera = cameraObject.GetComponent<Camera>();
- // 保证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<Text>();
- return true;
- }
- /// <summary>
- /// Create the component of Image。
- /// </summary>
- /// <param name="name"></param>
- /// <param name="position"></param>
- public void Create(string name, Vector3 position, Vector2 size)
- {
- GameObject image = new GameObject(name, typeof(Image));
- //判断是否是滑块
- if (name.Equals("VolumeTag"))
- {
- tagImage = image;
- }
- image.GetComponent<Image>().sprite = Resources.Load<Sprite>("RemindBox/" + name);
- image.transform.SetParent(remindbox.transform);
- image.GetComponent<RectTransform>().sizeDelta = size;
- image.GetComponent<Image>().rectTransform.localPosition = position;
- image.GetComponent<Image>().rectTransform.localScale = new Vector3(1f, 1f, 1f);
- image.GetComponent<Image>().rectTransform.localRotation = Quaternion.identity;
- image.GetComponent<Image>().raycastTarget = false;
- }
- /// <summary>
- /// Create the component of Text。
- /// </summary>
- /// <param name="name"></param>
- /// <param name="context"></param>
- /// <param name="position"></param>
- public void Create(string name, string context, Vector3 position, Vector2 size)
- {
- GameObject text = new GameObject(name);
- text.AddComponent<Text>();
- Text mText = text.GetComponent<Text>();
- mText.font = defaultText.font;
- text.transform.SetParent(remindbox.transform);
- text.GetComponent<RectTransform>().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;
- }
- /// <summary>
- /// Create the component of Button。
- /// </summary>
- /// <param name="name"></param>
- /// <param name="position"></param>
- /// <param name="action"></param>
- public void Create(string name, Vector3 position, Vector2 size, NibiruRemindBoxEvent.RemindBoxEvent action)
- {
- GameObject quad =
- (GameObject) Instantiate(Resources.Load<GameObject>("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<NibiruRemindBoxEvent>().handleRemindBox = action;
- quad.GetComponent<MeshRenderer>().material.color = new Color(0, 0, 0, 0f);
- // 怀疑是unity的bug,导致碰撞信息失效
- quad.GetComponent<BoxCollider>().enabled = false;
- quad.GetComponent<BoxCollider>().enabled = true;
- }
- /// <summary>
- /// Create tag and reset time.
- /// </summary>
- /// <param name="position"></param>
- /// <param name="size"></param>
- public void Create(Vector3 position, Vector2 size)
- {
- if (time >= timeEnd)
- {
- Image[] contents = remindbox.GetComponentsInChildren<Image>();
- foreach (Image child in contents)
- {
- child.color = new Color(255, 255, 255, 1);
- }
- Text[] context = remindbox.GetComponentsInChildren<Text>();
- foreach (Text child in context)
- {
- child.color = new Color(255, 255, 255, 1);
- }
- }
- time = 0;
- if (tagImage != null)
- {
- Destroy(tagImage);
- }
- Create("VolumeTag", position, size);
- }
- /// <summary>
- /// Close
- /// </summary>
- public void Close()
- {
- isClose = true;
- }
- public void ReleaseDestory()
- {
- if (remindbox != null)
- {
- Destroy(remindbox);
- }
- isClose = false;
- }
- /// <summary>
- /// FadeOut
- /// </summary>
- /// <param name="remindbox"></param>
- public void FadeOut(GameObject remindbox)
- {
- if (remindbox != null)
- {
- Image[] contents = remindbox.GetComponentsInChildren<Image>();
- foreach (Image child in contents)
- {
- child.color = new Color(255, 255, 255, child.color.a - 0.01f);
- }
- Text[] context = remindbox.GetComponentsInChildren<Text>();
- foreach (Text child in context)
- {
- child.color = new Color(255, 255, 255, child.color.a - 0.01f);
- }
- MeshRenderer[] meshRenderer = remindbox.GetComponentsInChildren<MeshRenderer>();
- 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);
- }
- }
- }
- }
- }
|