每秒激活和停用 GameObject
Activate and deactivate GameObject per seconds
我想制作一个物体 - 龙头,它会在 5 秒内和每 3 秒内爆发一次火焰。但我不确定该怎么做...我的脚本实际上是这样的:
using UnityEngine;
using System.Collections;
public class Dragon_Head_statue: MonoBehaviour {
public GameObject openFire;
private bool IsActive;
void Update()
{
Invoke("OpenFire", 4);
}
void OpenFire()
{
IsActive = !IsActive;
openFire.SetActive(IsActive);
}
}
所以现在它的工作方式就像它开始燃烧,然后循环激活和停用...所以它不会起作用。我还尝试了其他东西,比如协程,InvokeRepeating 没有成功。
协程配合new WaitForSeconds(3.0f)
完美解决这个问题。看看这段代码:
using UnityEngine;
using System.Collections;
public class Dragon_Head_statue: MonoBehaviour {
public GameObject openFire;
public bool IsActive = false; //IsActive means here that we're in a loop bursting flames, not that we're currently not bursting flames.
void Start()
{
//Start the coroutine
//5 seconds flame burst, then wait 3 secs
StartCoroutine(OpenFire(5.0f, 3.0f));
}
/* function for making this thing stop */
void StopFlames()
{
//Stop the first running coroutine with the function name "OpenFire".
StopCoroutine("OpenFire");
IsActive = false; //propage this metadata outside
//Additionally turn of the flames here if we were just
//in the middle of bursting some
openFire.SetActive(false);
}
IEnumerator OpenFire(float fireTime, float waitTime )
{
IsActive = true; //For outside checking if this is spitting fire
while(true)
{
//Activate the flames
openFire.SetActive(true);
//Wait *fireTime* seconds
yield return new WaitForSeconds(fireTime);
//Deactivate the flames
openFire.SetActive(false);
//Now wait the specified time
yield return new WaitForSeconds(waitTime);
//After this, go back to the beginning of the loop
}
}
}
你用StartCoroutine
启动协程,然后你可以再次通过函数名停止协程。您也可以先将 OpenFire
返回的 IEnumerator
保存到局部变量中,如果您不喜欢输入字符串,则对该变量使用 StopCoroutine
。
参考资料:
http://docs.unity3d.com/ScriptReference/MonoBehaviour.StopCoroutine.html
http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
我想制作一个物体 - 龙头,它会在 5 秒内和每 3 秒内爆发一次火焰。但我不确定该怎么做...我的脚本实际上是这样的:
using UnityEngine;
using System.Collections;
public class Dragon_Head_statue: MonoBehaviour {
public GameObject openFire;
private bool IsActive;
void Update()
{
Invoke("OpenFire", 4);
}
void OpenFire()
{
IsActive = !IsActive;
openFire.SetActive(IsActive);
}
}
所以现在它的工作方式就像它开始燃烧,然后循环激活和停用...所以它不会起作用。我还尝试了其他东西,比如协程,InvokeRepeating 没有成功。
协程配合new WaitForSeconds(3.0f)
完美解决这个问题。看看这段代码:
using UnityEngine;
using System.Collections;
public class Dragon_Head_statue: MonoBehaviour {
public GameObject openFire;
public bool IsActive = false; //IsActive means here that we're in a loop bursting flames, not that we're currently not bursting flames.
void Start()
{
//Start the coroutine
//5 seconds flame burst, then wait 3 secs
StartCoroutine(OpenFire(5.0f, 3.0f));
}
/* function for making this thing stop */
void StopFlames()
{
//Stop the first running coroutine with the function name "OpenFire".
StopCoroutine("OpenFire");
IsActive = false; //propage this metadata outside
//Additionally turn of the flames here if we were just
//in the middle of bursting some
openFire.SetActive(false);
}
IEnumerator OpenFire(float fireTime, float waitTime )
{
IsActive = true; //For outside checking if this is spitting fire
while(true)
{
//Activate the flames
openFire.SetActive(true);
//Wait *fireTime* seconds
yield return new WaitForSeconds(fireTime);
//Deactivate the flames
openFire.SetActive(false);
//Now wait the specified time
yield return new WaitForSeconds(waitTime);
//After this, go back to the beginning of the loop
}
}
}
你用StartCoroutine
启动协程,然后你可以再次通过函数名停止协程。您也可以先将 OpenFire
返回的 IEnumerator
保存到局部变量中,如果您不喜欢输入字符串,则对该变量使用 StopCoroutine
。
参考资料: http://docs.unity3d.com/ScriptReference/MonoBehaviour.StopCoroutine.html http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html