Unity_触摸屏_实现360序列帧

参考过的一篇文章: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_ 开头 方便拼接

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值