首页 > 解决方案 > 计算点击次数

问题描述

我想使用 Unity 计算页面上的点击次数以及每隔几秒出现的硬币的点击次数。你能告诉我如何获得页面上的点击次数吗?

每次我想分别计算硬币和页面的点击次数。

标签: unity3dcounter

解决方案


它不清楚你的想法coin。它是一个 UI 元素或一个GameObejct.

我想它是一个带有RigidBodyand的对象Collider

public class HitDetector : MonoBehaviour
{
    
    public static int allCounter = 0;
    public static int hitCounter = 0;
        
    
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            allCounter++;
            if (IsHit())
            {
                hitCounter++;
            }
            
            Debug.Log($"All: {allCounter} & hit: {hitCounter}");
        }
    }

    private bool IsHit()
    {
        return Physics2D.Raycast(Camera.current.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    }   

}


推荐阅读