c# - c#-使Int每秒增加x值


 public bool MakingMuffins = false;


 public static int MuffinIncrease = 1;


 public int InternalIncrease;



 void Update () {


 InternalIncrease = MuffinIncrease;


 if (MakingMuffins == false)


 {


 MakingMuffins = true;


 StartCoroutine(MakeTheMuffin());


 }


 }



 IEnumerator MakeTheMuffin ()


 {


 GlobalMuffins.MuffinCount += InternalIncrease;


 yield return new WaitForSeconds(1);


 MakingMuffins = false;



 }


}





public void StartAutoMuffin()


 {


 if (InternalPlayerMuffins >=bakerycost){


 playSound.Play();


 StartBakery.SetActive(true);


 InternalPlayerMuffins -= bakerycost;


 bakerycost *= 2;


 turnOffButton = true;


 themps += 1;


 thelevel += 1;


 }else{


 Debug.Log("Cant do anything");


 }


 }



下面是我的主脚本中的更多代码,来帮助理解。


using System;


using System.Collections;


using System.Collections.Generic;


using UnityEngine;


using UnityEngine.UI;



public class GlobalGameMechanics : MonoBehaviour


{


 public static bool turnOffButton = false;


 public AudioSource muffinSound;


 public AudioSource playSound;


 public static int GlobalPlayerDiamonds;


 public int InternalPlayerDiamonds = 1200;


 public GameObject PlayerDiamondDisplay;


 public GameObject TotalMPS;


 public GameObject Cost;


 public GameObject Level;


 public GameObject MPS;


 public GameObject FakeCost;


 public GameObject FakeLevel;


 public GameObject FakeMPS;


 public int InternalPlayerMuffins = 15;


 public int bakerycost = 25;


 public int thelevel = 0;


 public int themps = 0;


 public int currentMuffins;


 public GameObject ShopPanel;


 public GameObject fakeButton;


 public GameObject realButton;


 public GameObject StartBakery;



 void Start()


 {



 }



 void Update()


 {



 GlobalPlayerDiamonds = InternalPlayerDiamonds;


 PlayerDiamondDisplay.GetComponent<Text>().text ="" + InternalPlayerDiamonds;


 TotalMPS.GetComponent<Text>().text ="/Sec" + themps;



 GlobalMuffins.MuffinCount = InternalPlayerMuffins;


 Cost.GetComponent<Text>().text ="" + bakerycost;


 Level.GetComponent<Text>().text ="Level" + thelevel;


 MPS.GetComponent<Text>().text ="MPS" + themps;



 FakeCost.GetComponent<Text>().text ="" + bakerycost;


 FakeLevel.GetComponent<Text>().text ="Level" + thelevel;


 FakeMPS.GetComponent<Text>().text ="MPS" + themps;



 currentMuffins = InternalPlayerMuffins;


 if (currentMuffins >= bakerycost)


 {


 fakeButton.SetActive(false);


 realButton.SetActive(true);


 }


 if (turnOffButton == true)


 {


 realButton.SetActive(false);


 fakeButton.SetActive(true);


 turnOffButton = false;


 }



 }



到目前为止,我已经创建了1个按钮,希望每秒将“ InternalPlayerMuffins ”增加1。

时间:

调用方法invokerRepeating,例如


void Start()


{


 InvokeRepeating("CreateNewMuffin", 0f, 1f);


}



...