我有4张PNG图像的动画。我想让帧在1/2秒的过程中以1-2-3-4-2-1的顺序播放,并具有透明过渡。
我所写的内容应该是在生成持有不同精灵的父对象时立即显示第一帧,然后使它在1/12秒钟内变为透明,而第二帧变为不透明,依此类推,直到最后一帧结束其透明-不透明-透明循环。
这可能不是最有效的方法,但是我制作了一个空对象的预制件,在该对象下放置了6个Sprite框架,每个Sprite都有一个单独的脚本。
我以前三个脚本为例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Frame1 : MonoBehaviour
{
private SpriteRenderer thisSprite;
private Color alpha;
private float timer;
// Start is called before the first frame update
void Start()
{
alpha.a = 255;
thisSprite.GetComponent<SpriteRenderer>().color = alpha;
}
// Update is called once per frame
void Update()
{
timer = timer + Time.deltaTime;
alpha.a -= timer * 3060;
thisSprite.GetComponent<SpriteRenderer>().color = alpha;
if (timer >= 1/12)
{
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Frame2 : MonoBehaviour
{
private SpriteRenderer thisSprite;
private Color alpha;
private float timer;
private int direction;
// Start is called before the first frame update
void Start()
{
alpha.a = 0;
thisSprite.GetComponent<SpriteRenderer>().color = alpha;
}
// Update is called once per frame
void Update()
{
if (direction == 0)
{
timer = timer + Time.deltaTime;
alpha.a += timer * 3060;
thisSprite.GetComponent<SpriteRenderer>().color = alpha;
if (timer >= 1/12)
{
direction = 1;
}
}
if (direction == 1)
{
timer = timer - Time.deltaTime;
alpha.a += timer * 3060;
thisSprite.GetComponent<SpriteRenderer>().color = alpha;
if (timer >= 1/6)
{
Destroy(gameObject);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Frame3 : MonoBehaviour
{
private SpriteRenderer thisSprite;
private Color alpha;
private float timer;
private int direction;
// Start is called before the first frame update
void Start()
{
alpha.a = 0;
thisSprite.GetComponent<SpriteRenderer>().color = alpha;
timer -= 1 / 12;
}
// Update is called once per frame
void Update()
{
if (direction == 0)
{
timer = timer + Time.deltaTime;
alpha.a += timer * 3060;
thisSprite.GetComponent<SpriteRenderer>().color = alpha;
if (timer >= 1 / 12)
{
direction = 1;
}
}
if (direction == 1)
{
timer = timer - Time.deltaTime;
alpha.a += timer * 3060;
thisSprite.GetComponent<SpriteRenderer>().color = alpha;
if (timer >= 1 / 6)
{
Destroy(gameObject);
}
}
}
}
它们在生成的那一刻似乎都是可见的,并且它们不会消失,甚至不会被破坏。有什么问题吗?
谢谢。
答案 0 :(得分:0)
正如评论所建议的那样,使用动画是一种可行的选择。但是,您的代码将无法正常工作,因为alpha接受0到1而不是0到255的值。 因此,只需将逻辑从1降低到0进行调整,您就应该会看到渐变。
答案 1 :(得分:0)
您可以做的一件简单的事情就是创建一个包含所有精灵的动画。首先,您应该做的是Alpha应该是1,然后是第一个Sprite的0。然后第二个精灵应该具有0 alpha,然后从0-1开始。只需将Alpha键保持在动画窗口上即可。 Unity将完成补间动画的其余工作,然后您可以通过脚本播放动画。作为参考,您可以观看Brackeys video about creating transitions between scenes but you understand how to use the animation tab