public class SlotScript : MonoBehaviour, IDropHander
{
public int id;
private float score = 0;
public void OnDrop(PointerEventData eventData)
{
Debug.Log("Item Dropped");
if(eventData.pointerDrag != null)
{
if (eventData.pointerDrag.GetComponent<DragAndDrop>().id == id)
{
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = this.GetComponent<RectTransform>().anchoredPosition;
}
else
{
eventData.pointerDrag.GetComponent<DragAndDrop>().ResetPos();
}
score += 1;
Debug.Log("Score is: " + score);
if (score > 14)
{
SceneManager.LoadScene(1, LoadSceneMode.Single);
}
}
}
}
我检查了我的另一个脚本,它拖放了对象,但这些都不是问题。我只是想继续提高我的分数,即使 ID 号不同。
我决定创建另一个脚本来计算有多少个 id 满足条件
if (eventData.pointerDrag.GetComponent<DragAndDrop>().id == id)
,现在可以使用了。我使用列表作为使用相同脚本来计算其他 15 个对象的方法。
public class Counter : MonoBehaviour
{
public List<SlotScript> slots;
void Start()
{
slots = new List<SlotScript>(FindObjectsOfType<SlotScript>());
}
void Update()
{
int matchedCount = CountMatchedSlots();
Debug.Log("Matched Count: " + matchedCount);
}
int CountMatchedSlots()
{
int count = 0;
foreach (SlotScript slot in slots)
{
if (slot.IsMatched())
{
count++;
if(count == 15)
{
SceneManager.LoadScene(1, LoadSceneMode.Single);
}
}
}
return count;
}
}