关于在unity中使用序列帧动画

 //动画数组
    public object[] anim;
    //限制一秒多少帧
    public float fps = 30;
    //帧序列
    private int nowFram;
    //记录当前时间
    private float switchTime;
    public string path = "Texture/33";

    public bool isLoop = false;

    public Image image;

    public Texture2D texture;
    void Awake()
    {

       anim = Resources.LoadAll(path);
    }

    void Update()
    {

        if (anim == null)
        {
            anim = Resources.LoadAll(path);
        }
       DrawAnimation(anim);
    }

    void DrawAnimation(object[] tx)
    {
        switchTime += Time.deltaTime;
        if (switchTime >= 1.0 / fps)
        {

            nowFram++;
            switchTime = 0;
            if (nowFram >= tx.Length)
            {
                if (isLoop)

                    nowFram = 0;
                else
                    return;
            }
        }
        if (nowFram < tx.Length)
        {
            texture = (Texture2D)tx[nowFram];
           Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            image.sprite = sp;
        }

    }

如果在运行时,出现报错,说是类型无法转换的错误的话。可能是你把图片类型转换成sprite了。我们在unity中在把塔转换成texture格式在运行就不会报错了。

原文地址:https://www.cnblogs.com/unitySPK/p/7279351.html