我有一个动画,我正在使用发射材料,我试图使用FOR循环打开和关闭该发射。更具体一点......
我有这个功能,我用来设置发射:
void TurnOnLight(bool on)
{
Renderer renderer = GetComponent<Renderer>();
Material mat = renderer.material;
Color baseColor = Color.yellow;
float emission;
Color finalColor;
if (on)
{
emission = 1;
finalColor = baseColor * Mathf.LinearToGammaSpace(emission);
}
else
{
emission = 0;
finalColor = baseColor * Mathf.LinearToGammaSpace(emission);
}
mat.SetColor("_EmissionColor", finalColor);
}
这是一个函数,我把它设置为开启和关闭延迟(但我不知道如何设置延迟):
void blink(void){
TurnOnLight(true);
//something to make a delay(wait) of 1 second
TurnOnLight(false);
//something to make a delay(wait) of 1 second
}
还有一个FOR循环,我正在尝试制作和开灯效果:
for(int i=0;i<50;i++){
blink();
}
现在我有2个问题......
答案 0 :(得分:2)
您应该使用以下协同程序:
public IEnumerator blink( int count, float onDuration, float offDuration )
{
WaitForSeconds onWait = new WaitForSeconds( onDuration ) ;
WaitForSeconds offWait = new WaitForSeconds( offDuration ) ;
for( int i = 0 ; i < count ; ++i )
{
TurnOnLight(true);
yield return onWait ;
TurnOnLight(false);
yield return offWait ;
}
// If you want your light to be turned on in the end
TurnOnLight(true);
}
然后,打电话给:
StartCoroutine( blink( 50, 1, 1 ) ) ;