MouseConfirm.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. void Update()
  12. {
  13. if (!SB_EventSystem.ins.simulateMouseIsAwaked)
  14. {
  15. Hide();
  16. return;
  17. }
  18. if (!_targetSelectable)
  19. {
  20. Hide();
  21. }
  22. }
  23. void Show() {
  24. if (KeyBoardSelector.ins) return;
  25. if (!gameObject.activeSelf) gameObject.SetActive(true);
  26. }
  27. public void Hide() {
  28. ResumeTarget();
  29. _targetSelectable = null;
  30. if (KeyBoardSelector.ins) return;
  31. if (gameObject.activeSelf) gameObject.SetActive(false);
  32. }
  33. public void OnClikc_Confirm(bool hide = true)
  34. {
  35. try
  36. {
  37. if (_targetSelectable && _targetSelectable.interactable) {
  38. Button btn = _targetSelectable.GetComponent<Button>();
  39. btn.onClick.Invoke();
  40. }
  41. }
  42. catch (System.Exception e)
  43. {
  44. Debug.LogError(e.Message);
  45. Debug.LogError(e.StackTrace);
  46. }
  47. if (hide) Hide();
  48. }
  49. public void SetSelectable(Selectable selectable) {
  50. if (!selectable) return;
  51. if (selectable == _targetSelectable) return;
  52. ResumeTarget();
  53. Button btn = selectable.GetComponent<Button>();
  54. if (!btn) return;
  55. if (!btn.interactable) return;
  56. _targetSelectable = selectable;
  57. RectTransform rtf = _targetSelectable.transform as RectTransform;
  58. _btnTransition = btn.transition;
  59. if (btn.transition == Selectable.Transition.ColorTint) { //变色和放大
  60. Vector3 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(rtf, Vector2.one * 0.5f);
  61. rtf.pivot = Vector2.one * 0.5f;
  62. rtf.position = pos;
  63. _targetlocalScale = rtf.localScale;
  64. rtf.localScale = rtf.localScale * 1.15f;
  65. _targetColor = _targetSelectable.targetGraphic.color;
  66. var newcolor = _targetSelectable.targetGraphic.color;
  67. newcolor.a = 0.6f;
  68. _targetSelectable.targetGraphic.color = newcolor;
  69. } else if (btn.transition == Selectable.Transition.None) { //则使用自定义变色和放大
  70. _mouseButton = rtf.GetComponent<MouseButton>();
  71. if (_mouseButton) _mouseButton.OnSelect();
  72. }
  73. Show();
  74. }
  75. Vector3 _targetlocalScale;
  76. Color _targetColor;
  77. Selectable.Transition _btnTransition;
  78. MouseButton _mouseButton;
  79. void ResumeTarget()
  80. {
  81. if (_targetSelectable) {
  82. if (_btnTransition == Selectable.Transition.ColorTint) {
  83. _targetSelectable.transform.localScale = _targetlocalScale;
  84. _targetSelectable.targetGraphic.color = _targetColor;
  85. } else if (_btnTransition == Selectable.Transition.None) {
  86. if (_mouseButton) _mouseButton.OnExit();
  87. }
  88. }
  89. _mouseButton = null;
  90. }
  91. }