MouseConfirm.cs 3.0 KB

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