C# 如何统一编程精灵之间的淡入淡出过渡?

C# 如何统一编程精灵之间的淡入淡出过渡?,c#,unity3d,sprite,C#,Unity3d,Sprite,我有一个动画在4 PNG图像。我想让帧在1/2秒内以1-2-3-4-2-1的顺序播放,并进行透明度转换 我写的是,当包含不同精灵的父对象生成时,第一帧应该立即出现,然后让它在1/12秒的时间内变为透明,而第二帧变为不透明,依此类推,直到最后一帧结束其透明循环 这可能不是最有效的方法,但我制作了一个en空对象的预制件,在该预制件下放置了6个精灵框架,每个精灵都有一个单独的脚本 我以前三个脚本为例: using System.Collections; using System.Collections

我有一个动画在4 PNG图像。我想让帧在1/2秒内以1-2-3-4-2-1的顺序播放,并进行透明度转换

我写的是,当包含不同精灵的父对象生成时,第一帧应该立即出现,然后让它在1/12秒的时间内变为透明,而第二帧变为不透明,依此类推,直到最后一帧结束其透明循环

这可能不是最有效的方法,但我制作了一个en空对象的预制件,在该预制件下放置了6个精灵框架,每个精灵都有一个单独的脚本

我以前三个脚本为例:

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);
        }

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类框架1:单一行为
{
私家雪碧;
私有颜色α;
专用浮点定时器;
//在第一帧更新之前调用Start
void Start()
{
αa=255;
thisSprite.GetComponent().color=alpha;
}
//每帧调用一次更新
无效更新()
{
定时器=定时器+Time.deltaTime;
α.a-=定时器*3060;
thisSprite.GetComponent().color=alpha;
如果(计时器>=1/12)
{
摧毁(游戏对象);
}
}
}

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类框架2:单一行为
{
私家雪碧;
私有颜色α;
专用浮点定时器;
私人int方向;
//在第一帧更新之前调用Start
void Start()
{
αa=0;
thisSprite.GetComponent().color=alpha;
}
//每帧调用一次更新
无效更新()
{
如果(方向==0)
{
定时器=定时器+Time.deltaTime;
α.a+=定时器*3060;
thisSprite.GetComponent().color=alpha;
如果(计时器>=1/12)
{
方向=1;
}
}
如果(方向==1)
{
定时器=定时器-Time.deltaTime;
α.a+=定时器*3060;
thisSprite.GetComponent().color=alpha;
如果(计时器>=1/6)
{
摧毁(游戏对象);
}
}
}
}

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类框架3:单一行为
{
私家雪碧;
私有颜色α;
专用浮点定时器;
私人int方向;
//在第一帧更新之前调用Start
void Start()
{
αa=0;
thisSprite.GetComponent().color=alpha;
定时器-=1/12;
}
//每帧调用一次更新
无效更新()
{
如果(方向==0)
{
定时器=定时器+Time.deltaTime;
α.a+=定时器*3060;
thisSprite.GetComponent().color=alpha;
如果(计时器>=1/12)
{
方向=1;
}
}
如果(方向==1)
{
定时器=定时器-Time.deltaTime;
α.a+=定时器*3060;
thisSprite.GetComponent().color=alpha;
如果(计时器>=1/6)
{
摧毁(游戏对象);
}
}
}
}
它们似乎在生成的那一刻就可以看到,而且它们不会消失,甚至根本不会被摧毁。问题是什么


谢谢。

如评论所示,使用动画是一种可行的选择。但是,您的代码无法正常工作,因为alpha接受0到1的值,而不是0到255的值。
因此,只需将逻辑从1调整为0,就可以看到渐变过渡

你需要一个完全不同的方法来做这件事。创建一个动画,使用精灵表完美地模拟这些动画。将动画放置在animator控制器中,将其附加到对象,对于启动动画的对象,您必须只有一个脚本(使用animator控制器条件设置其触发器)。。我还没有时间给出一个完整的答案,但如果没有人给出,我会稍后再做。我会检查我是否理解你当前答案中的方法。。。但是我的方法有什么问题?假设我有一组不同的对象,我希望它们的alpha值随时间而变化。你怎么告诉他们那样做?根据我收集的信息,这些线条应该会修改对象的alpha值。这种方法对我来说非常奇怪,我从来没有使用过类似的方法,因此我无法回答这个问题,相反,我可以向你指出最佳实践:)想象一下,如果你有很多精灵,你想以不同的方式设置它们的动画。精灵图纸和动画将是一种方式去。这就是为什么我没有发布答案,我不知道如何解决你的问题,相反,我可以解释正确的方法-也许有人会回答:)Sprite sheet是一个单独的PNG图像,在矩阵中放置了不同的帧,是吗?你不能用你想用的那种方法来使用单独的PNG帧吗?哦,等等。如果我理解正确,你的意思是我应该在PNG中设置淡入动画?我想避免这种情况,因为这样会创建更多我认为不必要的PNG数据。我正在制作动画的帧的分辨率非常高,因此制作其中30帧(以60帧/秒的速度为1/2)来制作4个关键帧的动画似乎效率很低。我最终明白了这一点,第一个问题实际上是我没有正确编写GetComponent部分的文字,因此它没有与包含脚本的对象交互,但是现在由于某种原因,虽然精灵现在确实出现和消失了,但它并没有在正确的时间内这样做。它们只存在一小部分时间,我不明白为什么。
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);
            }
        }

    }
}