【问题标题】:Counting number of clicks计算点击次数
【发布时间】:2021-04-12 02:20:21
【问题描述】:

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

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

【问题讨论】:

  • 每次检测到点击时增加一个变量的值
  • 谢谢你的回答但是每次我都想分别计算金币和页面的点击次数。你说的这种方式,如果我点击图片,也会计算页面的点击次数。
  • 所以,您知道图像的点击次数,并且您知道页面的点击次数....看来您的问题是关于简单的算术,而不是关于编程

标签: unity3d counter


【解决方案1】:

不清楚您对coin 有什么想法。它是一个 UI 元素或 GameObejct

我想它是一个带有RigidBodyCollider 的对象:

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);
    }   

}

【讨论】:

  • 感谢您的回复。有没有其他方法可以让我在屏幕上点击图片的次数(没有光线投射)??
猜你喜欢
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 2014-08-14
  • 2021-12-12
  • 2012-08-11
  • 1970-01-01
  • 2020-06-09
相关资源
最近更新 更多