Unity-如何展示2d人物动画

记录一下我在做仿杀戮尖塔游戏的角色动画展示方法

简单来说就是把2d动画或者gif转为很多张静态图片,然后一张一张读取

下面是代码。path就是图片路径,state_change用来转换角色状态,实现了一个点击ui的接口用来测试动画是否能正常播放和转换

97和46是动画总帧数,也就是图片张数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ShowImage : MonoBehaviour,IPointerClickHandler
{
    int idle_image = 1;
    int attack_image = 1;
    float times = 0;
    public int state_change = 0;//0是idle,1是attack
    // Start is called before the first frame update
    void Start()
    {
        state_change = 0;
    }

    // Update is called once per frame
    void Update()
    {
        if(state_change == 0)
        {
            idle();
        }
        if(state_change == 1)
        {
            attack();
        }
        
    }

    void idle()
    {
        times += Time.deltaTime;
        if (times > 0.02)
        {
            string path = "Exusiai_Idle/" + idle_image.ToString();
            transform.GetComponent<Image>().sprite = Resources.Load<Sprite>(path);
            idle_image++;
            if (idle_image > 97)
            {
                idle_image = 1;
            }
            times = 0;
        }
    }

    void attack()
    {
            times += Time.deltaTime;
            if (times > 0.02)
            {
                string path = "Exusiai_Attack/" + attack_image.ToString();
                transform.GetComponent<Image>().sprite = Resources.Load<Sprite>(path);
                attack_image++;
                if (attack_image > 46)
                {
                    attack_image = 1;
                    state_change = 0;
            }
                times = 0;
            }
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        state_change = 1;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值