记录一下我在做仿杀戮尖塔游戏的角色动画展示方法
简单来说就是把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;
}
}