187 lines
5.2 KiB
C#
187 lines
5.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using System.Collections.Generic;
|
|
using AibisDream;
|
|
|
|
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 isInDialogue = false; // 是否处于对话状态
|
|
private bool isDraggingPlug = false; // 是否正在拖拽Plug
|
|
private bool isUsingTool = false; // 是否处于使用工具状态
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
ResetCursor(); // 设置为默认光标
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
cursorImage.transform.position = Input.mousePosition;
|
|
UpdateCursorState();
|
|
|
|
// 检测点击事件
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
HandleClick();
|
|
}
|
|
}
|
|
|
|
private void UpdateCursorState()
|
|
{
|
|
if (DialogController.Instance.IsTalking)
|
|
{
|
|
SetCursor(dialogueCursorSprite);
|
|
dialogueCanvas.sortingOrder = 1000; // 确保对话窗口在最前
|
|
dialogueCanvas.overrideSorting = true;
|
|
dialogueCanvas.GetComponent<GraphicRaycaster>().blockingMask = LayerMask.GetMask("Everything");
|
|
}
|
|
else
|
|
{
|
|
dialogueCanvas.overrideSorting = false;
|
|
dialogueCanvas.GetComponent<GraphicRaycaster>().blockingMask = LayerMask.GetMask("Nothing");
|
|
|
|
if (IsHoveringOverDragItem())
|
|
{
|
|
SetCursor(handCursorSprite);
|
|
}
|
|
else if (IsHoveringOverDialogueItem())
|
|
{
|
|
SetCursor(searchCursorSprite, true); // 使用翻转光标
|
|
}
|
|
else
|
|
{
|
|
ResetCursor();
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool IsHoveringOverDialogueItem()
|
|
{
|
|
if (EventSystem.current.IsPointerOverGameObject())
|
|
{
|
|
PointerEventData pointerData = new PointerEventData(EventSystem.current) { position = Input.mousePosition };
|
|
var results = new List<RaycastResult>();
|
|
EventSystem.current.RaycastAll(pointerData, results);
|
|
|
|
foreach (var result in results)
|
|
{
|
|
if (result.gameObject.CompareTag("DialogueObject")) // 物体需添加 DialogueObject 标签
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private bool IsHoveringOverDragItem()
|
|
{
|
|
if (EventSystem.current.IsPointerOverGameObject())
|
|
{
|
|
PointerEventData pointerData = new PointerEventData(EventSystem.current) { position = Input.mousePosition };
|
|
var results = new List<RaycastResult>();
|
|
EventSystem.current.RaycastAll(pointerData, results);
|
|
|
|
foreach (var result in results)
|
|
{
|
|
if (result.gameObject.CompareTag("Dragable")) // 物体需添加 Dragable 标签
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 处理点击事件
|
|
private void HandleClick()
|
|
{
|
|
PointerEventData pointerData = new PointerEventData(EventSystem.current) { position = Input.mousePosition };
|
|
var results = new List<RaycastResult>();
|
|
EventSystem.current.RaycastAll(pointerData, results);
|
|
|
|
foreach (var result in results)
|
|
{
|
|
if (result.gameObject.CompareTag("DialogueObject"))
|
|
{
|
|
string dialogueNode =result.gameObject.name+"Dialogue"; // 假设物体名是对话节点
|
|
DialogController.Instance.StartDialogNode(dialogueNode);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void EnterDialogue()
|
|
{
|
|
isInDialogue = true;
|
|
}
|
|
|
|
public void ExitDialogue()
|
|
{
|
|
isInDialogue = false;
|
|
ResetCursor();
|
|
}
|
|
|
|
public void StartDraggingPlug()
|
|
{
|
|
isDraggingPlug = true;
|
|
}
|
|
|
|
public void StopDraggingPlug()
|
|
{
|
|
isDraggingPlug = false;
|
|
ResetCursor();
|
|
}
|
|
|
|
public void StartUsingTool()
|
|
{
|
|
isUsingTool = true;
|
|
}
|
|
|
|
public void StopUsingTool()
|
|
{
|
|
isUsingTool = 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);
|
|
}
|
|
}
|
|
|
|
public void ResetCursor()
|
|
{
|
|
cursorImage.sprite = defaultCursorSprite;
|
|
cursorImage.rectTransform.localScale = Vector3.one; // 恢复为不翻转的默认状态
|
|
}
|
|
}
|