Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一-我怎样才能使一个函数只有在玩家点击它3次时才被激活?_C#_Unity3d_2d Games - Fatal编程技术网

C# 统一-我怎样才能使一个函数只有在玩家点击它3次时才被激活?

C# 统一-我怎样才能使一个函数只有在玩家点击它3次时才被激活?,c#,unity3d,2d-games,C#,Unity3d,2d Games,我的游戏上有一个提示按钮,但每次你点击它,它就会启动。我需要它每3次点击只发射一次 我尝试添加一个loadCount=0和一个if语句 if (loadCount % 3 == 0) { } 但它似乎不起作用 供参考的图像: 以下是脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; [ExecuteInEditMode] public class HintScript : Mon

我的游戏上有一个提示按钮,但每次你点击它,它就会启动。我需要它每3次点击只发射一次

我尝试添加一个
loadCount=0
和一个if语句

if (loadCount % 3 == 0) { }
但它似乎不起作用

供参考的图像:

以下是脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class HintScript : MonoBehaviour 
{

    LineRenderer line;

    // Use this for initialization
    void Start () 
    {

    }

    // Update is called once per frame
    void Update () 
    { 
        line = GetComponent<LineRenderer>();
        line.positionCount = transform.childCount;
        for (int i = 0; i<transform.childCount; i++)
        {
            line.SetPosition(i, transform.GetChild(i).position);
        }
    }

    // This is called from onClick of the Button
    public void Hint() 
    { 
        FindObjectOfType<AdMobManager>().Hint = true; 
        FindObjectOfType<AdMobManager>().showInterstitial(); 
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
[执行编辑模式]
公共类HintScript:单一行为
{
线条渲染器线条;
//用于初始化
无效开始()
{
}
//每帧调用一次更新
无效更新()
{ 
line=GetComponent();
line.positionCount=transform.childCount;

对于(inti=0;i你可以有一个计数器来记录玩家点击的次数。当计数器为3时触发

int amountOfClicks = 0;
void clickme(){
   amountOfClicks++;
   if(amountOfClicks == 3){
      amountOfClicks = 0; 
      YourFunction();     
   }
}

void YourFunction(){
   // do something...

}

1.保留一个静态/全局变量


2.您无法停止事件通知,因此,请在事件处理程序代码中的代码块上添加条件(如果条件),您应该使用简单计数器

我还更改并注释了代码中的其他“问题”:

public class HintScript : MonoBehaviour 
{
    // by adding SerializeField you can already set those references
    // directly in the Unity Editor. This is better than getting them at runtime.
    [SerializeField] private LineRenderer line;
    [SerializeField] private AdMobManager adMobManager;
    // you can still change the required clicks in the inspector
    // Note: afterwards changing it here will have no effect!
    [SerializeField] private int requiredClicks = 3;    

    // counter for your clicks
    private int counter;

    // Use this for initialization
    void Start () 
    {
        // you should do this only once
        line = GetComponent<LineRenderer>();

        // you should also this only do once
        adMobManager = FindObjectOfType<AdMobManager>();

        // If instead you would drag those references directly into the now
        // serialized fields you wouldn't have to get them on runtime at all.
    }

    // Update is called once per frame
    void Update () 
    { 
        line.positionCount = transform.childCount;
        var positions = new Vector3[transform.childCount];
        for (var i = 0; i < transform.childCount; i++)
        {
            position[i] = transform.GetChild(i).position;
        }

        // it is more efficient to call this only once
        // see https://docs.unity3d.com/ScriptReference/LineRenderer.SetPositions.html
        line.SetPositions(positions);
    }

    // This is called from onClick of the Button
    public void Hint() 
    { 
        counter++;

        if(counter < requiredClicks) return;

        // Or using your solution should actually also work
        if(counter % requiredClicks != 0) return;

        adMobManager.Hint = true; 
        adMobManager.showInterstitial(); 

        // reset the counter
        counter = 0;
    }
}
公共类HintScript:monobhavior
{
//通过添加序列化字段,您可以设置这些引用
//直接在Unity编辑器中。这比在运行时获取它们要好。
[SerializeField]专用行渲染器行;
[序列化字段]专用AdMobManager AdMobManager;
//您仍然可以在inspector中更改所需的单击
//注意:之后在此处更改它将无效!
[SerializeField]private int requiredClicks=3;
//为您的点击计数
专用int计数器;
//用于初始化
无效开始()
{
//你只应该这样做一次
line=GetComponent();
//你也应该只做一次
adMobManager=FindObjectOfType();
//如果您现在将这些引用直接拖动到
//序列化字段根本不需要在运行时获取它们。
}
//每帧调用一次更新
无效更新()
{ 
line.positionCount=transform.childCount;
var positions=newvector3[transform.childCount];
对于(var i=0;i