71 lines
1.5 KiB
C#
71 lines
1.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class UI_Cursor : MonoBehaviour
|
|
{
|
|
public Vector2 xClamp;
|
|
|
|
public Vector2 yClamp;
|
|
|
|
public float speed;
|
|
|
|
public Camera camera;
|
|
|
|
private Button selectedButton;
|
|
|
|
private void Update()
|
|
{
|
|
CursorClickUpdate();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
CursorMovementUpdate();
|
|
}
|
|
|
|
private void CursorClickUpdate()
|
|
{
|
|
if (Input.GetMouseButtonDown(0) && (bool)selectedButton)
|
|
{
|
|
selectedButton.onClick.Invoke();
|
|
}
|
|
}
|
|
|
|
private void CursorMovementUpdate()
|
|
{
|
|
float x = Input.GetAxis("Mouse X") * speed;
|
|
float y = Input.GetAxis("Mouse Y") * speed;
|
|
Vector3 position = (base.transform.position += new Vector3(x, y, 0f));
|
|
position.x = Mathf.Clamp(position.x, xClamp.x, xClamp.y);
|
|
position.y = Mathf.Clamp(position.y, yClamp.x, yClamp.y);
|
|
base.transform.position = position;
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D collision)
|
|
{
|
|
if (collision.gameObject.layer == LayerMask.NameToLayer("UI"))
|
|
{
|
|
if (selectedButton != null)
|
|
{
|
|
selectedButton.image.color = selectedButton.colors.normalColor;
|
|
}
|
|
if (collision.TryGetComponent<Button>(out selectedButton))
|
|
{
|
|
selectedButton.image.color = selectedButton.colors.pressedColor;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit2D(Collider2D collision)
|
|
{
|
|
if (collision.gameObject.layer == LayerMask.NameToLayer("UI"))
|
|
{
|
|
if (selectedButton != null)
|
|
{
|
|
selectedButton.image.color = selectedButton.colors.normalColor;
|
|
}
|
|
selectedButton = null;
|
|
}
|
|
}
|
|
}
|