unity序列帧特效动态创建播放
来源:互联网 发布:如何模拟网络拥塞 编辑:程序博客网 时间:2024/06/08 13:35
以前的项目里面用过一个序列帧特效的类,但是很麻烦,每一个特效都需要预先创建好perfab,才可以使用播放,而且不能动态创建
昨天自己写了一个方便一点的只需要把图片放到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);}}
0 0
- unity序列帧特效动态创建播放
- 创建序列帧动画特效
- unity序列帧动画的粒子特效
- Unity基础开发----播放序列帧图片
- unity 粒子特效播放状态停止在最后一帧
- Unity3D:粒子特效(Particle System)播放序列帧动画
- Unity3D:粒子特效(Particle System)播放序列帧动画
- unity 动态创建摄像机
- unity动态创建文件夹
- JavaScript js 动态更换、播放图片特效
- 关于Unity特效未播放完的问题
- 【Unity】编辑器下同时播放多个粒子特效
- unity粒子特效附上贴图后播放动画
- Unity 动态创建mesh API
- 播放特效
- unity自动获取特效的播放时间,并自动删除特效
- Unity3d 序列帧播放
- Unity 序列帧动画
- git 使用摘要--版本回退
- 求一组整数数组中的连续子序列和的最大值
- buffer cache 深度解析
- 关于gcc编译错误expected expression before token的一些想法
- 黑马程序员_精简三层架构DAL(初步认识DAL,Model,DBNull.Value的作用)
- unity序列帧特效动态创建播放
- hadoop配置 - 启动backupNode和SecondaryNamenode
- 标准IO库--unix环境高级编程读书笔记
- iOS7下有时MKMapView中的字特别大的问题的解决
- SciTe 中文设置(解决乱码,中文界面)
- 解决plsql不能连接64位oracle问题
- linux 后台执行命令
- NuGet 安装 SimpleJson
- oracle 修改字符集