Unity 手指滑动控制视频播放进度。

1:我这边在一开始做的时候用的是unity自带videoPlayer来做视频的控制器的,后来测试发现滑动的并不是那么平滑,后来改为了AVPro这个插件,就比较平滑。

2:下面是代码


using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using RenderHeads.Media.AVProVideo;
using System;

public class Swape_Help : MonoBehaviour
{
    private Vector3 startFingerPos;
    private Vector3 nowFingerPos;
    private float xMoveDistance;
    private float yMoveDistance;
    private int backValue = 0;
    public MediaPlayer video;
    Text Text;

    void Update()
    {
        JudgeFinger();
    }


    float timer;//计时器
    Vector3 beforePos;//前0.1秒的手指位置;

    public void JudgeFinger()
    {

        if (Input.touchCount > 0)
        {
            if (Input.GetTouch(0).phase == TouchPhase.Stationary) return;

            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
                startFingerPos = Input.GetTouch(0).position;

            }
            timer += Time.deltaTime;
            if (timer > 0.1f)
            {
                beforePos = Input.GetTouch(0).position;
                timer = 0;

            }

            nowFingerPos = Input.GetTouch(0).position;


            xMoveDistance = Mathf.Abs(nowFingerPos.x - startFingerPos.x);

            yMoveDistance = Mathf.Abs(nowFingerPos.y - startFingerPos.y);

            if (xMoveDistance > 0)
            {
                //向屏幕右滑动
                if (nowFingerPos.x - beforePos.x > 0)//当前位置减去前0.1位置来判断方向
                {
                    backValue = -1;

                }
                else
                {

                    //向屏幕左滑动
                    backValue = 1;

                }

            }
            if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                if (video.Control.IsPlaying())
                {
                    video.Pause();
                }
                SetVideo_ChangeValue(backValue);
            }
            if (Input.GetTouch(0).phase == TouchPhase.Ended)
            {
                video.Play();
            }
        }


    }

    void SetVideo_ChangeValue(int isRightOrLeft)
    {

        /* TODO测试*/   //向左滑动
        if (isRightOrLeft == 1)
        {
            float m = video.Control.GetCurrentTimeMs() - (xMoveDistance * 0.5f / Screen.width) * video.Info.GetDurationMs();
            m = Mathf.Clamp(m, 0, video.Info.GetDurationMs());

            video.Control.Seek(m);

            //Text.text = video.frame.ToString() + "滑动后帧数"+x.ToString()+"ppp";
        }
        if (isRightOrLeft == -1)
        {
            float m = video.Control.GetCurrentTimeMs() + (xMoveDistance * 0.5f / Screen.width) * video.Info.GetDurationMs();
            m = Mathf.Clamp(m, 0, video.Info.GetDurationMs());

            video.Control.Seek(m);
        }

    }

    public void LoadScnes(string Name)
    {
        SceneManager.LoadScene(Name);
       // Debug.Log("....");

    }

}

猜你喜欢

转载自blog.csdn.net/wwrm111/article/details/84851854
今日推荐