123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- namespace SC.XR.Unity
- {
-
- [RequireComponent(typeof(CameraFollower))]
- public class Module_Notice : MonoBehaviour
- {
- private static Module_Notice instance;
- public static Module_Notice getInstance
- {
- get
- {
- if (instance == null)
- {
- instance = Instantiate(Resources.Load<GameObject>("prefabs/Module_Notice")).GetComponent<Module_Notice>();
- }
- return instance;
- }
- }
- public string _mainText = "";
- public string _minorText = "";
- public FollowType _isFollow = FollowType.False;
- [Range(0.1f, 10.0f)]
- public float _distance = 1.2f;
- public float _durationTime = 3f;
- public NoticeType _type = NoticeType.None;
- public AlignmentType _anchorType = AlignmentType.Center;
-
- private List<Image> _imageList;
- public List<Image> ImageList
- {
- get
- {
- if (_imageList == null)
- {
- _imageList = new List<Image>(GetComponentsInChildren<Image>());
- }
- return _imageList;
- }
- }
- private List<TextMeshProUGUI> _textMeshProUGUIList;
- public List<TextMeshProUGUI> TextMeshProUGUIList
- {
- get
- {
- if (_textMeshProUGUIList == null)
- {
- _textMeshProUGUIList = new List<TextMeshProUGUI>(GetComponentsInChildren<TextMeshProUGUI>());
- }
- return _textMeshProUGUIList;
- }
- }
- private CameraFollower _follower;
- public CameraFollower _Follower
- {
- get
- {
- if (_follower == null)
- {
- _follower = GetComponentInChildren<CameraFollower>();
- }
- return _follower;
- }
- }
- private NoticeEffect _effect;
- public NoticeEffect _Effect
- {
- get
- {
- if (_effect == null)
- {
- _effect = GetComponentInChildren<NoticeEffect>();
- }
- return _effect;
- }
- }
- private void Init()
- {
- if (_textMeshProUGUIList == null)
- {
- _textMeshProUGUIList = new List<TextMeshProUGUI>(GetComponentsInChildren<TextMeshProUGUI>());
- }
- if (_imageList == null)
- {
- _imageList = new List<Image>(GetComponentsInChildren<Image>());
- }
- if (_follower == null)
- {
- _follower = GetComponentInChildren<CameraFollower>();
- }
- if (_effect == null)
- {
- _effect = GetComponentInChildren<NoticeEffect>();
- }
- }
- public void RefreshInfo()
- {
- Init();
- foreach (var item in TextMeshProUGUIList)
- {
- #if UNITY_EDITOR
- UnityEditor.Undo.RecordObject(item, " modify Property1");
- #endif
- }
- foreach (var item in ImageList)
- {
- #if UNITY_EDITOR
- UnityEditor.Undo.RecordObject(item, " modify Property2");
- #endif
- }
- SetMainText(_mainText);
- SetSubText(_minorText);
- SetIsFollow(_isFollow);
- SetDistance(_distance);
- SetDurationTime(_durationTime);
- SetIconTip(_type);
- }
- public void SetNoticeInfo(string mainString, string subString, NoticeType type = NoticeType.Warning, float distance = 0.8f, AlignmentType _anchorType = AlignmentType.Center, FollowType isFollower = FollowType.True)
- {
- SetMainText(mainString);
- SetSubText(subString);
- SetIsFollow(isFollower);
- SetDistance(distance);
- SetIconTip(type);
- SetTextAnchor(_anchorType);
- }
- public void SetNoticeInfo(string mainString, string subString)
- {
- SetMainText(mainString);
- SetSubText(subString);
- }
- private void OnEnable()
- {
- // StartNotice();
- foreach (var item in ImageList)
- {
- item.color = new Color(item.color.r, item.color.g, item.color.b, 0);
- }
- foreach (var item in TextMeshProUGUIList)
- {
- item.color = new Color(item.color.r, item.color.g, item.color.b, 0);
- }
- }
- public void StartNotice()
- {
- RefreshInfo();
- if (_Effect != null)
- {
- _Effect.enabled = true;
- }
- }
- public void StartNotice(float time)
- {
- SetDurationTime(time);
- if (_Effect != null && !_Effect.enabled)
- {
- _Effect.enabled = true;
- }
- }
- public void StopNotice()
- {
- if (_Effect != null && _Effect.enabled)
- {
- _Effect.enabled = false;
- }
- }
- private void OnDisable()
- {
- _textMeshProUGUIList = null;
- _follower = null;
- _effect = null;
- }
- private void SetTextAnchor(AlignmentType anchorType )
- {
- foreach (var item in TextMeshProUGUIList)
- {
- switch (anchorType)
- {
- case AlignmentType.Left:
- item.alignment = TextAlignmentOptions.Left;
- break;
- case AlignmentType.Center:
- item.alignment = TextAlignmentOptions.Center;
- break;
- case AlignmentType.Right:
- item.alignment = TextAlignmentOptions.Right;
- break;
- default:
- break;
- }
- }
- }
- private void SetMainText(string str)
- {
- foreach (var item in TextMeshProUGUIList)
- {
- if (item.gameObject.name == "MainTip" && str != null)
- {
- item.text = str;
- }
- }
- }
- private void SetSubText(string str)
- {
- foreach (var item in TextMeshProUGUIList)
- {
- if (item.gameObject.name == "MinorTip" && str != null)
- {
- item.text = str;
- }
- }
- }
- private void SetIsFollow(FollowType isFollow)
- {
- if (isFollow== FollowType.True)
- {
- if (_Follower?.enabled == false)
- {
- _Follower.enabled = true;
- }
- }
- else if(isFollow == FollowType.False)
- {
- if (_Follower?.enabled == true)
- {
- _Follower.enabled = false;
- }
- }
- }
- private void SetDistance(float distance)
- {
- if (_Follower != null)
- {
- _Follower.WindowDistance = distance;
- }
- }
- private void SetDurationTime(float time)
- {
- if (_Effect != null)
- {
- _Effect.effectDurtion = time;
- }
- }
- private void SetIconTip(NoticeType type)
- {
- foreach (var item in ImageList)
- {
- if (item.gameObject.name == "IconTip")
- {
- switch (type)
- {
- case NoticeType.None:
- item.enabled = false;
- break;
- case NoticeType.Warning:
- item.enabled = true;
- item.color =new Color(1,0,0,0);
- item.sprite = Resources.Load<Sprite>("Sprites/warning");
- break;
- case NoticeType.Normal:
- item.enabled = true;
- item.color = new Color(1, 1, 1, 0);
- item.sprite = Resources.Load<Sprite>("Sprites/Normal");
- break;
- default:
- break;
- }
- }
- }
- }
- List<string> mainstrs = new List<string>();
- List<string> substrs = new List<string>();
- Coroutine multiple;
- public void AddStrsList(string mainstr,string substr)
- {
- mainstrs.Add(mainstr);
- substrs.Add(substr);
- }
- public void StartMultipleNotice(float time)
- {
- if (mainstrs != null && substrs !=null)
- {
- multiple = StartCoroutine(MultipleNotice(time, mainstrs, substrs));
- }
-
- }
- public void StopMultipleNotice()
- {
- if (multiple !=null)
- {
- mainstrs.Clear();
- substrs.Clear();
- StopCoroutine(multiple);
- }
- }
- IEnumerator MultipleNotice(float time,List<string> mainstrs,List<string> substrs)
- {
- int count =Mathf.Min(mainstrs.Count, substrs.Count);
- for (int i = 0; i < count; i++)
- {
- yield return null;
- SetMainText(mainstrs[i]);
- SetSubText(substrs[i]);
- StartNotice(time);
- yield return new WaitWhile(()=>_Effect.isEnable);
- }
- yield break;
- }
-
- }
- }
|