Unity-仅当播放器单击3次后,我才能如何触发该功能?

时间:2019-05-27 09:59:42

标签: c# unity3d 2d-games

我的游戏上有一个提示按钮,但是每次单击它都会触发。我需要它每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 个答案:

答案 0 :(得分:1)

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

我还更改了您的代码并评论了其他“问题”:

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

答案 1 :(得分:0)

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

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

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

}

答案 2 :(得分:0)

1。保留 static / global 变量。

2。您无法停止事件通知,因此,请在事件处理程序代码内的代码段内添加条件(如果条件)。