You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity拖拽匹配分数逻辑需求:无论ID是否相同均累加分数

调整分数累加逻辑的解决方案

你的需求是无论拖拽物品的ID与槽位ID是否匹配,每次完成拖拽放置操作后分数都持续累加。当前代码的核心问题在于score是每个SlotScript实例的私有变量,导致不同槽位的分数各自独立,无法实现全局累加。以下是两种可行的修改方案:

方案一:使用静态变量实现全局分数

将score改为静态变量,让所有SlotScript实例共享同一个分数值:

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

public class SlotScript : MonoBehaviour, IDropHandler
{
    public int id;
    // 改为静态变量,实现全局分数共享
    private static float score = 0;

    public void OnDrop(PointerEventData eventData)
    {
        Debug.Log("Item Dropped");

        if (eventData.pointerDrag != null)
        {
            DragAndDrop dragItem = eventData.pointerDrag.GetComponent<DragAndDrop>();
            if (dragItem.id == id)
            {
                eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
            }
            else
            {
                dragItem.ResetPos();
            }

            // 每次放置操作都累加分数
            score += 1;
            Debug.Log("Score is: " + score);

            if (score > 14)
            {
                SceneManager.LoadScene(1, LoadSceneMode.Single);
            }
        }
    }
}

方案二:使用GameManager管理全局分数(推荐)

创建专门的GameManager单例来统一管理全局状态(如分数),这种方式更符合模块化设计,便于后续扩展:

1. 创建GameManager脚本

using UnityEngine;

public class GameManager : MonoBehaviour
{
    // 单例实例,确保全局唯一
    public static GameManager Instance;
    public float Score { get; private set; } = 0;

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    // 统一的分数累加方法
    public void AddScore(float amount)
    {
        Score += amount;
        Debug.Log("Score is: " + Score);
    }
}

2. 修改SlotScript代码

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

public class SlotScript : MonoBehaviour, IDropHandler
{
    public int id;

    public void OnDrop(PointerEventData eventData)
    {
        Debug.Log("Item Dropped");

        if (eventData.pointerDrag != null)
        {
            DragAndDrop dragItem = eventData.pointerDrag.GetComponent<DragAndDrop>();
            if (dragItem.id == id)
            {
                eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
            }
            else
            {
                dragItem.ResetPos();
            }

            // 通过GameManager累加全局分数
            GameManager.Instance.AddScore(1);

            if (GameManager.Instance.Score > 14)
            {
                SceneManager.LoadScene(1, LoadSceneMode.Single);
            }
        }
    }
}

注意:使用方案二时,需要在场景中创建一个空对象并挂载GameManager脚本,确保它在场景加载时初始化。

内容的提问来源于stack exchange,提问作者Ash Ketchup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 04:14:55