Unity3d延迟,而真正的循环不起作用
Unity3d delay in while true loop not working
我仍在开发游戏,我 运行 遇到了另一个问题,我正在尝试制作一个无限循环,每次执行都会等待几秒钟,我目前有:
StartScript.cs
using UnityEngine;
using System.Collections;
using ProgressBar;
public class StartScript : MonoBehaviour {
private ProgressBarBehaviour hunger;
private ProgressBarBehaviour stamina;
private ProgressRadialBehaviour health;
private ProgressBarBehaviour thirst;
public void OnGUI(){
this.hunger = GameObject.Find("hungerBar").GetComponent<ProgressBarBehaviour>();
this.stamina = GameObject.Find("staminaBar").GetComponent<ProgressBarBehaviour>();
this.health = GameObject.Find("healthBar").GetComponent<ProgressRadialBehaviour>();
this.thirst = GameObject.Find("thirstBar").GetComponent<ProgressBarBehaviour>();
this.health.Value = 100f;
this.stamina.Value = 100f;
StartCoroutine ("runHunger");
}
bool addBar(ProgressBarBehaviour target, float percentage){
Debug.Log (percentage);
if ((target.Value + percentage) <= 100f) {
target.IncrementValue (percentage);
return true;
}
return false;
}
bool damageBar(ProgressBarBehaviour target, float percentage){
if ((target.Value - percentage) >= 0f) {
target.DecrementValue (percentage);
return true;
}
return false;
}
bool addRadial(ProgressRadialBehaviour target, float percentage){
if ((target.Value + percentage) <= 100f) {
target.IncrementValue (percentage);
return true;
}
return false;
}
bool damageRadial(ProgressRadialBehaviour target, float percentage){
if ((target.Value - percentage) >= 0f) {
target.DecrementValue (percentage);
return true;
}
return false;
}
IEnumerator runHunger(){
while (true) {
yield return new WaitForSeconds(10f);
/*if (!this.addBar(this.hunger,5f)) {
this.damageRadial(this.health,3f);
}*/
Debug.Log("Time: "+Time.time);
}
}
IEnumerator runHealth(){
while (true) {
}
}
/*
IEnumerator runThirst(){
while (true) {
if (!this.thirst.Add (2)) {
this.health.Damage(8);
}
}
}*/
}
正如你们所看到的,我正在尝试用 yield return new WaitForSeconds()
制作一个 while(true){}
循环,想法是它每隔(在这个测试用例中)10 在循环中运行函数秒。
第一次执行很顺利,等待 10 秒,但之后只等待 0.1 秒,然后再次执行。
希望有人能帮我解决这个问题。
我目前在您的代码中看到的错误太多了。
这个:
IEnumerator runHealth(){
while (true) {
}
}
改为
IEnumerator runHealth(){
while (true) {
yield return null;
}
}
还有这个:
IEnumerator runThirst(){
while (true) {
if (!this.thirst.Add (2)) {
this.health.Damage(8);
}
}
}
改成
IEnumerator runThirst(){
while (true) {
if (!this.thirst.Add (2)) {
this.health.Damage(8);
}
yield return null;
}
}
如果你要在协程中有 while(true)
,你必须有 yield return 东西,否则它会 lock 你的程序。 先解决这些问题。最好将它放在 while 循环的右括号之前。
我相信你也在 other scripts 中做过。
Go 越过其他 scripts 的 rest 并做同样的事情。把yield return null
里面每个while循环放在每个协程函数里
另一个大错误:
删除 OnGUI 函数 中的所有代码并将其放入Start 函数中,如下所示。代码每帧被调用太多次,这将导致 变慢 下降,因为您将 creating 很多很多 coroutines never stops due to your while loop 在协程中 function.Putting 它在 Start 函数中将调用它 once.
void Start()
{
this.hunger = GameObject.Find("hungerBar").GetComponent<ProgressBarBehaviour>();
this.stamina = GameObject.Find("staminaBar").GetComponent<ProgressBarBehaviour>();
this.health = GameObject.Find("healthBar").GetComponent<ProgressRadialBehaviour>();
this.thirst = GameObject.Find("thirstBar").GetComponent<ProgressBarBehaviour>();
this.health.Value = 100f;
this.stamina.Value = 100f;
StartCoroutine ("runHunger");
}
调用 StartCoroutine ("runHunger");
在 OnGUI() 之外的某个地方。它将起作用。因为每次调用 OnGUI 都会启动一个新的协程
在 Start() 或其他地方调用 StartCoroutine
我仍在开发游戏,我 运行 遇到了另一个问题,我正在尝试制作一个无限循环,每次执行都会等待几秒钟,我目前有:
StartScript.cs
using UnityEngine;
using System.Collections;
using ProgressBar;
public class StartScript : MonoBehaviour {
private ProgressBarBehaviour hunger;
private ProgressBarBehaviour stamina;
private ProgressRadialBehaviour health;
private ProgressBarBehaviour thirst;
public void OnGUI(){
this.hunger = GameObject.Find("hungerBar").GetComponent<ProgressBarBehaviour>();
this.stamina = GameObject.Find("staminaBar").GetComponent<ProgressBarBehaviour>();
this.health = GameObject.Find("healthBar").GetComponent<ProgressRadialBehaviour>();
this.thirst = GameObject.Find("thirstBar").GetComponent<ProgressBarBehaviour>();
this.health.Value = 100f;
this.stamina.Value = 100f;
StartCoroutine ("runHunger");
}
bool addBar(ProgressBarBehaviour target, float percentage){
Debug.Log (percentage);
if ((target.Value + percentage) <= 100f) {
target.IncrementValue (percentage);
return true;
}
return false;
}
bool damageBar(ProgressBarBehaviour target, float percentage){
if ((target.Value - percentage) >= 0f) {
target.DecrementValue (percentage);
return true;
}
return false;
}
bool addRadial(ProgressRadialBehaviour target, float percentage){
if ((target.Value + percentage) <= 100f) {
target.IncrementValue (percentage);
return true;
}
return false;
}
bool damageRadial(ProgressRadialBehaviour target, float percentage){
if ((target.Value - percentage) >= 0f) {
target.DecrementValue (percentage);
return true;
}
return false;
}
IEnumerator runHunger(){
while (true) {
yield return new WaitForSeconds(10f);
/*if (!this.addBar(this.hunger,5f)) {
this.damageRadial(this.health,3f);
}*/
Debug.Log("Time: "+Time.time);
}
}
IEnumerator runHealth(){
while (true) {
}
}
/*
IEnumerator runThirst(){
while (true) {
if (!this.thirst.Add (2)) {
this.health.Damage(8);
}
}
}*/
}
正如你们所看到的,我正在尝试用 yield return new WaitForSeconds()
制作一个 while(true){}
循环,想法是它每隔(在这个测试用例中)10 在循环中运行函数秒。
第一次执行很顺利,等待 10 秒,但之后只等待 0.1 秒,然后再次执行。
希望有人能帮我解决这个问题。
我目前在您的代码中看到的错误太多了。
这个:
IEnumerator runHealth(){
while (true) {
}
}
改为
IEnumerator runHealth(){
while (true) {
yield return null;
}
}
还有这个:
IEnumerator runThirst(){
while (true) {
if (!this.thirst.Add (2)) {
this.health.Damage(8);
}
}
}
改成
IEnumerator runThirst(){
while (true) {
if (!this.thirst.Add (2)) {
this.health.Damage(8);
}
yield return null;
}
}
如果你要在协程中有 while(true)
,你必须有 yield return 东西,否则它会 lock 你的程序。 先解决这些问题。最好将它放在 while 循环的右括号之前。
我相信你也在 other scripts 中做过。
Go 越过其他 scripts 的 rest 并做同样的事情。把yield return null
里面每个while循环放在每个协程函数里
另一个大错误: 删除 OnGUI 函数 中的所有代码并将其放入Start 函数中,如下所示。代码每帧被调用太多次,这将导致 变慢 下降,因为您将 creating 很多很多 coroutines never stops due to your while loop 在协程中 function.Putting 它在 Start 函数中将调用它 once.
void Start()
{
this.hunger = GameObject.Find("hungerBar").GetComponent<ProgressBarBehaviour>();
this.stamina = GameObject.Find("staminaBar").GetComponent<ProgressBarBehaviour>();
this.health = GameObject.Find("healthBar").GetComponent<ProgressRadialBehaviour>();
this.thirst = GameObject.Find("thirstBar").GetComponent<ProgressBarBehaviour>();
this.health.Value = 100f;
this.stamina.Value = 100f;
StartCoroutine ("runHunger");
}
调用 StartCoroutine ("runHunger");
在 OnGUI() 之外的某个地方。它将起作用。因为每次调用 OnGUI 都会启动一个新的协程
在 Start() 或其他地方调用 StartCoroutine