70 lines
1.7 KiB
C#
70 lines
1.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class SpriteButton
|
|
{
|
|
private GameObject _self;
|
|
private bool _isHold;
|
|
|
|
public bool isButtonActive;
|
|
|
|
public event Action OnButtonDown;
|
|
public event Action OnButtonUp;
|
|
public event Action OnButtonHold;
|
|
|
|
public SpriteButton(GameObject gameObject, bool initActive = true)
|
|
{
|
|
_self = gameObject;
|
|
isButtonActive = initActive;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查按钮
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
|
|
public void SetActive(bool isActive)
|
|
{
|
|
isButtonActive = isActive;
|
|
}
|
|
public Transform GetTransform()
|
|
{
|
|
return _self.transform;
|
|
}
|
|
}
|
|
}
|