我在制作脚本来淡化 in/out Unity c# 中的精灵列表时遇到问题
I am having trouble making a script to fade in/out a list of sprites in Unity c#
我有几个 sprite,我想在游戏的演职员表场景中循环使用淡入淡出 in/out 效果。我有一个有效的脚本,但它只适用于一个精灵。我怎样才能做到这一点,以便我可以有一个精灵列表来循环?
using UnityEngine;
using System.Collections;
public class possible : MonoBehaviour
{
public SpriteRenderer sprite;
public Color spriteColor = Color.white;
public float fadeInTime = 1.5f;
public float fadeOutTime = 3f;
public float delayToFadeOut = 5f;
public float delayToFadeIn = 5f;
void Start()
{
StartCoroutine("FadeCycle");
}
IEnumerator FadeCycle()
{
float fade = 0f;
float startTime;
while (true)
{
startTime = Time.time;
while (fade < 1f)
{
fade = Mathf.Lerp(0f, 1f, (Time.time - startTime) /
fadeInTime);
spriteColor.a = fade;
sprite.color = spriteColor;
yield return null;
}
//Make sure it's set to exactly 1f
fade = 1f;
spriteColor.a = fade;
sprite.color = spriteColor;
yield return new WaitForSeconds(delayToFadeOut);
startTime = Time.time;
while (fade > 0f)
{
fade = Mathf.Lerp(1f, 0f, (Time.time - startTime) /
fadeOutTime);
spriteColor.a = fade;
sprite.color = spriteColor;
yield return null;
}
fade = 0f;
spriteColor.a = fade;
sprite.color = spriteColor;
yield return new WaitForSeconds(delayToFadeIn);
}
}
}
我不确定您要做什么,但很快您就可以添加一个黑色 sprite/canvas 图像来覆盖所有场景,其中 alpha = 0 并使用您的类似方法将 alpha 更改为 1。它应该比循环每个精灵有更好的性能。
如果您想单独控制每个精灵:将 SpriteRenderer 参数添加到您的方法并将所有精灵存储在一个列表中,然后为 spriteList 中的每个精灵调用您的方法。为了更好地练习,您可以将扩展方法添加到 SpriteRenderer
首先,让我们做一个简单的重构,取出执行实际工作的部分,并将其分离到一个方法中。所以这两行:
spriteColor.a = fade;
sprite.color = spriteColor;
可以变成一个方法,在你的代码中调用
void SetFade(float fade)
{
spriteColor.a = fade;
sprite.color = spriteColor;
}
然后你的代码的其余部分会变得更短并且更易读:
IEnumerator FadeCycle()
{
float startTime;
while (true)
{
startTime = Time.time;
while (fade < 1f)
{
fade = Mathf.Lerp(0f, 1f, (Time.time - startTime) / fadeInTime);
SetFade(fade);
yield return null;
}
SetFade(1);
yield return new WaitForSeconds(delayToFadeOut);
startTime = Time.time;
while (fade > 0f)
{
SetFade(Mathf.Lerp(1f, 0f, (Time.time - startTime) / fadeOutTime));
yield return null;
}
SetFade(0);
yield return new WaitForSeconds(delayToFadeIn);
}
}
}
现在,如果您想将更改应用到多个精灵,您只需在一个地方执行 int。更改您的声明:
public SpriteRenderer sprite;
到
public SpriteRenderer[] sprites;
最后我们可以将 SetFade 方法修改为:
void SetFade(float fade)
{
spriteColor.a = fade;
foreach(var sprite in sprites)
sprite.color = spriteColor;
}
我有几个 sprite,我想在游戏的演职员表场景中循环使用淡入淡出 in/out 效果。我有一个有效的脚本,但它只适用于一个精灵。我怎样才能做到这一点,以便我可以有一个精灵列表来循环?
using UnityEngine;
using System.Collections;
public class possible : MonoBehaviour
{
public SpriteRenderer sprite;
public Color spriteColor = Color.white;
public float fadeInTime = 1.5f;
public float fadeOutTime = 3f;
public float delayToFadeOut = 5f;
public float delayToFadeIn = 5f;
void Start()
{
StartCoroutine("FadeCycle");
}
IEnumerator FadeCycle()
{
float fade = 0f;
float startTime;
while (true)
{
startTime = Time.time;
while (fade < 1f)
{
fade = Mathf.Lerp(0f, 1f, (Time.time - startTime) /
fadeInTime);
spriteColor.a = fade;
sprite.color = spriteColor;
yield return null;
}
//Make sure it's set to exactly 1f
fade = 1f;
spriteColor.a = fade;
sprite.color = spriteColor;
yield return new WaitForSeconds(delayToFadeOut);
startTime = Time.time;
while (fade > 0f)
{
fade = Mathf.Lerp(1f, 0f, (Time.time - startTime) /
fadeOutTime);
spriteColor.a = fade;
sprite.color = spriteColor;
yield return null;
}
fade = 0f;
spriteColor.a = fade;
sprite.color = spriteColor;
yield return new WaitForSeconds(delayToFadeIn);
}
}
}
我不确定您要做什么,但很快您就可以添加一个黑色 sprite/canvas 图像来覆盖所有场景,其中 alpha = 0 并使用您的类似方法将 alpha 更改为 1。它应该比循环每个精灵有更好的性能。
如果您想单独控制每个精灵:将 SpriteRenderer 参数添加到您的方法并将所有精灵存储在一个列表中,然后为 spriteList 中的每个精灵调用您的方法。为了更好地练习,您可以将扩展方法添加到 SpriteRenderer
首先,让我们做一个简单的重构,取出执行实际工作的部分,并将其分离到一个方法中。所以这两行:
spriteColor.a = fade;
sprite.color = spriteColor;
可以变成一个方法,在你的代码中调用
void SetFade(float fade)
{
spriteColor.a = fade;
sprite.color = spriteColor;
}
然后你的代码的其余部分会变得更短并且更易读:
IEnumerator FadeCycle()
{
float startTime;
while (true)
{
startTime = Time.time;
while (fade < 1f)
{
fade = Mathf.Lerp(0f, 1f, (Time.time - startTime) / fadeInTime);
SetFade(fade);
yield return null;
}
SetFade(1);
yield return new WaitForSeconds(delayToFadeOut);
startTime = Time.time;
while (fade > 0f)
{
SetFade(Mathf.Lerp(1f, 0f, (Time.time - startTime) / fadeOutTime));
yield return null;
}
SetFade(0);
yield return new WaitForSeconds(delayToFadeIn);
}
}
}
现在,如果您想将更改应用到多个精灵,您只需在一个地方执行 int。更改您的声明:
public SpriteRenderer sprite;
到
public SpriteRenderer[] sprites;
最后我们可以将 SetFade 方法修改为:
void SetFade(float fade)
{
spriteColor.a = fade;
foreach(var sprite in sprites)
sprite.color = spriteColor;
}