Unity3D鼠标拖拽播放循环序列

鼠标拖拽,通过计算鼠标坐标和按下时的坐标判断播放的序列帧,一定时间无操作自动播放序列。

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

public class RotateBuildnew : MonoBehaviour
{
    //序列载体
    public Image image;
    //序列
    public Sprite[] sprit_arr;
    //当前序列位置
    private int index;
    private float oriX;
    private int offset;
    private int currentIndex = 0;

    //速度控制
    private int delta = 10;

    //无操作时间
    private float noOperate = 0;
    //是否开始计时
    private bool isTime = true;
    //是否允许自动播放
    public bool isCanRoate = false;

    private void OnEnable()
    {
        image.sprite = sprit_arr[0];
        currentIndex = 0;
    }

    // Update is called once per frame
    void Update()
    {
        //鼠标拖拽播放序列
        if (Input.GetMouseButtonDown(0))
        {
            //停止自动播放
            StopCoroutine("onAutoRotate");
            //更新当前图片的序列位置
            currentIndex = index;
            //记录当前鼠标位置
            oriX = Input.mousePosition.x;
        }
        if (Input.GetMouseButton(0))
        {
            //计算当前鼠标坐标和按下鼠标时的差值
            offset = (int)(Input.mousePosition.x - oriX);
            //控制速度
            if (offset != 0)
            {
                offset = offset / delta;
            }
            //计算序列帧数
            index = currentIndex + offset;
            if (index > 0)
            {
                index = index % sprit_arr.Length;
            }
            if (index < 0)
            {
                index = Math.Abs(index);
                index = index % sprit_arr.Length;
                index = sprit_arr.Length - index;
                if (index == sprit_arr.Length)
                {
                    index = 0;
                }
            }
            //更新图片
            image.sprite = sprit_arr[index];
        }
        if (Input.GetMouseButtonUp(0))
        {
            //更新当前图片的序列位置
            currentIndex = index;
        }

        //是否允许自动播放序列
        if(isCanRoate)
        {
            //拖拽时停止计时
            if (Input.GetMouseButtonDown(0) || Input.GetMouseButton(0))
            {
                noOperate = 0;
            }
            //结束拖拽时开始计时
            if (Input.GetMouseButtonUp(0))
            {
                isTime = true;
            }
            if (isTime)
            {
                noOperate += Time.deltaTime;
                if (noOperate >= 10)
                {
                    isTime = false;
                    //超过一定时间开始自动播放
                    StartCoroutine("onAutoRotate");
                }
            }
        }
    }
    //开启自动播放
    private IEnumerator onAutoRotate()
    {
        while (true)
        {
            for (int i = index; i < sprit_arr.Length; i++)
            {
                image.sprite = sprit_arr[i];
                index++;
                yield return new WaitForSeconds(1 / 30.0f);
            }
            index = 0;
        }
    }
}

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值