c# unity将迭代数转换为基于点击次数的范围数
本文关键字:于点击 范围 转换 unity 迭代 | 更新日期: 2023-09-27 18:11:54
我试图得到一个介于0和3之间的数字。我试图通过计数器迭代,每5次点击一个按钮,它调用一个方法,但我似乎无法弄清楚。我试过各种方法来做这件事。如果我能得到一些提示如何完成这一点,请让我知道。如果你需要任何其他信息,也让我知道。谢谢!
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Threading;
public class ButtonClick : MonoBehaviour {
private Vector3 starPos;
private int starCounter;
private int[] starTypes;
private int totalStarTypes = 4;
public Button button;
public UnityEngine.UI.Text starCounterText;
private Image starImage;
// Use this for initialization
void Start () {
starTypes = new int[totalStarTypes];
}
void Update(){
if (Input.GetMouseButtonDown (0)) {
starCounter++;
}
for (int i = 0; i < starCounter; i++) {
int j = i;
int type = (j % 5);
if (type == 0) {
//SpawnStar (j%5);
}
}
}
// Update is called once per frame
public void UpdateStar () {
starCounterText.text = "Star Counter: " + starCounter;
}
public void SpawnStar(int type){
if (type == 0) {
Debug.Log ("White Star Spawned!");
}
if (type == 1) {
Debug.Log ("Red Star Spawned!");
}
if (type == 2) {
Debug.Log ("Yellow Star Spawned!");
}
if (type == 3) {
Debug.Log ("Blue Star Spawned!");
}
}
}
Random r = new Random();
void Update()
{
if (Input.GetMouseButtonDown(0))
{
starCounter = (starCounter + 1) % 5;
if (starCounter == 0) SpawnStar(r.Next(0, 4));
}
}
你的代码将在第5次点击后每帧生成所有星星。
试试这个,它会产生一个单一的星星,当你点击,只有一个你想要产生。
- int
starCounter
将处理生成哪个星 - bool
executeSpawn
将处理何时生成星形
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Threading;
public class ButtonClick : MonoBehaviour {
private Vector3 starPos;
private int starCounter;
private bool executeSpawn;
private int[] starTypes;
private int totalStarTypes = 4;
public Button button;
public UnityEngine.UI.Text starCounterText;
private Image starImage;
// Use this for initialization
void Start () {
starTypes = new int[totalStarTypes];
}
void Update(){
if (Input.GetMouseButtonDown (0)) {
starCounter++;
executeSpawn = true;
}
if(executeSpawn) {
SpawnStar (i % 5);
executeSpawn = false;
}
}
// Update is called once per frame
public void UpdateStar () {
starCounterText.text = "Star Counter: " + starCounter;
}
public void SpawnStar(int type){
if (type == 0) {
Debug.Log ("White Star Spawned!");
}
if (type == 1) {
Debug.Log ("Red Star Spawned!");
}
if (type == 2) {
Debug.Log ("Yellow Star Spawned!");
}
if (type == 3) {
Debug.Log ("Blue Star Spawned!");
}
}
}