Created
May 10, 2019 18:53
-
-
Save ratozumbi/ad609d56eed64b49b5f1e6ea32e884c2 to your computer and use it in GitHub Desktop.
Preview Video on Unity Editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.Video; | |
//Attach this script to a GameObject | |
[ExecuteInEditMode] | |
[RequireComponent(typeof(VideoPlayer))] | |
public class PlayOnEditor : MonoBehaviour | |
{ | |
[Range(0f,1f)]public float CurrSeek = 0; | |
[HideInInspector] | |
public float LastSeek = 0; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
CurrSeek = GetSeek(); | |
LastSeek = CurrSeek; | |
} | |
public void PlayVideo() | |
{ | |
GetComponent<VideoPlayer>().Play(); | |
} | |
public void PauseVideo() | |
{ | |
GetComponent<VideoPlayer>().Pause(); | |
} | |
public void RestartVideo() | |
{ | |
GetComponent<VideoPlayer>().Stop(); | |
GetComponent<VideoPlayer>().Play(); | |
} | |
public void SeekVideo(float normalized) | |
{ | |
var real = GetComponent<VideoPlayer>().frameCount * normalized; | |
GetComponent<VideoPlayer>().frame = Convert.ToInt64(real); | |
} | |
public float GetSeek() | |
{ | |
var real = (float)GetComponent<VideoPlayer>().frame / (float)GetComponent<VideoPlayer>().frameCount; | |
if (real < 0) return 0; //video not ready | |
return real; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEditor; | |
//Place this script on Assets/Editor | |
[CustomEditor(typeof(PlayOnEditor))] | |
public class ObjectBuilderEditor : Editor | |
{ | |
public override void OnInspectorGUI() | |
{ | |
DrawDefaultInspector(); | |
PlayOnEditor myScript = (PlayOnEditor)target; | |
if(GUILayout.Button("Play")) | |
{ | |
myScript.PlayVideo(); | |
} | |
if(GUILayout.Button("Pause")) | |
{ | |
myScript.PauseVideo(); | |
} | |
if(GUILayout.Button("Restart")) | |
{ | |
myScript.RestartVideo(); | |
} | |
if (myScript.CurrSeek != myScript.LastSeek) | |
{ | |
myScript.SeekVideo(myScript.CurrSeek); | |
myScript.LastSeek = myScript.CurrSeek; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment