下面给大家分享一下unity 2d Texture 动态获取序列帧的一些内容,昨天自己写了一个方便一点的只需要把图片放到resources文件夹下面的AniEffect里面就可以了(可以改成自己的文件夹路径)
用法:静态方法SpriteAniTool.AddEffect(多个参数);既可以在场景中添加序列帧特效了
原理:根据名字(要与图片的名称相同),访问一个全局字典,如果这个特效已经加载过,则直接取出该特效的材质球赋予给创建的特效面片,如果这个特效 从来没有用过,那么就去resources文件下下面找,至于序列帧的播放,和大家的方法基本一样,计算uv的tilling 和offect即可,offect保存在一个数组里面,播放序列帧的时候根据速度去数组里面取一个vector2的值,特效就播放了
注意事项:1.特效有两种,根据最后一个参数bool OnGround,如果为true,特效与地面平行,如果为false,特效则和摄像机平行

by the way :脚本加注释一共100行,特意的

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpriteAniTool{
    public static Dictionary<string,Material> EffectLib = new Dictionary<string, Material>();
    /// <summary>
    /// 在场景中添加一个特效,按位置添加,参数代表:名字,持续时间,列,行,速度(帧),位置,是否在地面
    /// </summary>
    public  static void  AddEffect(string name, float time, int clomn, int row,int speed, Vector3 position,bool OnGround){
        GameObject newEffect = GameObject.CreatePrimitive(PrimitiveType.Quad);
        newEffect.name =name;
        newEffect.transform.position = position;
        newEffect.transform.eulerAngles = OnGround?new Vector3(90f,0,0):Camera.main.transform.rotation.eulerAngles;
        AniEffect AniEffect = newEffect.AddComponent<AniEffect>() as AniEffect;
        AniEffect.SetEffect(name, time, clomn, row, speed);
    }
 
}
//特效类
public class AniEffect:MonoBehaviour
{
    public bool Start = false;
    private float _time;
    private string _name;
    private int _Clomn;
    private int _Row;
    private float _speed;
    private float ScaleX;
    private float ScaleY;
    private Texture2D _Texture;
    private Material _Material;
    private Vector2[]  sequence;//序列帧偏移
    //设置特效
    public void SetEffect(string name, float time, int clomn, int row,int speed)
    {
        _Material = this.renderer.material;
        _time = time;
        _name = name;
        _Clomn = clomn;
        _Row = row;
        _speed = speed;
        ScaleX = 1/(float)row;
        ScaleY = 1/(float)clomn;
        SetSequence();
        GetEffectResource();
    }
    float index;
    void Update()
    {
        if(index >= sequence.Length )
        {
            index = 0f ;
        }
        if(Start)
        {
            renderer.material.mainTextureOffset = sequence[(int)index];
            index += _speed*Time.deltaTime;
        }
    }
    //计算每一个具体帧的位置
    private void SetSequence()
    {
        sequence = new Vector2[_Clomn* _Row];
        for(int i = 0 ; i < _Clomn;i ++)
        {
            for(int j = 0 ; j < _Row; j ++)
            {
                sequence[i*_Row + j] = new Vector2(ScaleX*j ,ScaleY*(_Clomn-i-1));
            }
        }
    }
    //为特效图片赋值
    private void GetEffectResource()
    {
        //已经加载过的,去全局字典中寻找,没有加载过的从Resource读取
        if(SpriteAniTool.EffectLib.ContainsKey(_name))
        {
            _Material = SpriteAniTool.EffectLib[_name];
        }else
        {
            _Texture = Resources.Load("AniEffect/" + _name) as Texture2D;
            _Material.shader = Shader.Find("Unlit/Transparent");
            _Material.mainTexture = _Texture;
            SpriteAniTool.EffectLib.Add(name,_Material);
        }
        renderer.material = _Material;
        renderer.material.mainTextureScale = new Vector2(ScaleX,ScaleY);
        StartCoroutine(DestroyAni(_time));
    }
    //删除自己
    IEnumerator   DestroyAni(float time)
    {
        Start = true;
        yield return new WaitForSeconds(time);
        Resources.UnloadUnusedAssets();
        Destroy(this.gameObject);
    }
}