208 lines
5.0 KiB
C#
208 lines
5.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using System.Collections.Generic;
|
|
using AibisDream;
|
|
using Unity.VisualScripting;
|
|
|
|
public class MouseCursorManager : MonoBehaviour
|
|
{
|
|
public static MouseCursorManager Instance { get; private set; }
|
|
|
|
[Header("Cursor Images")]
|
|
public Sprite dialogueCursorSprite;
|
|
public Sprite handCursorSprite; // 手的图标
|
|
public Sprite searchCursorSprite; // 搜索图标
|
|
public Sprite defaultCursorSprite;
|
|
|
|
public Canvas dialogueCanvas; // 对话Canvas
|
|
public Image cursorImage; // 用于显示自定义光标的Image
|
|
private bool isDraggingPlug = false; // 是否正在拖拽Plug
|
|
private bool isUsingTool = false; // 是否处于使用工具状态
|
|
private bool isInClueBoard = false; // 是否处于使用工具状态
|
|
|
|
private Vector3 defaultCursorOffset = new Vector3(20, -20, 0); // default 指针的偏移量
|
|
|
|
public List<Button> ButtonDisableWhenIsTalking;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
ResetCursor(); // 设置为默认光标
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (cursorImage.sprite != defaultCursorSprite)
|
|
{
|
|
cursorImage.transform.position = Input.mousePosition;
|
|
}
|
|
UpdateCursorState();
|
|
|
|
//检测点击事件
|
|
if (Input.GetMouseButtonDown(0)&&!isDraggingPlug)
|
|
{
|
|
|
|
HandleClick();
|
|
}
|
|
}
|
|
|
|
private void UpdateCursorState()
|
|
{
|
|
if(isInClueBoard)
|
|
{
|
|
cursorImage.gameObject.SetActive(false);
|
|
return;
|
|
|
|
}
|
|
else
|
|
{
|
|
cursorImage.gameObject.SetActive(true);
|
|
}
|
|
if (DialogController.Instance.IsTalking)
|
|
{
|
|
foreach(Button button in ButtonDisableWhenIsTalking)
|
|
{
|
|
button.interactable=false;
|
|
|
|
}
|
|
SetCursor(dialogueCursorSprite);
|
|
dialogueCanvas.sortingOrder = 1000; // 确保对话窗口在最前
|
|
dialogueCanvas.overrideSorting = true;
|
|
dialogueCanvas.GetComponent<GraphicRaycaster>().blockingMask = LayerMask.GetMask("Everything");
|
|
}
|
|
else
|
|
{
|
|
foreach(Button button in ButtonDisableWhenIsTalking)
|
|
{
|
|
button.interactable=true;
|
|
|
|
}
|
|
|
|
dialogueCanvas.overrideSorting = false;
|
|
dialogueCanvas.GetComponent<GraphicRaycaster>().blockingMask = LayerMask.GetMask("Nothing");
|
|
|
|
if(isDraggingPlug)
|
|
{
|
|
SetCursor(handCursorSprite);
|
|
}
|
|
|
|
else if (IsHoveringOverDragItem())
|
|
{
|
|
SetCursor(handCursorSprite);
|
|
}
|
|
else if (IsHoveringOverDialogueItem())
|
|
{
|
|
SetCursor(searchCursorSprite, true); // 使用翻转光标
|
|
}
|
|
else
|
|
{
|
|
ResetCursor();
|
|
}
|
|
}
|
|
}
|
|
private bool IsHoveringOverDialogueItem()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
private bool IsHoveringOverDragItem()
|
|
{
|
|
|
|
return false;
|
|
}
|
|
|
|
// 处理点击事件
|
|
private void HandleClick()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StartDraggingPlug()
|
|
{
|
|
isDraggingPlug = true;
|
|
}
|
|
|
|
public void StopDraggingPlug()
|
|
{
|
|
isDraggingPlug = false;
|
|
ResetCursor();
|
|
}
|
|
|
|
public void StartUsingTool()
|
|
{
|
|
isUsingTool = true;
|
|
}
|
|
|
|
public void StopUsingTool()
|
|
{
|
|
isUsingTool = false;
|
|
|
|
ResetCursor();
|
|
}
|
|
public void ClueBoardIn()
|
|
{
|
|
isInClueBoard = true;
|
|
}
|
|
|
|
public void ClueBoardOut()
|
|
{
|
|
isInClueBoard = false;
|
|
|
|
ResetCursor();
|
|
}
|
|
|
|
public void SetCursor(Sprite cursorSprite, bool flip = false)
|
|
{
|
|
if (cursorSprite != null)
|
|
{
|
|
cursorImage.sprite = cursorSprite;
|
|
cursorImage.enabled = true;
|
|
cursorImage.rectTransform.localScale = new Vector3(flip ? -1 : 1, 1, 1);
|
|
|
|
// 如果是默认指针图标,应用偏移量
|
|
if (cursorSprite == defaultCursorSprite)
|
|
{
|
|
cursorImage.transform.position = Input.mousePosition + defaultCursorOffset;
|
|
}
|
|
else
|
|
{
|
|
// 非默认图标时直接跟随鼠标位置
|
|
cursorImage.transform.position = Input.mousePosition;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ResetCursor()
|
|
{
|
|
Cursor.visible = false;
|
|
cursorImage.sprite = defaultCursorSprite;
|
|
cursorImage.rectTransform.localScale = Vector3.one; // 恢复为不翻转的默认状态
|
|
|
|
// 如果是默认指针图标,应用偏移量
|
|
if (cursorImage.sprite == defaultCursorSprite)
|
|
{
|
|
cursorImage.transform.position = Input.mousePosition + defaultCursorOffset;
|
|
}
|
|
else
|
|
{
|
|
// 非默认图标时直接跟随鼠标位置
|
|
cursorImage.transform.position = Input.mousePosition;
|
|
}
|
|
}
|
|
}
|