ビデオコントローラの作成
ビデオコントローラ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
public class MovieController : MonoBehaviour
{
public VideoPlayer vp;
public AudioClip clickSound;
public GameObject playButton;
public Sprite[] buttonImages;
public Slider timeSlider;
public Text totalTimeLabel;
private int totalTime;
private int totalMin;
private int totalSec;
public Text playTimeLabel;
private int playTime;
private int playMin;
private int playSec;
public GameObject loopButton;
public VideoClip[] movies;
private int currentNum = 0;
private Canvas canvas;
private void Start()
{
// ④動画の総時間の表示
totalTime = (int)vp.length;
totalMin = totalTime / 60;
totalSec = totalTime % 60;
totalTimeLabel.text = totalMin + ":" + totalSec.ToString("D2");
canvas = GetComponent<Canvas>();
}
private void Update()
{
if(vp.isPlaying)
{
// ③タイムスライダー
timeSlider.maxValue = vp.frameCount;
timeSlider.value = vp.frame;
// ⑤動画の再生時間の表示
// vp.frame・・・>経過フレーム数
// vp.frameRate・・・>フレームレート(1秒間あたりのフレーム更新数)
// 例;150 / 30 = 5秒
playTime = (int)(vp.frame / vp.frameRate);
playMin = playTime / 60;
playSec = playTime % 60;
playTimeLabel.text = playMin + ":" + playSec.ToString("D2");
}
// ⑩コントローラキャンバスの表示・非表示
if(Application.isEditor)
{
if (Input.GetKeyDown(KeyCode.T))
{
if (canvas.enabled)
{
canvas.enabled = false;
}
else
{
canvas.enabled = true;
}
}
}
else
{
if(OVRInput.GetDown(OVRInput.Button.Back))
{
if (canvas.enabled)
{
canvas.enabled = false;
}
else
{
canvas.enabled = true;
}
}
}
}
// ①再生&一時停止
public void OnPlayButtonClicked()
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
if(!vp.isPlaying)
{
vp.Play();
playButton.GetComponent<Image>().sprite = buttonImages[1];
}
else
{
vp.Pause();
playButton.GetComponent<Image>().sprite = buttonImages[0];
}
}
// ②停止
public void OnStopButtonClicked()
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
vp.Stop();
playButton.GetComponent<Image>().sprite = buttonImages[0];
// (追加)スラーダーをリセットする。
timeSlider.value = 0;
// (追加)動画再生時間をリセットする。
playMin = 0;
playSec = 0;
playTimeLabel.text = playMin + ":" + playSec.ToString("D2");
}
// ⑥5秒早送り
public void OnFastForwardButtonClicked()
{
if(vp.isPlaying)
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
// (考え方)30FPS・・・>30frame = 1second・・・>150frame = 5second
vp.frame += 150;
}
}
// ⑦5秒巻き戻し
public void OnRewindButtonClicked()
{
if (vp.isPlaying)
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
// (考え方)30FPS・・・>30frame = 1second・・・>150frame = 5second
vp.frame -= 150;
}
}
// ⑧ループ設定
public void OnLoopButtonClicked()
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
if(!vp.isLooping)
{
vp.isLooping = true;
loopButton.GetComponent<Image>().color = Color.green;
}
else
{
vp.isLooping = false;
loopButton.GetComponent<Image>().color = Color.white;
}
}
// ⑨動画選択(次の動画を再生)
public void OnNextButtonClicked()
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
vp.Stop();
currentNum = (currentNum + 1) % movies.Length;
vp.clip = movies[currentNum];
totalTime = (int)vp.clip.length;
totalMin = totalTime / 60;
totalSec = totalTime % 60;
totalTimeLabel.text = totalMin + ":" + totalSec.ToString("D2");
vp.Play();
playButton.GetComponent<Image>().sprite = buttonImages[1];
}
}
Unity Code Memo
他のコースを見るビデオコントローラ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
public class MovieController : MonoBehaviour
{
public VideoPlayer vp;
public AudioClip clickSound;
public GameObject playButton;
public Sprite[] buttonImages;
public Slider timeSlider;
public Text totalTimeLabel;
private int totalTime;
private int totalMin;
private int totalSec;
public Text playTimeLabel;
private int playTime;
private int playMin;
private int playSec;
public GameObject loopButton;
public VideoClip[] movies;
private int currentNum = 0;
private Canvas canvas;
private void Start()
{
// ④動画の総時間の表示
totalTime = (int)vp.length;
totalMin = totalTime / 60;
totalSec = totalTime % 60;
totalTimeLabel.text = totalMin + ":" + totalSec.ToString("D2");
canvas = GetComponent<Canvas>();
}
private void Update()
{
if(vp.isPlaying)
{
// ③タイムスライダー
timeSlider.maxValue = vp.frameCount;
timeSlider.value = vp.frame;
// ⑤動画の再生時間の表示
// vp.frame・・・>経過フレーム数
// vp.frameRate・・・>フレームレート(1秒間あたりのフレーム更新数)
// 例;150 / 30 = 5秒
playTime = (int)(vp.frame / vp.frameRate);
playMin = playTime / 60;
playSec = playTime % 60;
playTimeLabel.text = playMin + ":" + playSec.ToString("D2");
}
// ⑩コントローラキャンバスの表示・非表示
if(Application.isEditor)
{
if (Input.GetKeyDown(KeyCode.T))
{
if (canvas.enabled)
{
canvas.enabled = false;
}
else
{
canvas.enabled = true;
}
}
}
else
{
if(OVRInput.GetDown(OVRInput.Button.Back))
{
if (canvas.enabled)
{
canvas.enabled = false;
}
else
{
canvas.enabled = true;
}
}
}
}
// ①再生&一時停止
public void OnPlayButtonClicked()
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
if(!vp.isPlaying)
{
vp.Play();
playButton.GetComponent<Image>().sprite = buttonImages[1];
}
else
{
vp.Pause();
playButton.GetComponent<Image>().sprite = buttonImages[0];
}
}
// ②停止
public void OnStopButtonClicked()
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
vp.Stop();
playButton.GetComponent<Image>().sprite = buttonImages[0];
// (追加)スラーダーをリセットする。
timeSlider.value = 0;
// (追加)動画再生時間をリセットする。
playMin = 0;
playSec = 0;
playTimeLabel.text = playMin + ":" + playSec.ToString("D2");
}
// ⑥5秒早送り
public void OnFastForwardButtonClicked()
{
if(vp.isPlaying)
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
// (考え方)30FPS・・・>30frame = 1second・・・>150frame = 5second
vp.frame += 150;
}
}
// ⑦5秒巻き戻し
public void OnRewindButtonClicked()
{
if (vp.isPlaying)
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
// (考え方)30FPS・・・>30frame = 1second・・・>150frame = 5second
vp.frame -= 150;
}
}
// ⑧ループ設定
public void OnLoopButtonClicked()
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
if(!vp.isLooping)
{
vp.isLooping = true;
loopButton.GetComponent<Image>().color = Color.green;
}
else
{
vp.isLooping = false;
loopButton.GetComponent<Image>().color = Color.white;
}
}
// ⑨動画選択(次の動画を再生)
public void OnNextButtonClicked()
{
AudioSource.PlayClipAtPoint(clickSound, transform.position);
vp.Stop();
currentNum = (currentNum + 1) % movies.Length;
vp.clip = movies[currentNum];
totalTime = (int)vp.clip.length;
totalMin = totalTime / 60;
totalSec = totalTime % 60;
totalTimeLabel.text = totalMin + ":" + totalSec.ToString("D2");
vp.Play();
playButton.GetComponent<Image>().sprite = buttonImages[1];
}
}
ビデオコントローラの作成