128 lines
4.1 KiB
C#
128 lines
4.1 KiB
C#
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class CursorManager : Singleton<CursorManager>
|
|
{
|
|
[SerializeField] private GameObject border;
|
|
|
|
[Header("指针图片")] public Sprite defaultCursor;
|
|
public Sprite nextTalk, talking, option, interactive, holding, disable;
|
|
|
|
private Image _cursor;
|
|
|
|
private void Start()
|
|
{
|
|
border.SetActive(true);
|
|
InitRefs();
|
|
InitCursor();
|
|
}
|
|
|
|
private void InitCursor()
|
|
{
|
|
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
|
|
_cursor.gameObject.SetActive(false);
|
|
#endif
|
|
Cursor.visible = false;
|
|
_cursor.sprite = defaultCursor;
|
|
}
|
|
|
|
private void InitRefs()
|
|
{
|
|
_cursor = transform.Find("Cursor").GetComponent<Image>();
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
// // 将虚拟指针与鼠标位置同步
|
|
// RectTransformUtility.ScreenPointToLocalPointInRectangle(_cursorCanvas, Input.mousePosition,
|
|
// CameraKit.Instance.uiCamera, out var worldPos);
|
|
|
|
// 判断当前要用什么样的指针
|
|
// _cursor.rectTransform.position = Input.mousePosition;
|
|
|
|
var cursorState = CheckCursorState();
|
|
_cursor.sprite = GetCursorSprite(cursorState);
|
|
}
|
|
|
|
private CursorStateEnum CheckCursorState()
|
|
{
|
|
var gameState = GameManager.Instance.state;
|
|
|
|
if (!gameState.isInGame || gameState.isInPause)
|
|
{
|
|
// 非游戏中显示默认指针
|
|
return CursorStateEnum.Default;
|
|
}
|
|
else if (UIManager.Instance.IsOverRegion("MainUI"))
|
|
{
|
|
// 游戏中鼠标移至右上角Main Panel时显示默认指针
|
|
return CursorStateEnum.Default;
|
|
}
|
|
else if (gameState.isInDialog)
|
|
{
|
|
// 处理对话中的指针
|
|
var dialogState = DialogController.Instance.GetDialogState();
|
|
return HandleDialogState(dialogState);
|
|
}
|
|
else
|
|
{
|
|
// 非对话处理指针交互
|
|
var triggerEnum = EventSystemEx.Instance.GetPointerState();
|
|
return HandleDraggingState(triggerEnum);
|
|
}
|
|
}
|
|
|
|
private CursorStateEnum HandleDialogState(LineSyncState dialogState)
|
|
{
|
|
return dialogState switch
|
|
{
|
|
LineSyncState.Default => CursorStateEnum.Default,
|
|
LineSyncState.PlayText => CursorStateEnum.PlayText,
|
|
LineSyncState.AdvanceDelay => CursorStateEnum.PlayText,
|
|
LineSyncState.WaitForAdvance => CursorStateEnum.WaitForAdvance,
|
|
LineSyncState.WaitForAutoAdvance => CursorStateEnum.WaitForAdvance,
|
|
LineSyncState.Advanced => CursorStateEnum.Default,
|
|
LineSyncState.InOption => CursorStateEnum.InOption,
|
|
_ => CursorStateEnum.Default,
|
|
};
|
|
}
|
|
|
|
private CursorStateEnum HandleDraggingState(TriggerEnum triggerEnum)
|
|
{
|
|
return triggerEnum switch
|
|
{
|
|
TriggerEnum.Dragging => CursorStateEnum.Dragging,
|
|
TriggerEnum.PointerOver => CursorStateEnum.PointerOver,
|
|
_ => CursorStateEnum.Default,
|
|
};
|
|
}
|
|
|
|
private Sprite GetCursorSprite(CursorStateEnum cursorState)
|
|
{
|
|
return cursorState switch
|
|
{
|
|
CursorStateEnum.Default => defaultCursor,
|
|
CursorStateEnum.PlayText => talking,
|
|
CursorStateEnum.WaitForAdvance => nextTalk,
|
|
CursorStateEnum.InOption => option,
|
|
CursorStateEnum.Dragging => holding,
|
|
CursorStateEnum.PointerOver => interactive,
|
|
_ => defaultCursor,
|
|
};
|
|
}
|
|
}
|
|
|
|
public enum CursorStateEnum
|
|
{
|
|
Default,
|
|
PlayText,
|
|
WaitForAdvance,
|
|
InOption,
|
|
Dragging,
|
|
PointerOver
|
|
}
|
|
} |