C# 我在用Unity c制作一个淡入/淡出精灵列表的脚本时遇到了麻烦#
我有两个精灵,我想在我的游戏场景中循环使用淡入淡出效果。我有一个脚本,但它只适用于一个精灵。我如何制作它,以便我可以有一个列表的精灵循环C# 我在用Unity c制作一个淡入/淡出精灵列表的脚本时遇到了麻烦#,c#,unity3d,sprite,C#,Unity3d,Sprite,我有两个精灵,我想在我的游戏场景中循环使用淡入淡出效果。我有一个脚本,但它只适用于一个精灵。我如何制作它,以便我可以有一个列表的精灵循环 using UnityEngine; using System.Collections; public class possible : MonoBehaviour { public SpriteRenderer sprite; public Color spriteColor = Color.white; public float fadeInTime
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);
}
}
}
使用UnityEngine;
使用系统集合;
公共课堂可能:单一行为
{
公共雪碧;
公共颜色spriteColor=Color.white;
公共浮动fadeInTime=1.5f;
公共浮点数衰减时间=3f;
公共浮点数延时延时=5f;
公共浮动延迟时间=5f;
void Start()
{
开始例行程序(“FadeCycle”);
}
IEnumerator FadeCycle()
{
浮动衰减=0f;
浮动起始时间;
while(true)
{
startTime=Time.Time;
同时(衰减<1f)
{
fade=Mathf.Lerp(0f,1f,(Time.Time-startTime)/
发散时间);
spriteColor.a=褪色;
sprite.color=spriteColor;
收益返回空;
}
//确保设置为1f
衰减=1f;
spriteColor.a=褪色;
sprite.color=spriteColor;
返回新的WaitForSeconds(delayToFadeOut);
startTime=Time.Time;
同时(淡入度>0f)
{
fade=Mathf.Lerp(1f,0f,(Time.Time-startTime)/
衰减时间);
spriteColor.a=褪色;
sprite.color=spriteColor;
收益返回空;
}
衰减=0f;
spriteColor.a=褪色;
sprite.color=spriteColor;
产生返回新WaitForSeconds(delayToFadeIn);
}
}
}
我不太清楚您想做什么,但很快您就可以添加一个黑色精灵/画布图像,覆盖所有场景,使用alpha=0,并使用类似的方法将alpha更改为1。它应该比循环每个精灵有更好的性能
如果希望对每个精灵进行单独控制:将SpriteRenderer参数添加到方法中,将所有精灵存储在列表中,并在spriteList中为每个精灵调用方法。为了更好地实践,您可以首先向添加一个扩展方法,让我们进行一个简单的重构,并将实际工作的部分分离为一个方法。所以这两条线:
spriteColor.a = fade;
sprite.color = spriteColor;
可以转换为方法,并在您的O代码中调用
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);
}
}
}
到
最后,我们可以将SetFade方法修改为:
void SetFade(float fade)
{
spriteColor.a = fade;
foreach(var sprite in sprites)
sprite.color = spriteColor;
}
获取一个列表,获取该代码并将所有特定的精灵替换为列表中的一个foreach。我尝试过,但它会给我带来错误。然后,正常的Ettiquette会发布您尝试过的代码,请求帮助并解释它的错误。获取错误CS0103:当前上下文中不存在名称“fade”修复了它,但是这是一次完成的。你没有提到你想要它们,不是一次完成的。我很抱歉。我将在以后的评论中对此进行研究。
public SpriteRenderer[] sprites;
void SetFade(float fade)
{
spriteColor.a = fade;
foreach(var sprite in sprites)
sprite.color = spriteColor;
}