100 lines
2.7 KiB
C#
Executable File
100 lines
2.7 KiB
C#
Executable File
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Video;
|
|
|
|
public class VideoTest : MonoBehaviour
|
|
{
|
|
public GameObject VideoPlayerObject;
|
|
VideoPlayer videoPlayer;
|
|
public string VideoFileName = "test1.mp4";
|
|
|
|
public bool ScriptPlaysVideoOnMouseDown = false;
|
|
public bool ScriptStopsVideoOnMouseDown = false;
|
|
public RawImage UIViewport;
|
|
public bool UseAssetBundles = false;
|
|
public AssetBundle VideoBundle;
|
|
public VideoBundleLoader BundleLoader;
|
|
|
|
private void Start()
|
|
{
|
|
videoPlayer = VideoPlayerObject.GetComponent<VideoPlayer>();
|
|
if (UseAssetBundles && BundleLoader)
|
|
VideoBundle = BundleLoader.VideoAssetBundle;
|
|
}
|
|
|
|
public void SetVideoFileName(string filename)
|
|
{
|
|
VideoFileName = filename;
|
|
}
|
|
|
|
void OnMouseDown()
|
|
{
|
|
if (ScriptPlaysVideoOnMouseDown)
|
|
{
|
|
PlayVideo();
|
|
}
|
|
else if (ScriptStopsVideoOnMouseDown)
|
|
{
|
|
StopVideo();
|
|
}
|
|
else
|
|
{
|
|
SetVideoUrlFromBundle();
|
|
}
|
|
}
|
|
public void SetVideoUrl()
|
|
{
|
|
VideoFileName = Path.Combine(Application.streamingAssetsPath, "Video", VideoFileName);
|
|
if (!File.Exists(VideoFileName))
|
|
{
|
|
Debug.LogError("Video not found: " + VideoFileName);
|
|
VideoFileName = "";
|
|
}
|
|
else
|
|
{
|
|
videoPlayer.url = VideoFileName;
|
|
videoPlayer.Stop();
|
|
Debug.LogError("Set videoPlayer URL to " + VideoFileName);
|
|
}
|
|
//VideoPlayerObject.transform.localScale = new Vector3(videoPlayer.clip.width / 16 / 4, videoPlayer.clip.height / 16 / 4);
|
|
}
|
|
|
|
public void SetVideoUrlFromBundle()
|
|
{
|
|
if (VideoBundle == null)
|
|
{
|
|
Debug.Log("Failed to load VideoBundle!");
|
|
return;
|
|
}
|
|
VideoClip clip = VideoBundle.LoadAsset<VideoClip>(VideoFileName);
|
|
videoPlayer.clip = clip;
|
|
videoPlayer.Stop();
|
|
Debug.LogError("Loaded video from assetbundle: " + VideoBundle.name + " " + clip.name);
|
|
}
|
|
|
|
public void PlayVideo()
|
|
{
|
|
Debug.Log("Tried playing " + VideoFileName);
|
|
videoPlayer.Play();
|
|
}
|
|
public void PlayUIVideo()
|
|
{
|
|
if (UIViewport)
|
|
{
|
|
videoPlayer.renderMode = VideoRenderMode.RenderTexture;
|
|
//videoPlayer.Prepare();
|
|
|
|
UIViewport.texture = videoPlayer.texture;
|
|
videoPlayer.Play();
|
|
}
|
|
}
|
|
public void StopVideo()
|
|
{
|
|
Debug.Log("Tried stopping playback of " + VideoFileName);
|
|
if (videoPlayer.isPaused)
|
|
videoPlayer.Stop();
|
|
videoPlayer.Pause();
|
|
}
|
|
}
|