参考过的一篇文章:https://blog.csdn.net/athryin/article/details/73770502
我这里使用了 EasyTouch 插件
不废话直接上代码:
using System.Collections;
using System.Collections.Generic;
using HedgehogTeam.EasyTouch;
using UnityEngine;
public class XuLieZhenAni : MonoBehaviour
{
//texture贴图变量
[SerializeField]
private Texture ImageAmi;
//计数器
[SerializeField]
private int _countImageNumber = 41;
//当前时间
private float _currentTime = 0f;
//每帧间隔时间
[SerializeField]
private float IntervalTime = 3.0f;
//贴图文件名
private string _leftTextureName = "Image/0_";
//贴图名字
public string _myTextureName;
//起始帧
public int _myStartFrame = 0000;
//帧间隔
public int _myOffsetFrame = 1;
//当前帧
private int _currentFrame = 0;
// Use this for initialization
void Start()
{
_countImageNumber = 0;
this.GetComponent<Renderer>().material.mainTexture = ImageAmi;
}
// Update is called once per frame
void Update()
{
//获取当前触摸点
Gesture gesture = EasyTouch.current;
if (gesture != null && EasyTouch.EvtType.On_Swipe == gesture.type)
{
OnSwiper(gesture);
}
LeftLoadImage();
//if (_currentFrame < 41)
//{
// _currentFrame++;
//}
//else
//{
// _countImageNumber = 0;
// _currentFrame = 0;
//}
}
//顺序加载图片
void LeftLoadImage()
{
//string _rightTextureName = string.Format("{0:D4}", _currentFrame);
_myTextureName = _leftTextureName + _currentFrame;
Texture ImgAmi = (Texture)Resources.Load(_myTextureName);
this.GetComponent<Renderer>().material.mainTexture = ImgAmi;
Debug.Log("_myTextureName ==" + _myTextureName);
}
//当触摸滑动
private void OnSwiper(Gesture gesture)
{
if (gesture.swipe == EasyTouch.SwipeDirection.Right || gesture.swipe == EasyTouch.SwipeDirection.DownRight || gesture.swipe == EasyTouch.SwipeDirection.UpRight)
{
if (_currentFrame < 41)
{
_currentFrame++;
}
else {
_countImageNumber = 0;
_currentFrame = 0;
}
}
if (gesture.swipe == EasyTouch.SwipeDirection.Left || gesture.swipe == EasyTouch.SwipeDirection.DownLeft || gesture.swipe == EasyTouch.SwipeDirection.UpLeft)
{
if (_currentFrame > 0)
{
_currentFrame--;
}
else
{
_countImageNumber = 41;
_currentFrame = 41;
}
}
}
}
如果你没有效果 需要注意⚠️
//贴图文件名
private string _leftTextureName = "Image/0_";
需要在 Assets 下面创建一个 Resources 文件夹
然后我在 Resources 文件夹下 又创建了 Image 文件夹序列帧 图片全部放在Image文件夹里面
目录如下?
我用的是 Unity Resources 加载 所以创建 Resources文件夹是必须的
所以 private string _leftTextureName = "Image/0_"; 如果想修改路径 只需要修改这里
比如我在 Resources 文件夹下 创建了一个 img 文件夹 并且图片都放在里面
那么 这句代码应该这样写
private string _leftTextureName = "img/0_";
后面的 0_ 应为的我的序列帧图片前缀 全是0_ 开头 方便拼接