Unity_Learn/Assets/Scripts/Controllers/CursorController.cs
2025-09-21 23:56:27 +09:00

62 lines
2.2 KiB
C#

using UnityEngine;
public class CursorController : MonoBehaviour
{
int _mask = (1 << (int)Define.Layer.Ground) | (1 << (int)Define.Layer.Monster);
Texture2D _cursorAttack;
Texture2D _cursorHand;
enum CursorType
{
None,
Hand,
Attack,
}
CursorType _cursorType = CursorType.None;
void Start()
{
_cursorAttack = Managers.Resource.Load<Texture2D>("Textures/Cursor/Attack");
_cursorHand = Managers.Resource.Load<Texture2D>("Textures/Cursor/Hand");
}
void Update()
{
if(Input.GetMouseButton(0)) return;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Debug.DrawRay(Camera.main.transform.position, ray.direction * 100.0f, Color.blue, 1.0f);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100.0f, _mask))
{
if(hit.collider.gameObject.layer == (int)Define.Layer.Monster)
{
// 몬스터 클릭시 공격 처리
// 이 두번째 인자가 뭐냐면 보면 이게 16*16짜리 이미지에 커서를 그리면 이 커서 이미지 끝에 맞춰야 하는데 .zero로 하면 이미지의 끝이 아니라
// 16*16 의 아이콘 레이어의 끝단에 맞게 된다. 그래서 커서 그림에 맞게 좀 옮겨 줘야 한다.
// 세번째 인자는 오토에 맞게 맞춰주는건데 그냥 오토가 편하다. 수동으로 하는건 나중에 필요하면 따로 알아서 학습하는거로
// Cursor.SetCursor(_cursorAttack, new Vector2(_cursorAttack.width / 5, 0), CursorMode.Auto);
if(_cursorType != CursorType.Attack)
{
Cursor.SetCursor(_cursorAttack, new Vector2(_cursorAttack.width / 5, 0), CursorMode.Auto);
_cursorType = CursorType.Attack;
}
}
else
{
if (_cursorType != CursorType.Hand)
{
Cursor.SetCursor(_cursorHand, new Vector2(_cursorHand.width / 3, 0), CursorMode.Auto);
_cursorType = CursorType.Hand;
}
}
}
}
}