Initial commit
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.Video;
|
||||
|
||||
namespace XRMultiplayer
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a RenderTexture for rendering video to a target renderer.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(VideoPlayer))]
|
||||
public class VideoPlayerRenderTexture : MonoBehaviour
|
||||
{
|
||||
const string k_ShaderName = "Unlit/Texture";
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("The target Image which will display the video.")]
|
||||
RawImage m_Image;
|
||||
|
||||
[SerializeField] Vector2 m_ImageSize = new Vector2(1920, 1080);
|
||||
[SerializeField] float m_Scale = 1.0f;
|
||||
[SerializeField] float m_ZOffset = -1;
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("The width of the RenderTexture which will be created.")]
|
||||
int m_RenderTextureWidth = 1920;
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("The height of the RenderTexture which will be created.")]
|
||||
int m_RenderTextureHeight = 1080;
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("The bit depth of the depth channel for the RenderTexture which will be created.")]
|
||||
int m_RenderTextureDepth;
|
||||
|
||||
VideoPlayer m_VideoPlayer;
|
||||
|
||||
|
||||
void OnValidate()
|
||||
{
|
||||
if (m_Image == null) return;
|
||||
m_Image.rectTransform.sizeDelta = m_ImageSize * m_Scale;
|
||||
m_Image.rectTransform.localPosition = new Vector3(m_Image.rectTransform.localPosition.x, m_Image.rectTransform.localPosition.y, m_ZOffset);
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (!TryGetComponent(out m_VideoPlayer))
|
||||
{
|
||||
Utils.Log("VideoPlayerRenderTexture requires a VideoPlayer component.", 2);
|
||||
return;
|
||||
}
|
||||
var renderTexture = new RenderTexture(m_RenderTextureWidth, m_RenderTextureHeight, m_RenderTextureDepth);
|
||||
renderTexture.Create();
|
||||
m_Image.material.mainTexture = renderTexture;
|
||||
m_VideoPlayer.targetTexture = renderTexture;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9f926e9bbc6b3149869d0f7ecdf53a3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,165 @@
|
||||
using System.Collections;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.Video;
|
||||
|
||||
namespace XRMultiplayer
|
||||
{
|
||||
public class VideoPlayerTutorial : MonoBehaviour
|
||||
{
|
||||
[SerializeField] GameObject m_InfoButtonObject;
|
||||
[SerializeField] GameObject m_VideoPlayerObject;
|
||||
[SerializeField] TMP_Dropdown m_Dropdown;
|
||||
[SerializeField] TMP_Text m_HeaderText;
|
||||
[SerializeField] VideoClip[] m_VideoClips;
|
||||
[SerializeField] float m_HideDistance = 10.0f;
|
||||
[SerializeField] bool m_AutoDisplay = false;
|
||||
[SerializeField] CanvasGroup m_InfoButtonFadeGroup;
|
||||
[SerializeField] CanvasGroup m_VideoFadeGroup;
|
||||
[SerializeField] RawImage m_VideoImage;
|
||||
[SerializeField] VideoPlayer m_VideoPlayer;
|
||||
[SerializeField] float m_FadeSpeedVideo = 5.0f;
|
||||
[SerializeField] float m_FadeSpeedButton = 5.0f;
|
||||
|
||||
Camera m_MainCam;
|
||||
|
||||
bool m_IsHidden = false;
|
||||
|
||||
IEnumerator m_FadeEnumerator;
|
||||
|
||||
void Start()
|
||||
{
|
||||
m_MainCam = Camera.main;
|
||||
|
||||
if (m_VideoClips.Length < 2)
|
||||
{
|
||||
m_Dropdown.gameObject.SetActive(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Clear the dropdown options
|
||||
m_Dropdown.ClearOptions();
|
||||
foreach (var c in m_VideoClips)
|
||||
{
|
||||
TMP_Dropdown.OptionData optionData = new TMP_Dropdown.OptionData(c.name);
|
||||
m_Dropdown.options.Add(optionData);
|
||||
}
|
||||
m_Dropdown.onValueChanged.AddListener(PickVideo);
|
||||
}
|
||||
|
||||
Hide();
|
||||
}
|
||||
|
||||
void PickVideo(int index)
|
||||
{
|
||||
m_VideoPlayer.Stop();
|
||||
m_VideoPlayer.clip = m_VideoClips[index];
|
||||
m_HeaderText.text = m_VideoClips[index].name;
|
||||
m_VideoPlayer.targetTexture.Release();
|
||||
m_VideoPlayer.Play();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Vector3.Distance(m_MainCam.transform.position, transform.position) < m_HideDistance)
|
||||
{
|
||||
if (m_IsHidden)
|
||||
{
|
||||
EnableTutorial();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!m_IsHidden)
|
||||
{
|
||||
HideTutorial();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HideTutorial()
|
||||
{
|
||||
m_IsHidden = true;
|
||||
if (m_FadeEnumerator != null) StopCoroutine(m_FadeEnumerator);
|
||||
m_FadeEnumerator = FadeOutRoutine();
|
||||
StartCoroutine(m_FadeEnumerator);
|
||||
}
|
||||
|
||||
void EnableTutorial()
|
||||
{
|
||||
m_IsHidden = false;
|
||||
ToggleVideo(m_AutoDisplay);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called from UI button press.
|
||||
/// </summary>
|
||||
/// <param name="toggle"></param>
|
||||
public void ToggleVideo(bool toggle)
|
||||
{
|
||||
m_InfoButtonObject.SetActive(!toggle);
|
||||
m_VideoPlayerObject.SetActive(toggle);
|
||||
|
||||
if (toggle)
|
||||
{
|
||||
m_VideoFadeGroup.alpha = 0.0f;
|
||||
m_VideoImage.material.color = new Color(1, 1, 1, m_VideoFadeGroup.alpha);
|
||||
m_VideoPlayer.targetTexture.Release();
|
||||
if (m_FadeEnumerator != null) StopCoroutine(m_FadeEnumerator);
|
||||
m_FadeEnumerator = FadeVideoRoutine();
|
||||
StartCoroutine(m_FadeEnumerator);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_InfoButtonFadeGroup.alpha = 0.0f;
|
||||
if (m_FadeEnumerator != null) StopCoroutine(m_FadeEnumerator);
|
||||
m_FadeEnumerator = FadeButtonRoutine();
|
||||
StartCoroutine(m_FadeEnumerator);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator FadeButtonRoutine()
|
||||
{
|
||||
while (m_InfoButtonFadeGroup.alpha < 1.0f)
|
||||
{
|
||||
m_InfoButtonFadeGroup.alpha += Time.deltaTime * m_FadeSpeedButton;
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator FadeVideoRoutine()
|
||||
{
|
||||
while (m_VideoFadeGroup.alpha < 1.0f)
|
||||
{
|
||||
m_VideoFadeGroup.alpha += Time.deltaTime * m_FadeSpeedVideo;
|
||||
m_VideoImage.material.color = new Color(1, 1, 1, m_VideoFadeGroup.alpha);
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator FadeOutRoutine()
|
||||
{
|
||||
while (m_VideoFadeGroup.alpha > 0.0f || m_InfoButtonFadeGroup.alpha > 0.0f)
|
||||
{
|
||||
float fadeAmount = Time.deltaTime * m_FadeSpeedVideo;
|
||||
m_VideoFadeGroup.alpha -= fadeAmount;
|
||||
m_VideoImage.material.color = new Color(1, 1, 1, m_VideoFadeGroup.alpha);
|
||||
|
||||
m_InfoButtonFadeGroup.alpha -= fadeAmount;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
Hide();
|
||||
m_VideoPlayer.targetTexture.Release();
|
||||
|
||||
}
|
||||
|
||||
void Hide()
|
||||
{
|
||||
m_InfoButtonObject.SetActive(false);
|
||||
m_VideoPlayerObject.SetActive(false);
|
||||
m_IsHidden = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fd3e26a6cf901f645a64b02f39f5c2f1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,180 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.Video;
|
||||
|
||||
namespace XRMultiplayer
|
||||
{
|
||||
/// <summary>
|
||||
/// Connects a UI slider control to a video player, allowing users to scrub to a particular time in th video.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(VideoPlayer))]
|
||||
public class VideoTimeScrubControl : MonoBehaviour
|
||||
{
|
||||
private const string TIME_FORMAT = "m':'ss";
|
||||
[SerializeField]
|
||||
[Tooltip("Video play/pause button GameObject")]
|
||||
GameObject m_ButtonPlayOrPause;
|
||||
[SerializeField, Tooltip("Use Play/Pause Butotn")]
|
||||
bool m_UsePlayPauseButton = true;
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("Slider that controls the video")]
|
||||
Slider m_Slider;
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("Play icon sprite")]
|
||||
Sprite m_IconPlay;
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("Pause icon sprite")]
|
||||
Sprite m_IconPause;
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("Play or pause button image.")]
|
||||
Image m_ButtonPlayOrPauseIcon;
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("If checked, the slider will fade off after a few seconds. If unchecked, the slider will remain on.")]
|
||||
bool m_HideSliderAfterFewSeconds;
|
||||
|
||||
[SerializeField, Tooltip("The Video Player Time Text")]
|
||||
TMP_Text m_VideoTimeText;
|
||||
|
||||
bool m_IsDragging;
|
||||
bool m_VideoIsPlaying;
|
||||
bool m_VideoJumpPending;
|
||||
long m_LastFrameBeforeScrub;
|
||||
VideoPlayer m_VideoPlayer;
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (!TryGetComponent(out m_VideoPlayer))
|
||||
{
|
||||
Utils.Log("VideoTimeScrubControl: No VideoPlayer component found on this GameObject.", 2);
|
||||
return;
|
||||
}
|
||||
if (!m_VideoPlayer.playOnAwake)
|
||||
{
|
||||
m_VideoPlayer.playOnAwake = true; // Set play on awake for next enable.
|
||||
m_VideoPlayer.Play(); // Play video to load first frame.
|
||||
VideoStop(); // Stop the video to set correct state and pause frame.
|
||||
}
|
||||
else
|
||||
{
|
||||
VideoPlay(); // Play to ensure correct state.
|
||||
}
|
||||
|
||||
|
||||
if (!m_UsePlayPauseButton && m_ButtonPlayOrPause != null)
|
||||
m_ButtonPlayOrPause.SetActive(false);
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (m_VideoPlayer != null)
|
||||
{
|
||||
m_VideoPlayer.frame = 0;
|
||||
VideoPlay(); // Ensures correct UI state update if paused.
|
||||
}
|
||||
|
||||
m_Slider.value = 0.0f;
|
||||
m_Slider.gameObject.SetActive(true);
|
||||
if (m_HideSliderAfterFewSeconds)
|
||||
StartCoroutine(HideSliderAfterSeconds());
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (m_VideoJumpPending)
|
||||
{
|
||||
// We're trying to jump to a new position, but we're checking to make sure the video player is updated to our new jump frame.
|
||||
if (m_LastFrameBeforeScrub == m_VideoPlayer.frame)
|
||||
return;
|
||||
|
||||
// If the video player has been updated with desired jump frame, reset these values.
|
||||
m_LastFrameBeforeScrub = long.MinValue;
|
||||
m_VideoJumpPending = false;
|
||||
}
|
||||
|
||||
if (!m_IsDragging && !m_VideoJumpPending)
|
||||
{
|
||||
if (m_VideoPlayer.frameCount > 0)
|
||||
{
|
||||
var progress = (float)m_VideoPlayer.frame / m_VideoPlayer.frameCount;
|
||||
m_Slider.value = progress;
|
||||
}
|
||||
}
|
||||
|
||||
TimeSpan elapsedTime = TimeSpan.FromSeconds(m_VideoPlayer.time);
|
||||
TimeSpan totalTime = TimeSpan.FromSeconds(m_VideoPlayer.length);
|
||||
m_VideoTimeText.text = $"{elapsedTime.ToString(TIME_FORMAT)} / {totalTime.ToString(TIME_FORMAT)}";
|
||||
}
|
||||
|
||||
public void OnPointerDown()
|
||||
{
|
||||
m_VideoJumpPending = true;
|
||||
VideoStop();
|
||||
VideoJump();
|
||||
}
|
||||
|
||||
public void OnRelease()
|
||||
{
|
||||
m_IsDragging = false;
|
||||
VideoPlay();
|
||||
VideoJump();
|
||||
}
|
||||
|
||||
IEnumerator HideSliderAfterSeconds(float duration = 1f)
|
||||
{
|
||||
yield return new WaitForSeconds(duration);
|
||||
m_Slider.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public void OnDrag()
|
||||
{
|
||||
m_IsDragging = true;
|
||||
m_VideoJumpPending = true;
|
||||
}
|
||||
|
||||
void VideoJump()
|
||||
{
|
||||
m_VideoJumpPending = true;
|
||||
var frame = m_VideoPlayer.frameCount * m_Slider.value;
|
||||
m_LastFrameBeforeScrub = m_VideoPlayer.frame;
|
||||
m_VideoPlayer.frame = (long)frame;
|
||||
}
|
||||
|
||||
public void PlayOrPauseVideo()
|
||||
{
|
||||
if (m_VideoIsPlaying)
|
||||
{
|
||||
VideoStop();
|
||||
}
|
||||
else
|
||||
{
|
||||
VideoPlay();
|
||||
}
|
||||
}
|
||||
|
||||
void VideoStop()
|
||||
{
|
||||
m_VideoIsPlaying = false;
|
||||
m_VideoPlayer.Pause();
|
||||
m_ButtonPlayOrPauseIcon.sprite = m_IconPlay;
|
||||
if (!m_UsePlayPauseButton && m_ButtonPlayOrPause != null)
|
||||
m_ButtonPlayOrPause.SetActive(true);
|
||||
}
|
||||
|
||||
void VideoPlay()
|
||||
{
|
||||
m_VideoIsPlaying = true;
|
||||
m_VideoPlayer.Play();
|
||||
m_ButtonPlayOrPauseIcon.sprite = m_IconPause;
|
||||
if (!m_UsePlayPauseButton && m_ButtonPlayOrPause != null)
|
||||
m_ButtonPlayOrPause.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ca75f292f7449044807f6ba9e6f954c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user