| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using JC.Unity.UI;
- using JC.Unity;
- public class MouseConfirm : MonoBehaviour
- {
- [NonSerialized] public Selectable _targetSelectable;
- [NonSerialized] public Action<Selectable> OnResumeTarget;
- void Update()
- {
- if (!SB_EventSystem.ins.simulateMouseIsAwaked)
- {
- Hide();
- return;
- }
- if (!_targetSelectable)
- {
- Hide();
- }
- }
- void Show() {
- if (KeyBoardSelector.ins) return;
- if (!gameObject.activeSelf) gameObject.SetActive(true);
- }
- public void Hide() {
- ResumeTarget();
- _targetSelectable = null;
- if (KeyBoardSelector.ins) return;
- if (gameObject.activeSelf) gameObject.SetActive(false);
- }
- public void OnClikc_Confirm(bool hide = true)
- {
- try
- {
- if (_targetSelectable && _targetSelectable.interactable) {
- Button btn = _targetSelectable.GetComponent<Button>();
- btn.onClick.Invoke();
- }
- }
- catch (System.Exception e)
- {
- Debug.LogError(e.Message);
- Debug.LogError(e.StackTrace);
- }
- if (hide) Hide();
- }
- public void SetSelectable(Selectable selectable) {
- if (!selectable) return;
- if (selectable == _targetSelectable) return;
- ResumeTarget();
- Button btn = selectable.GetComponent<Button>();
- if (!btn) return;
- if (!btn.interactable) return;
- _targetSelectable = selectable;
- RectTransform rtf = _targetSelectable.transform as RectTransform;
- _btnTransition = btn.transition;
- _mouseButton = rtf.GetComponent<MouseButton>();
- if (btn.transition == Selectable.Transition.ColorTint)
- { //变色和放大
- Vector3 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(rtf, Vector2.one * 0.5f);
- rtf.pivot = Vector2.one * 0.5f;
- rtf.position = pos;
- _targetlocalScale = rtf.localScale;
- rtf.localScale = rtf.localScale * 1.15f;
- _targetColor = _targetSelectable.targetGraphic.color;
- var newcolor = _targetSelectable.targetGraphic.color;
- newcolor.a = 0.6f;
- _targetSelectable.targetGraphic.color = newcolor;
- }
- else if (btn.transition == Selectable.Transition.None)
- { //则使用自定义变色和放大
- //_mouseButton = rtf.GetComponent<MouseButton>();
- if (_mouseButton) _mouseButton.OnSelect();
- }
- else if (btn.transition == Selectable.Transition.SpriteSwap) {
- //_mouseButton = rtf.GetComponent<MouseButton>();
- if (_mouseButton) _mouseButton.OnSelect();
- }
- if (_mouseButton) _mouseButton.OnSendStart();
- Show();
- }
- Vector3 _targetlocalScale;
- Color _targetColor;
- Selectable.Transition _btnTransition;
- MouseButton _mouseButton;
- void ResumeTarget()
- {
- if (_targetSelectable) {
- if (_btnTransition == Selectable.Transition.ColorTint) {
- _targetSelectable.transform.localScale = _targetlocalScale;
- _targetSelectable.targetGraphic.color = _targetColor;
- } else if (_btnTransition == Selectable.Transition.None) {
- if (_mouseButton) _mouseButton.OnExit();
- }
- else if (_btnTransition == Selectable.Transition.SpriteSwap)
- {
- if (_mouseButton) _mouseButton.OnExit();
- }
- }
- if (_mouseButton) _mouseButton.OnSendEnd();
- _mouseButton = null;
- OnResumeTarget?.Invoke(_targetSelectable);
- }
- }
|