我想通过每1秒实例化7个立方体来制作统一2d游戏中的加载栏。
我用了 :
yield WaitForSeconds(1);
在每个实例化语句之后的函数更新中,但它不起作用:((我得到一个错误:
脚本错误:Update()不能是协程。
还有其他想法吗?
我制作了一个新场景并将其命名为“输掉”,然后我编写了这个脚本并将其附加到主摄像头:
#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);
}
我希望这些立方体能够在1秒后相互显示出来 看起来像装货吧......
我通过这种方式解决了这个问题::
#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;
}
答案 0 :(得分:12)
首先,你不能在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语法。
答案 1 :(得分:2)
如果你坚持想要使用更新功能,你可以。以下是如何操作的一个示例:
private float _elapsedTime = 0;
private int counter = 0;
void Update(){
if(counter < 7){
if(_elapsedTime >= 1){
_elapsedTime = 0; //reset it zero again
_counter++;
//instantiate the cube, and update the loading bar here
}else{
_elapsedTime += Time.deltaTime;
}
}
}
答案 2 :(得分:1)
您无法更改现有方法的返回类型。相反,您需要在Start或Awake方法中触发StartCoroutine,并将IEnumerator定义为MonoBehaviour的单独私有函数。