Uri*_*pov 2 c# unity-game-engine
嗨,我正在尝试统一创建一个协程,以处理各种物体上的褪色。到目前为止,我已经能够获得所需的不同类型的Alpha值,但是在使用新颜色后如何设置它还处于死胡同。这是我的代码:
public static IEnumerator FadeTo(float aValue, float aTime,GameObject gameObj)
{
Component[] allComponents = gameObj.GetComponents(typeof(Component));
Component fadableComponent;
Type type = null;
float alpha=0;
foreach(Component c in allComponents)
{
if(c.GetType()==typeof(Image))
{
fadableComponent = c;
alpha = fadableComponent.GetComponent<Image>().color.a;
type = fadableComponent.GetType();
Debug.Log("Found a image");
break;
}
if (c.GetType() == typeof(Renderer))
{
fadableComponent = c;
alpha = fadableComponent.GetComponent<Renderer>().material.color.a;
type = fadableComponent.GetType();
break;
}
if (c.GetType() == typeof(SpriteRenderer))
{
fadableComponent = c;
alpha = fadableComponent.GetComponent<SpriteRenderer>().color.a;
type = fadableComponent.GetType();
break;
}
if(c.GetType()== typeof(CanvasGroup))
{
fadableComponent = c;
alpha = fadableComponent.GetComponent<CanvasGroup>().alpha;
type = fadableComponent.GetType();
}
Debug.Log(alpha.ToString());
}
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
{
Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha, aValue, t));
gameObj.transform.GetComponent(type) // What now ?!
yield return null;
}
yield return null;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我知道如何使用另一组if或not进行操作,但是我想避免这种情况。以我想避免的解决方案为例,我介绍了iTween实现。这是我要避免的完美示例:
if(target.GetComponent<GUITexture>()){
target.GetComponent<GUITexture>().color=colors[3];
}else if(target.GetComponent<GUIText>()){
target.GetComponent<GUIText>().material.color=colors[3];
}else if(target.GetComponent<Renderer>()){
target.GetComponent<Renderer>().material.color=colors[3];
}else if(target.GetComponent<Light>()){
target.GetComponent<Light>().color=colors[3];
}
Run Code Online (Sandbox Code Playgroud)
妈妈,这是一团糟。延长这将是一场噩梦。
可能是您在寻找
游戏编程的可卡因!
基本语法是
time.Tweeng( do something )
Run Code Online (Sandbox Code Playgroud)
所以(用伪代码)
StartCoroutine( 2f.Tweeng( .. move the ball .. ) );
StartCoroutine( .5f.Tweeng( .. spin the spaceship .. ) );
StartCoroutine( 1f.Tweeng( .. fade the text .. ) );
Run Code Online (Sandbox Code Playgroud)
这些例子是
更多...
// tweeng z to 20 degrees in .12 seconds
StartCoroutine(.12f.Tweeng( (t)=>transform.Eulers(0f,0f,t), 0f,20f) );
// fade in alpha in .75 seconds
StartCoroutine(.75f.Tweeng( (u)=>{c.a=u;s.color=c;}, 0f,1f) );
// duck volume..
StartCoroutine( .3f.Tweeng( x=>{current.volume=x;}, current.volume, 0f) );
// move something..
StartCoroutine(secs.Tweeng( (p)=>parked.transform.position=p,
parked.transform.position,
landing.transform.position) );
// twist image
yield return StartCoroutine( .3f.Tweeng(
(u)=>polaroid.localEulerAngles = new Vector3(0f,0f,u),
0f,9f) );
Run Code Online (Sandbox Code Playgroud)
您可以补间任何东西,当然包括淡入淡出。
Tweeng的基本代码库:
只需将其放在文件Extns.cs中:
public static class Extns
{
public static IEnumerator Tweeng( this float duration,
System.Action<float> var, float aa, float zz )
{
float sT = Time.time;
float eT = sT + duration;
while (Time.time < eT)
{
float t = (Time.time-sT)/duration;
var( Mathf.SmoothStep(aa,zz, t) );
yield return null;
}
var(zz);
}
public static IEnumerator Tweeng( this float duration,
System.Action<Vector3> var, Vector3 aa, Vector3 zz )
{
float sT = Time.time;
float eT = sT + duration;
while (Time.time < eT)
{
float t = (Time.time-sT)/duration;
var( Vector3.Lerp(aa,zz, t) );
yield return null;
}
var(zz);
}
}
Run Code Online (Sandbox Code Playgroud)
(如果您有兴趣,可以使用泛型方法- 对此进行质量检查)
Tweeng-它可以挽救您的生命!