[Unity] Unity3D实现2D人物动画① UGUI&Native2D序列帧动画

查看:4776 |回复:25 | 2016-11-10 09:14:55

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 成林 于 2018-5-22 21:47 编辑


40.jpg

动态实现2D人物角色动画目前有两种主流方法,下面我会分别进行介绍。
      第一种方法我称之为图片切换法,准备工作:首先通过3DMAX等工具3D渲染2D的方法制作出角色,然后将角色每个动作均导出8个方向每方向若干帧的系列图片(如果是有方向的魔法图片,很多2D-MMORPG往往会导出16个方向的系列帧图片以求更为逼真),即将每个人物每个动作的各方向的每帧均存成一张图片,如下图仅以从破天一剑游戏中提取的素材为例:
(特别申明:本系列教程所使用的如有注明归属权的图片素材均来源于网络,请勿用于商业用途,否则造成的一切后果均与本人无关。)
[url=] 193331-20161108164717842-549690454.jpg
[/url]
从上图可以看到,我将人物向右方跑步共8帧图片通过Photoshop分别将画布等比例扩大成150*150象素图片(因为是提取的素材,初始宽和高是不均衡值,所以必须扩大成自己的需求,这样人物会在图片中居中,并且为后期加入武器或坐骑留好余地。稍微的偏离也可以在后期进行微调),并将他们从开始到结束分别命名为0.png,1.png,2.png,3.png,4.png,5.png,6.png,7.png,然后将这8张图片保存到相关目录下,到此准备工作终于结束了

这里在WPF中有一个UI线程级别的定时器DispatcherTimer,而Unity中没有提供类似的机制(或许是我不知道),Unity主要是心跳来控制的也就是Update函数了,但是这里的原理就是帧动画,每个多少帧变化一下player的动作图片即可,但我们知道帧就是和时间相关的。
简单的说:就是定义一个图片数组,然后实现一个定时器,时间到了就获取数组里的一张图,替换精灵的背景图片。
实现
这里我们把问题分解主要是两个子问题,一、定时获取图片替换精灵背景,简称定时器;二、数组的图片循环获取,简称数组顺序遍历
先从软柿子开始,二比较简单,一个数组,加一个全局基数器变量 搞定

private int currentTexture = 0;
public Sprite[] textureArray;
private SpriteRenderer spriteRenderer;
//遍历数组 到数组未重新回到0索引
void NextTexture()
{
    currentTexture++;
    if (currentTexture >= textureArray.Length)
    {
        currentTexture = 0;
    }
    spriteRenderer.sprite = textureArray[currentTexture];
}

一、定时器,稍微麻烦点,Unity3d并没有提供像样的UI定时器封装,这里为了验证 这种定帧动画的原理,我用几种Unity3d中定时器机制分别实现了动画功能,实际开发中用的A和D方法比较多,至少我查了不少教程基本是A和D
首先是变量

private float animationDeltaTime;
private float animationDelay = 5 / 60f;

A、Update 心跳延时定时器

void Update()
{
    animationDeltaTime += Time.deltaTime;
    // Debug.Log(animationDeltaTime);
    if (animationDeltaTime >= animationDelay)
    {
        animationDeltaTime = 0;
        NextTexture();
    }
}

B、协程递归定时器

void Start()
{
    spriteRenderer = GetComponent<SpriteRenderer>() as SpriteRenderer;
    StartCoroutine(TextureChanger());  
}
IEnumerator TextureChanger()
{
    yield return new WaitForSeconds(animationDelay);
    if (true)
    {
        //Debug.Log(animationDeltaTime);
        NextTexture();
        StartCoroutine(TextureChanger());
    }
}

C、InvokeRepeating定时器

void Start()
{
    spriteRenderer = GetComponent<SpriteRenderer>() as SpriteRenderer;
    InvokeRepeating("NextTexture", 1, 0.1f);//1秒后调用LaunchProjectile () 函数,之后每5秒调用一次      
}

D、时长求余法(我自己起的名字,比较巧妙可能也是用的比较多的方法)

[C#] 纯文本查看 复制代码
[color=white !important]
[color=white !important]?


01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

using UnityEngine;

using System.Collections;


public class PlayerAnimator : MonoBehaviour {


    public Sprite[] sprites;

    public float framesPerSecond;


    private SpriteRenderer spriteRenderer;

    // Use this for initialization

    void Start () {

        spriteRenderer = GetComponent<Renderer>() as SpriteRenderer;

    }

     

    // Update is called once per frame

    void Update () {

        int timeIndex = (int)(Time.timeSinceLevelLoad * framesPerSecond);

        int index = timeIndex % sprites.Length;

        spriteRenderer.sprite = sprites[index];


    }

}






原理的代码分析和代码展示完毕,下面是自己在网上找的前人分享的一些代码,自测可以运行,主要的问题还是一句老话,“原理很简单,现实很残酷”,实际一个简单的动画涉及的东西很多,比如性能效率,状态控制,封装合理性等等吧。
A、Unity3d UGUI序列帧动画 实现 (原文地址:[url=]http://www.cnblogs.com/mrblue/p/5191183.html[/url])
2016-11-10 09:14:55  
 赞 赞 0

使用道具 登录

25个回答,把该问题分享到群,邀请大神一起回答。
2#
非常有用,涨姿势啦,谢谢
回复 收起回复
2016-11-21 15:39:54   回复
 赞 赞 0

使用道具 登录

3#
好东西!
回复 收起回复
2017-1-4 23:26:34   回复
 赞 赞 0

使用道具 登录

4#
为了元素币
回复 收起回复
2017-1-10 22:57:18   回复
 赞 赞 0

使用道具 登录

5#
为了元素币
回复 收起回复
2017-1-10 22:57:22   回复
 赞 赞 0

使用道具 登录

6#
首发必须微元素,挖矿撩妹两不误!
回复 收起回复
2017-1-13 21:38:10   回复
 赞 赞 0

使用道具 登录

7#
我们先定一个能达到的小目标,先赚它一亿元素币
回复 收起回复
2017-2-6 15:51:41   回复
 赞 赞 0

使用道具 登录

8#
实现2D人物动
回复 收起回复
2017-3-12 13:42:15   回复
 赞 赞 0

使用道具 登录

9#
资源发布哪家强?元素首发称大王!
回复 收起回复
2017-12-18 09:48:34   回复
 赞 赞 0

使用道具 登录

10#
还行吧!
回复 收起回复
2017-12-18 17:39:58   回复
 赞 赞 0

使用道具 登录

11#
66666666666666
回复 收起回复
2017-12-19 20:16:45   回复
 赞 赞 0

使用道具 登录

12#
666
回复 收起回复
2018-2-28 16:07:09   回复
 赞 赞 0

使用道具 登录

13#
666
回复 收起回复
2018-3-1 09:42:52   回复
 赞 赞 0

使用道具 登录

14#
666
回复 收起回复
2018-3-23 10:16:46   回复
 赞 赞 0

使用道具 登录

15#
感谢分享
回复 收起回复
2018-9-7 17:24:19   回复
 赞 赞 0

使用道具 登录

16#
【给力】阅贴无数,楼主最强!
回复 收起回复
2018-10-14 14:23:10   回复
 赞 赞 0

使用道具 登录

17#
感谢分享
回复 收起回复
2018-10-14 14:36:00   回复
 赞 赞 0

使用道具 登录

18#
11
回复 收起回复
2018-10-14 15:56:18   回复
 赞 赞 0

使用道具 登录

19#
回复 收起回复
2018-10-30 09:49:00   回复
 赞 赞 0

使用道具 登录

20#
感謝分享這麼好的資源!
回复 收起回复
2018-10-30 09:51:59   回复
 赞 赞 0

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
12下一页
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表