Unity - 我怎样才能让一个函数只在玩家点击它 3 次时触发?

Unity - How can i make a function fire only when player clicked on it 3 times?

我的游戏中有一个提示按钮,但每次单击它时它都会启动。我需要它每点击 3 次才触发一次

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

if (loadCount % 3 == 0) { }

但它似乎不起作用

图片供参考:https://ibb.co/L9LnNDw

这是脚本:

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

您可以有一个计数器来跟踪玩家点击了多少次。然后在该计数器为 3 时触发。

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

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

}

1.Keep 一个 static/global 变量。

2.You 无法停止事件通知,请在事件处理程序代码[=]中的代码块上添加条件(if 条件) 17=].

你应该使用一个简单的计数器。

我还更改并评论了您代码中的其他 "issues":

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