87 lines
2.3 KiB
C#
87 lines
2.3 KiB
C#
using System.Collections.Generic;
|
|
using AibisDream.Framework;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class CursorManager : MonoBehaviour
|
|
{
|
|
[Header("指针图片")] public Sprite defaultCursor;
|
|
public Sprite nextTalk, talking, option, interactive, holding, disable, eye;
|
|
|
|
private Image _cursor;
|
|
private RectTransform _cursorCanvas;
|
|
|
|
// 卸载时注销事件
|
|
private List<IUnRegister> _eventUnRegisters = new();
|
|
|
|
public void Awake()
|
|
{
|
|
InitRefs();
|
|
InitCursor();
|
|
}
|
|
|
|
private void InitCursor()
|
|
{
|
|
SwitchCursorImage(defaultCursor);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
RegisterEvent();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
// 注销所有事件
|
|
foreach (var eventUnRegister in _eventUnRegisters)
|
|
{
|
|
eventUnRegister.UnRegister();
|
|
}
|
|
|
|
_eventUnRegisters.Clear();
|
|
}
|
|
|
|
private void InitRefs()
|
|
{
|
|
_cursor = transform.Find("Cursor").GetComponent<Image>();
|
|
_cursorCanvas = GetComponent<RectTransform>();
|
|
}
|
|
|
|
private void RegisterEvent()
|
|
{
|
|
// 此处监听所有可能对鼠标状态产生影响的事件
|
|
// 对话相关
|
|
EnumEventSystem.Global.Register<DialogEventEnum, DialogLine>(DialogEventEnum.LineStart, OnDialogLineStart);
|
|
EnumEventSystem.Global.Register(DialogEventEnum.OptionShow, () => SwitchCursorImage(option));
|
|
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
// 获取鼠标指针在UI Camera上的位置
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(_cursorCanvas, Input.mousePosition,
|
|
CameraKit.Instance.uiCamera, out var worldPos);
|
|
|
|
_cursor.rectTransform.localPosition = worldPos;
|
|
}
|
|
|
|
private void SwitchCursorImage(Sprite sprite)
|
|
{
|
|
_cursor.sprite = sprite;
|
|
}
|
|
|
|
private void OnDialogLineStart(DialogLine line)
|
|
{
|
|
// 对话指针
|
|
if (!line.isOption) SwitchCursorImage(talking);
|
|
}
|
|
|
|
private void OnDialogLineEnd()
|
|
{
|
|
// 下一句指针
|
|
SwitchCursorImage(nextTalk);
|
|
}
|
|
}
|
|
} |