using System; using UnityEngine; namespace AibisDream { public class SpriteButton { private GameObject _self; private bool _isHold; private bool _isButtonActive; public event Action OnButtonDown; public event Action OnButtonUp; public event Action OnButtonHold; public SpriteButton(GameObject gameObject) { _self = gameObject; _isButtonActive = true; } /// /// 检查按钮 /// /// public void CheckButtonClick() { if (!_isButtonActive) { _isHold = false; return; } // 发射射线 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction); // 检查射线是否与当前按钮接触 if (Input.GetMouseButton(0) && hit.collider && hit.collider.gameObject == _self) { // 按下 if (!_isHold) { OnButtonDown?.Invoke(); } OnButtonHold?.Invoke(); _isHold = true; } else { if (_isHold) { OnButtonUp?.Invoke(); } _isHold = false; } } } }