gpt4 book ai didi

unity3d - 如何统一等待特定秒数

转载 作者:行者123 更新时间:2023-12-01 05:36:11 25 4
gpt4 key购买 nike

我想通过每 1 秒实例化 7 个立方体来在 unity 2d 游戏中制作一个加载条。我用了 : yield WaitForSeconds(1);在每个实例化语句之后的函数更新中,但它不起作用:((我得到一个错误:

Script error : Update() can not be a coroutine.

还有其他想法吗?

我制作了一个新场景并将其命名为“丢失”然后我编写了这个脚本并将其附加到主摄像头:

#pragma strict

//var loadingBar: Transform;
var loading_bar : GameObject;

function Update()
{
Instantiate(loadingBar,Vector3(-1.849,-2.9371,2),Quaternion.identity);

gameTimer();


Instantiate(loadingBar,Vector3(-1.2909,-2.937,2),Quaternion.identity);

gameTimer();

Instantiate(loadingBar,Vector3(-0.5566,-2.93711,2),Quaternion.identity);

gameTimer();

Instantiate(loadingBar,Vector3(0.148236,-2.93711,2),Quaternion.identity);

gameTimer();

Instantiate(loadingBar,Vector3(0.823772,-2.93711,2),Quaternion.identity);

gameTimer();

Instantiate(loadingBar,Vector3(1.440567,-2.93711,2),Quaternion.identity);

gameTimer();

Instantiate(loadingBar,Vector3(2.057361,-2.93711,2),Quaternion.identity);

loadingTimer();

Application.LoadLevel(1);
}


function OnGUI()
{
GUI.color = Color.green;
GUI.Label(Rect(400,350,500,500),"<color=green><size=100>Lose</size></color>");
}

function loadingTimer()
{
yield WaitForSeconds(1);
}

enter image description here

I want to these cubes to appear after each other by 1 second so it will seem like a loading bar ...

我是这样解决这个问题的::

#pragma strict

var loadingBar: Transform;
var finished : boolean = false;

function Update()
{
loadingTimer();

if (finished == true)
{
Application.LoadLevel(1);
finished= false;
}
}


function OnGUI()
{
GUI.color = Color.green;
GUI.Label(Rect(295,320,500,500),"<color=green><size=100>Lose</size></color>");

}


function loadingTimer()
{
Instantiate(loadingBar,Vector3(-1.9,-2.9371,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(-1.3,-2.937,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(-1.3,-2.937,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(-0.7,-2.93711,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(-0.1,-2.93711,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(0.5,-2.93711,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(1.1,-2.93711,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(1.7,-2.93711,2),Quaternion.identity);

finished= true;
}

最佳答案

首先,您不能在 Update 函数中使用 yield WaitForSeconds。您需要导入 IEnumator。对于您的情况,我可以说以下代码可能对您有所帮助。

public class Loader : MonoBehaviour 
{
public GameObject cube;
private bool finished = false;
private Vector3[] positions = new Vector3[7] {new Vector3(-1.849,-2.9371,2), new Vector3(-1.2909,-2.937,2), new Vector3(-0.5566,-2.93711,2),new Vector3(0.148236,-2.93711,2),new Vector3(0.823772,-2.93711,2),new Vector3(1.440567,-2.93711,2),new Vector3(2.057361,-2.93711,2)};
private int loaderCounter=0;

void Start ()
{
StartCoroutine(StartLoader());
}

IEnumerator StartLoader ()
{
Instantiate(cube,positions[loaderCounter],Quaternion.identity);
yield return new WaitForSeconds(1);
loaderCounter++;
if(loaderCounter==7)
{
finished=true;
}
if(!finished)
{
StartCoroutine(StartLoader());
}
else
{
Application.LoadLevel(1);
}
}
}

此后如果有任何问题,请告诉我。只需使用变量声明的 javascript 语法。

关于unity3d - 如何统一等待特定秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19157051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com