using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class ClueSlotBase : MonoBehaviour, IDropHandler { protected ClueType expectedClueType; // 期望的 Clue 类型 protected ClueInteraction interaction; // Clue 的交互 protected RectTransform OriginalTransform; // Clue 的交互 [HideInInspector] public ClueItem currentClueItem; // 当前插入的 ClueItem [HideInInspector] public bool isOccupied = false; // 插槽是否已占用 [HideInInspector] public bool isLocked = false; // 插槽是否已占用 private Image slotFrame; protected virtual void Start() { currentClueItem = null; slotFrame=transform.GetChild(0).GetComponent(); slotFrame.color=Color.white; } public void SetSlotCorrect() { slotFrame.color=Color.green; SetSlotLockState(true); } public void OnDrop(PointerEventData eventData) { if(isLocked) { Debug.Log("clueSlot locked"); return; } ClueItem clueItem = eventData.pointerDrag.GetComponent(); if (clueItem != null) { Clue clue = clueItem.GetClueData(); interaction = clueItem.GetComponent(); if (clue != null && interaction != null) { isOccupied = true; // 如果当前已经有一个 ClueItem,返回它到集合 if (currentClueItem != null) { ReturnClueToCollection(currentClueItem); } // 更新当前的 ClueItem currentClueItem = clueItem; // 调整位置和父对象 interaction.SetClueParentAndPosition(transform); // 调整 RectTransform 的属性以匹配插槽大小 RectTransform clueRectTransform = clueItem.GetComponent(); OriginalTransform=clueRectTransform; RectTransform slotRectTransform = GetComponent(); clueRectTransform.SetParent(slotRectTransform); clueRectTransform.anchorMin = Vector2.zero; // 左下角 clueRectTransform.anchorMax = Vector2.one; // 右上角 clueRectTransform.offsetMin = Vector2.zero; // 左下偏移 clueRectTransform.offsetMax = Vector2.zero; // 右上偏移 clueRectTransform.pivot = new Vector2(0.5f, 0.5f); // 居中 // 调用虚方法处理插入逻辑 OnClueInserted(clue); } else { interaction?.ReturnToOriginalPosition(); // 类型不匹配,返回原位 } } } // 释放插槽时调用 public virtual void ReleaseSlot() { if (currentClueItem != null) { ReturnClueToCollection(currentClueItem); } isOccupied = false; currentClueItem = null; interaction = null; } // 将 ClueItem 返回到集合的逻辑 protected void ReturnClueToCollection(ClueItem clueItem) { // 这里调用 ClueInteraction 的返回逻辑 if (clueItem != null) { ClueInteraction clueInteraction = clueItem.GetComponent(); clueInteraction?.ReturnToOriginalPosition(); // 返回到原位的逻辑 // 你也可以在这里添加额外的逻辑来处理集合的更新 } } // 插入线索时调用的虚方法,子类可以重写此方法以实现特定功能 protected virtual void OnClueInserted(Clue clue) { // 默认不做任何操作 Debug.Log("Clue inserted in base slot."); } public void SetSlotLockState(bool lockState) { isLocked=lockState; } }