135 lines
4.1 KiB
C#
135 lines
4.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using Yarn.Unity;
|
|
using System;
|
|
|
|
public class ClueInteraction : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
|
|
{
|
|
private Transform originalParent;
|
|
public Transform OriginalParent => originalParent;
|
|
private Canvas canvas;
|
|
private ClueItem clueItem;
|
|
[HideInInspector]
|
|
public bool isFavorited = false; // 初始未收藏
|
|
|
|
private RectTransform rectTransform;
|
|
private Vector2 originalAnchorMin;
|
|
private Vector2 originalAnchorMax;
|
|
private Vector2 originalOffsetMin;
|
|
private Vector2 originalOffsetMax;
|
|
private Vector2 originalPivot;
|
|
|
|
private CursorMode cursorMode = CursorMode.Auto;
|
|
|
|
public event Action OnInteraction;
|
|
|
|
private bool isLocked=false;
|
|
|
|
private MemoryClueSlot slot;
|
|
|
|
private void Start()
|
|
{
|
|
canvas = GetComponentInParent<Canvas>();
|
|
clueItem = GetComponent<ClueItem>();
|
|
rectTransform = GetComponent<RectTransform>();
|
|
slot=FindObjectOfType<MemoryClueSlot>();
|
|
|
|
// 存储初始尺寸属性
|
|
StoreOriginalTransform();
|
|
}
|
|
|
|
private void StoreOriginalTransform()
|
|
{
|
|
originalParent = transform.parent;
|
|
originalAnchorMin = rectTransform.anchorMin;
|
|
originalAnchorMax = rectTransform.anchorMax;
|
|
originalOffsetMin = rectTransform.offsetMin;
|
|
originalOffsetMax = rectTransform.offsetMax;
|
|
originalPivot = rectTransform.pivot;
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
// 可以设置收藏夹样式的鼠标指针
|
|
// Cursor.SetCursor(favoriteCursor, Vector2.zero, cursorMode);
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
Cursor.SetCursor(null, Vector2.zero, cursorMode);
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
// if (!isFavorited||isLocked) return;
|
|
// transform.position = Input.mousePosition+new Vector3(0,200,0);
|
|
// transform.rotation = Quaternion.Euler(0, 0, -90f);
|
|
}
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
if (!isFavorited||isLocked) return;
|
|
|
|
transform.SetParent(canvas.transform);
|
|
GetComponent<CanvasGroup>().blocksRaycasts = false;
|
|
slot.coverOpen.enabled=true;
|
|
slot.coverClose.enabled=false;
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (!isFavorited||isLocked) return;
|
|
|
|
transform.position = Input.mousePosition+new Vector3(0,200,0);
|
|
transform.rotation = Quaternion.Euler(0, 0, -90f);
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
if (!isFavorited||isLocked) return;
|
|
|
|
if (transform.parent == canvas.transform)
|
|
{
|
|
ReturnToOriginalPosition();
|
|
}
|
|
GetComponent<CanvasGroup>().blocksRaycasts = true;
|
|
}
|
|
|
|
public void ReturnToOriginalPosition()
|
|
{
|
|
slot.coverOpen.enabled=false;
|
|
slot.coverClose.enabled=true;
|
|
isLocked=false;
|
|
transform.rotation = Quaternion.Euler(0, 0, 0);
|
|
transform.SetParent(originalParent);
|
|
rectTransform.anchorMin = originalAnchorMin;
|
|
rectTransform.anchorMax = originalAnchorMax;
|
|
rectTransform.offsetMin = originalOffsetMin;
|
|
rectTransform.offsetMax = originalOffsetMax;
|
|
rectTransform.pivot = originalPivot;
|
|
transform.localPosition = Vector3.zero; // 重置为初始局部位置
|
|
}
|
|
|
|
public void SetClueParentAndPosition(Transform newParent)
|
|
{
|
|
isLocked=true;
|
|
Destroy(this.gameObject);
|
|
// rectTransform.SetParent(newParent);
|
|
// // rectTransform.anchorMin = Vector2.zero;
|
|
// // rectTransform.anchorMax = Vector2.one;
|
|
// // rectTransform.offsetMin = Vector2.zero;
|
|
// // rectTransform.offsetMax = Vector2.zero;
|
|
// rectTransform.pivot = new Vector2(0.5f, 0.5f);
|
|
// transform.localPosition = Vector3.zero;
|
|
}
|
|
|
|
public void SetIsFavorited(bool isFavorited)
|
|
{
|
|
this.isFavorited = isFavorited;
|
|
}
|
|
public void SetIsLock(bool isLocked)
|
|
{
|
|
this.isLocked = isLocked;
|
|
}
|
|
}
|