1. インターフェースの作成
1. 以下に示すように Unity インターフェイスを作成します。
button1: 前のビデオ
button2: 再生/一時停止
button3: 次のビデオ
vidotime: はテキストであり、ビデオ時間を表示します
videoname: ビデオ名
2.具体的なインターフェースは次のとおりです
2. スクリプトのインポート
1. RawImage にスクリプトをインポートして、ビデオの再生/一時停止およびビデオ機能の切り替えを実現します。
System.Collections を使用します。
System.Collections.Generic を使用します。
UnityEngine を使用する。
UnityEngine.UI を使用します。
UnityEngine.Video を使用します。
public class Video_Controller : MonoBehaviour
{ private VideoPlayer ビデオプレーヤー; プライベート RawImage rawImage; プライベート int currentClipIndex;
パブリックテキスト text_playorpause;
パブリックボタン button_playorpause;
パブリックボタン button_pre;
パブリックボタン button_next;
public VideoClip[] ビデオクリップ;
void Start()
{ videoplayer = this.GetComponent<VideoPlayer>(); rawImage = this.GetComponent<RawImage>(); 現在のクリップインデックス = 0; button_playorpause.onClick.AddListener(OnplayorpauseVideo); button_pre.onClick.AddListener(OnpreVideo); button_next.onClick.AddListener(OnnextVideo); }
// Update はフレームごとに 1 回呼び出されます
void Update()
{ if (videoplayer.texture == null) { return; rawImage.texture = videoplayer.texture;
}
private void OnplayorpauseVideo()
{ if (videoplayer.enabled == true) { if (videoplayer.isPlaying) { videoplayer.Pause(); text_playorpause.text = "播放"; else if (!videoplayer.isPlaying) { videoplayer.Play(); text_playorpause.text = "暂停止"; } } } private void OnpreVideo() { currentClipIndex -= 1; if (currentClipIndex < 0) {
currentClipIndex = videoClips.Length - 1;
videoplayer.clip
= videoClips[currentClipIndex];
text_playorpause.text = "暂停止";
プライベート
void OnnextVideo()
{ currentClipIndex += 1; currentClipIndex = currentClipIndex % videoClips.Length; videoplayer.clip = videoClips[currentClipIndex]; text_playorpause.text = "暂停止"; } }
2. さまざまな機能については、対応するボタンを選択します
3. ビデオプレーヤーコンポーネントを追加し、再生するビデオを選択します
4. ビデオのプログレスバーと再生時間の表示を実現するスクリプトを追加します。
UnityEngine を使用する。
UnityEngine.UI を使用します。
UnityEngine.Video を使用します。
パブリック クラス ToPlayVideo : MonoBehaviour
{
public VideoClip videoClip; // ビデオ ファイルのパラメータ
public Text videoTimeText; // ビデオ時間 Text
public Text videoNameText; // ビデオ名 Text
public Slider videoTimeSlider; // ビデオ時間スライダー
// VideoPlayer コンポーネントと RawImage コンポーネントを取得するためのパラメータを定義します
内部 VideoPlayer ビデオプレーヤー;
プライベート RawImage rawImage;
// これを初期化に使用します
void Start()
{
// シーン内の対応するコンポーネントを取得します
videoPlayer = this.GetComponent<VideoPlayer>();
rawImage = this.GetComponent<RawImage>();
videoPlayer.clip = ビデオクリップ;
videoNameText.text = videoClip.name;
ClipHour = (int)videoPlayer.clip.length / 3600;
クリップ分 = (int)(videoPlayer.clip.length - クリップ時間 * 3600) / 60;
ClipSecond = (int)(videoPlayer.clip.length - ClipHour * 3600 - ClipMinute * 60);
videoPlayer.Play();
}
// Update はフレームごとに 1 回呼び出されます
void Update()
{
//videoPlayer に対応するビデオ テクスチャがない場合は、return
if (videoPlayer.texture == null)
{
戻る;
}
// VideoPlayerd のビデオを UGUI の RawImage にレンダリングします
rawImage.texture = videoPlayer.texture;
ShowVideoTime();
}
/// <概要>
/// 現在のビデオの時間を表示します
/// </概要>
プライベート void ShowVideoTime()
{
// 現在の動画の再生時間
currentHour = (int)videoPlayer.time / 3600;
currentMinute = (int)(videoPlayer.time - currentHour * 3600) / 60;
currentSecond = (int)(videoPlayer.time - currentHour * 3600 - currentMinute * 60);
// 現在の動画の再生時間をTextに表示します
videoTimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2} / {3:D2}:{4:D2}:{5:D2}",
currentHour、currentMinute、currentSecond、clipHour、clipMinute、clipSecond);
// 現在のビデオ再生の時間比率をスライダーに割り当てます
videoTimeSlider.value = (float)(videoPlayer.time / videoPlayer.clip.length);
}
/// <概要>
/// 現在のスライダー スケール値が現在のビデオ再生時間に変換されます
/// </概要>
private void SetVideoTimeValueChange()
{
videoPlayer.time = videoTimeSlider.value * videoPlayer.clip.length;
}
// 現在のビデオの合計時間値と現在の再生時間値のパラメータ
private int currentHour;
private int currentMinute;
プライベート int currentSecond;
プライベート int クリップ時間;
private int ClipMinute;
private int ClipSecond;
}
5. 対応するコントロールを追加します
6. プログレスバーによるビデオ再生のドラッグと早送りを実現するには、次のスクリプトをスライダー コントロールに追加します。
UnityEngine を使用する。
UnityEngine.EventSystems を使用します。
/// <概要>
/// ドラッグアンドドロップインターフェイスを継承します
/// </概要>
パブリック クラス SliderEvent : MonoBehaviour、IDragHandler
{
[シリアル化フィールド]
private ToPlayVideo toPlayVideo; // ビデオ再生用のスクリプト
// これを初期化に使用します
void Start()
{
}
// Update はフレームごとに 1 回呼び出されます
void Update()
{
}
/// <概要>
/// スライダーにドラッグイベントを追加
/// </概要>
/// <param name="eventData"></param>
public void OnDrag(PointerEventData イベントデータ)
{
SetVideoTimeValueChange();
}
/// <概要>
/// 現在のスライダー スケール値が現在のビデオ再生時間に変換されます
/// </概要>
private void SetVideoTimeValueChange()
{
toPlayVideo.videoPlayer.time = toPlayVideo.videoTimeSlider.value * toPlayVideo.videoPlayer.clip.length;
}
最終
的な効果