HintManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SoftKitty
  6. {
  7. /// <summary>
  8. /// This module displays short hint text in a small floating window, useful when the mouse hovers over buttons or icons. To use this module, simply add the <HintText> component to any UI element and fill in the hint text.
  9. /// </summary>
  10. public class HintManager : MonoBehaviour
  11. {
  12. #region Variables
  13. private static HintManager instance;
  14. public RectTransform Rect;
  15. public Text TextContent;
  16. public CanvasGroup HintBg;
  17. private float hintTime = 0F;
  18. private HintText hintScript;
  19. #endregion
  20. #region Internal Methods
  21. public void _showHint(string _text, HintText _hintScript)
  22. {
  23. TextContent.text = _text;
  24. hintScript = _hintScript;
  25. hintTime = 2F;
  26. }
  27. private void Update()
  28. {
  29. if (hintTime > 0F)
  30. {
  31. hintTime -= Time.unscaledDeltaTime;
  32. if (!HintBg.gameObject.activeSelf) HintBg.gameObject.SetActive(true);
  33. HintBg.alpha = Mathf.Clamp01(hintTime * 4F);
  34. HintBg.GetComponent<RectTransform>().sizeDelta = new Vector2(TextContent.rectTransform.sizeDelta.x + 50F, 35F);
  35. if (!hintScript.GetHover())
  36. {
  37. hintTime = Mathf.Min(hintTime, 0.25F);
  38. }
  39. else
  40. {
  41. HintBg.GetComponent<RectTransform>().position = TransferPos(MousePositionWithinScreenRect(), Rect, GetComponentInParent<Canvas>().renderMode == RenderMode.ScreenSpaceCamera ? GetComponentInParent<Canvas>().worldCamera : null);
  42. }
  43. }
  44. else
  45. {
  46. if (HintBg.gameObject.activeSelf) HintBg.gameObject.SetActive(false);
  47. }
  48. }
  49. private Vector3 MousePositionWithinScreenRect()
  50. {
  51. Vector3 _pos = InputProxy.mousePosition;
  52. Vector2 _size = HintBg.GetComponent<RectTransform>().sizeDelta * 0.5F;
  53. _pos.x = Mathf.Clamp(_pos.x, _size.x * (Screen.width * 1F / 1920F), Screen.width- _size.x * (Screen.width * 1F/1920F));
  54. _pos.y = Mathf.Clamp(_pos.y, _size.y * (Screen.height * 1F / 1080F), Screen.height - (_size.y+35F)*(Screen.height * 1F / 1080F));
  55. return _pos;
  56. }
  57. #endregion
  58. /// <summary>
  59. /// Normally you don't need to call this, just attach <HintText.cs> to your ui elements, HintText.cs will call this method when mouse hover.
  60. /// </summary>
  61. /// <param name="_text"></param>
  62. /// <param name="_hintScript"></param>
  63. public static void ShowHint(string _text,HintText _hintScript)
  64. {
  65. if (instance == null)
  66. {
  67. GameObject newObj = Instantiate(Resources.Load<GameObject>("SoftKittyShared/HintManager"), _hintScript.GetComponentInParent<CanvasScaler>().transform);
  68. newObj.transform.SetAsLastSibling();
  69. newObj.transform.localPosition = Vector3.zero;
  70. newObj.transform.localScale = Vector3.one;
  71. instance = newObj.GetComponent<HintManager>();
  72. }
  73. instance._showHint(_text, _hintScript);
  74. instance.transform.SetAsLastSibling();
  75. }
  76. /// <summary>
  77. /// Transfrom mouse position to rect position.
  78. /// </summary>
  79. /// <param name="_pos"></param>
  80. /// <param name="_parentTransform"></param>
  81. /// <param name="_camera"></param>
  82. /// <returns></returns>
  83. public static Vector3 TransferPos(Vector3 _pos, RectTransform _parentTransform,Camera _camera)
  84. {
  85. Vector2 localPosition = Vector2.zero;
  86. RectTransformUtility.ScreenPointToLocalPointInRectangle(
  87. _parentTransform,
  88. _pos,
  89. _camera,
  90. out localPosition);
  91. return _parentTransform.TransformPoint(localPosition);
  92. }
  93. }
  94. }