Unity中播放序列图
一、目的
想知道:Unity中播放序列图
二、参考
1、unity序列帧的播放方法
1、直接选中所有的图片拉到Hirarchy面板上去 ,保存动画,此时默认用了sprirender和动画播放
2、新建一个image 选中 然后在animation窗口上创建一个新的animation
samples是每秒播放多少张动画 用来控制动画速度,然后把图片拉上来
必须按这样的顺序 不然直接在Image上添加动画是不行的
————————————————
版权声明:本文为优快云博主「misiCreate」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/misiCreate/article/details/81412301
总结:亲测有效:先将图片转为sprite,然后拖到hierarcht中,变为动画就能正常播放
2、Unity自定义组件之序列帧播放组件
/***********************************
* Description:描述这是一个图片序列帧播放脚本,
* Mountpoint:挂载点将其挂载在Image组件上
* Date:2019.07.11
* Version:unity版本2017.2.0f3
* Author:LJF
***********************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using LjfLog;
using UnityEngine.Events;
namespace LJF
{
//规范命名、添加注释、合理封装、限制访问权限、异常处理
public class ImageAnimation : MonoBehaviour
{
public enum State
{
idle,
playing,
pause
}
public enum State1
{
once,
loop
}
[Header("播放方式(循环、单次)")]//默认单次
public State1 condition=State1.once;
[Header("自动播放")]//默认不自动播放
public bool Play_Awake = false;
//播放状态(默认、播放中、暂停)
private State play_state;
private Image manimg;
[Header("每秒播放的帧数(整数)")]
public float frame_number=30;
[Header("sprite数组")]
public Sprite[] sprit_arr;
//回调事件
public UnityEvent onCompleteEvent;
private int index;
private float tim;
private float waittim;
private bool isplay;
void Awake()
{
manimg = GetComponent<Image>();
tim = 0;
index = 0;
waittim = 1 / frame_number;
play_state = State.idle;
isplay = false;
if (manimg == null)
{
Debuger.LogWarning("Image为空,请添加Image组件!!!");
return;
}
if (sprit_arr.Length<1)
{
Debuger.LogWarning("sprite数组为0,请给sprite数组添加元素!!!");
}
manimg.sprite = sprit_arr[0];
if (Play_Awake)
{
Play();
}
}
void Update()
{
//测试
if (Input.GetKeyDown(KeyCode.A))
{
Play();
}
if (Input.GetKeyDown(KeyCode.S))
{
Replay();
}
if (Input.GetKeyDown(KeyCode.D))
{
Stop();
}
if (Input.GetKeyDown(KeyCode.P))
{
Pause();
}
UpMove();
}
private void UpMove()
{
//单播
if (condition == State1.once)
{
if (play_state == State.idle && isplay)
{
play_state = State.playing;
index = 0;
tim = 0;
}
if (play_state == State.pause && isplay)
{
play_state = State.playing;
tim = 0;
}
if (play_state == State.playing && isplay)
{
tim += Time.deltaTime;
if (tim >= waittim)
{
tim = 0;
index++;
if (index >= sprit_arr.Length)
{
index = 0;
manimg.sprite = sprit_arr[index];
isplay = false;
play_state = State.idle;
//此处可添加结束回调函数
if (onCompleteEvent != null)
{
onCompleteEvent.Invoke();
return;
}
}
manimg.sprite = sprit_arr[index];
}
}
}
//循环播放
if (condition == State1.loop)
{
if (play_state == State.idle && isplay)
{
play_state = State.playing;
index = 0;
tim = 0;
}
if (play_state == State.pause && isplay)
{
play_state = State.playing;
tim = 0;
}
if (play_state == State.playing && isplay)
{
tim += Time.deltaTime;
if (tim >= waittim)
{
tim = 0;
index++;
if (index >= sprit_arr.Length)
{
index = 0;
//此处可添加结束回调函数
}
manimg.sprite = sprit_arr[index];
}
}
}
}
/// <summary>
/// 播放
/// </summary>
public void Play()
{
isplay = true;
}
/// <summary>
/// 暂停
/// </summary>
public void Pause()
{
isplay = false;
play_state = State.pause;
}
/// <summary>
/// 停止
/// </summary>
public void Stop()
{
isplay = false;
play_state = State.idle;
index = 0;
tim = 0;
if (manimg == null)
{
Debuger.LogWarning("Image为空,请赋值");
return;
}
manimg.sprite = sprit_arr[index];
}
/// <summary>
/// 重播
/// </summary>
public void Replay()
{
isplay = true;
play_state = State.playing;
index = 0;
tim = 0;
}
}
}
总结:待检测;思路是:时间不停更新下一站序列图达到动画效果
三、执行&结论
总结:
1、参考一中两种方案,第一种为3D序列帧播放方案,第二种为ui播放方案;
实测时发现,创建animation时需要关注的点是播放序列帧的组件与节点要选对,并不是非要在物体上创建。
2、参考二中两种方案,第一种与参考一下方案一致,第二种采用代码方案,在update中进行每帧图片切换,
animation基本已经覆盖大部分使用场景,如果有更多自定功能,可以使用代码方案。
声明
此博客格式与大部分内容来自于misiCreated,看到这种写法感觉很高效并有趣,之前也见到多这种方案,但是当时还没有好好学习的觉悟,特此记录。