MouseConfirm.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using JC.Unity.UI;
  7. using JC.Unity;
  8. public class MouseConfirm : MonoBehaviour
  9. {
  10. [NonSerialized] public Selectable _targetSelectable;
  11. [NonSerialized] public Action<Selectable> OnResumeTarget;
  12. void Update()
  13. {
  14. if (!SB_EventSystem.ins.simulateMouseIsAwaked)
  15. {
  16. Hide();
  17. return;
  18. }
  19. if (!_targetSelectable)
  20. {
  21. Hide();
  22. }
  23. }
  24. void Show() {
  25. if (KeyBoardSelector.ins) return;
  26. if (!gameObject.activeSelf) gameObject.SetActive(true);
  27. }
  28. public void Hide() {
  29. ResumeTarget();
  30. _targetSelectable = null;
  31. if (KeyBoardSelector.ins) return;
  32. if (gameObject.activeSelf) gameObject.SetActive(false);
  33. }
  34. public void OnClikc_Confirm(bool hide = true)
  35. {
  36. try
  37. {
  38. if (_targetSelectable && _targetSelectable.interactable) {
  39. Button btn = _targetSelectable.GetComponent<Button>();
  40. btn.onClick.Invoke();
  41. }
  42. }
  43. catch (System.Exception e)
  44. {
  45. Debug.LogError(e.Message);
  46. Debug.LogError(e.StackTrace);
  47. }
  48. if (hide) Hide();
  49. }
  50. public void SetSelectable(Selectable selectable) {
  51. if (!selectable) return;
  52. if (selectable == _targetSelectable) return;
  53. ResumeTarget();
  54. Button btn = selectable.GetComponent<Button>();
  55. if (!btn) return;
  56. if (!btn.interactable) return;
  57. _targetSelectable = selectable;
  58. RectTransform rtf = _targetSelectable.transform as RectTransform;
  59. _btnTransition = btn.transition;
  60. _mouseButton = rtf.GetComponent<MouseButton>();
  61. if (btn.transition == Selectable.Transition.ColorTint)
  62. { //变色和放大
  63. Vector3 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(rtf, Vector2.one * 0.5f);
  64. rtf.pivot = Vector2.one * 0.5f;
  65. rtf.position = pos;
  66. _targetlocalScale = rtf.localScale;
  67. rtf.localScale = rtf.localScale * 1.15f;
  68. _targetColor = _targetSelectable.targetGraphic.color;
  69. var newcolor = _targetSelectable.targetGraphic.color;
  70. newcolor.a = 0.6f;
  71. _targetSelectable.targetGraphic.color = newcolor;
  72. }
  73. else if (btn.transition == Selectable.Transition.None)
  74. { //则使用自定义变色和放大
  75. //_mouseButton = rtf.GetComponent<MouseButton>();
  76. if (_mouseButton) _mouseButton.OnSelect();
  77. }
  78. else if (btn.transition == Selectable.Transition.SpriteSwap) {
  79. //_mouseButton = rtf.GetComponent<MouseButton>();
  80. if (_mouseButton) _mouseButton.OnSelect();
  81. }
  82. if (_mouseButton) _mouseButton.OnSendStart();
  83. Show();
  84. }
  85. Vector3 _targetlocalScale;
  86. Color _targetColor;
  87. Selectable.Transition _btnTransition;
  88. MouseButton _mouseButton;
  89. void ResumeTarget()
  90. {
  91. if (_targetSelectable) {
  92. if (_btnTransition == Selectable.Transition.ColorTint) {
  93. _targetSelectable.transform.localScale = _targetlocalScale;
  94. _targetSelectable.targetGraphic.color = _targetColor;
  95. } else if (_btnTransition == Selectable.Transition.None) {
  96. if (_mouseButton) _mouseButton.OnExit();
  97. }
  98. else if (_btnTransition == Selectable.Transition.SpriteSwap)
  99. {
  100. if (_mouseButton) _mouseButton.OnExit();
  101. }
  102. }
  103. if (_mouseButton) _mouseButton.OnSendEnd();
  104. _mouseButton = null;
  105. OnResumeTarget?.Invoke(_targetSelectable);
  106. }
  107. }