zoukankan      html  css  js  c++  java
  • 关于在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格式在运行就不会报错了。

  • 相关阅读:
    .NET反射的优化
    jdk、tomcat、solr环境搭建
    实现简单的ORM
    异步async/await简单应用与探究
    线程(Thread,ThreadPool)、Task、Parallel
    序列化
    IEnumerable与IEnumerator
    URL重写与URL路由
    django rest framework(10)
    restful 规范
  • 原文地址:https://www.cnblogs.com/unitySPK/p/7279351.html
Copyright © 2011-2022 走看看