133 lines
2.9 KiB
C#
133 lines
2.9 KiB
C#
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.Video;
|
||
|
|
using System.Collections;
|
||
|
|
|
||
|
|
public class VideoAutoVisibilityController : MonoBehaviour
|
||
|
|
{
|
||
|
|
[SerializeField] private GameObject targetObject;
|
||
|
|
[SerializeField] private VideoPlayer videoPlayer;
|
||
|
|
[SerializeField] private bool isVisibleOnStart = true;
|
||
|
|
[SerializeField] private float hideDelaySeconds = 3f;
|
||
|
|
|
||
|
|
private bool isVideoPlaying;
|
||
|
|
private bool isLooking;
|
||
|
|
private Coroutine hideRoutine;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
if (targetObject != null)
|
||
|
|
{
|
||
|
|
targetObject.SetActive(isVisibleOnStart);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Start()
|
||
|
|
{
|
||
|
|
if (videoPlayer != null)
|
||
|
|
{
|
||
|
|
isVideoPlaying = videoPlayer.isPlaying;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnEnable()
|
||
|
|
{
|
||
|
|
if (videoPlayer == null)
|
||
|
|
return;
|
||
|
|
|
||
|
|
videoPlayer.started += OnVideoStarted;
|
||
|
|
videoPlayer.loopPointReached += OnVideoEnded;
|
||
|
|
videoPlayer.errorReceived += OnVideoError;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnDisable()
|
||
|
|
{
|
||
|
|
if (videoPlayer != null)
|
||
|
|
{
|
||
|
|
videoPlayer.started -= OnVideoStarted;
|
||
|
|
videoPlayer.loopPointReached -= OnVideoEnded;
|
||
|
|
videoPlayer.errorReceived -= OnVideoError;
|
||
|
|
}
|
||
|
|
|
||
|
|
StopHideRoutine();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Show()
|
||
|
|
{
|
||
|
|
isLooking = true;
|
||
|
|
StopHideRoutine();
|
||
|
|
|
||
|
|
if (targetObject != null && !targetObject.activeSelf)
|
||
|
|
{
|
||
|
|
targetObject.SetActive(true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void HideDelayed()
|
||
|
|
{
|
||
|
|
isLooking = false;
|
||
|
|
|
||
|
|
if (isVideoPlaying)
|
||
|
|
return;
|
||
|
|
|
||
|
|
ScheduleHide();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnVideoStarted(VideoPlayer vp)
|
||
|
|
{
|
||
|
|
isVideoPlaying = true;
|
||
|
|
StopHideRoutine();
|
||
|
|
|
||
|
|
if (targetObject != null && !targetObject.activeSelf)
|
||
|
|
{
|
||
|
|
targetObject.SetActive(true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnVideoEnded(VideoPlayer vp)
|
||
|
|
{
|
||
|
|
isVideoPlaying = false;
|
||
|
|
ScheduleHide();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnVideoError(VideoPlayer vp, string message)
|
||
|
|
{
|
||
|
|
isVideoPlaying = false;
|
||
|
|
ScheduleHide();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnVideoStopped()
|
||
|
|
{
|
||
|
|
isVideoPlaying = false;
|
||
|
|
ScheduleHide();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ScheduleHide()
|
||
|
|
{
|
||
|
|
if (targetObject == null)
|
||
|
|
return;
|
||
|
|
|
||
|
|
StopHideRoutine();
|
||
|
|
hideRoutine = StartCoroutine(HideAfterDelay());
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator HideAfterDelay()
|
||
|
|
{
|
||
|
|
yield return new WaitForSeconds(hideDelaySeconds);
|
||
|
|
|
||
|
|
if (!isVideoPlaying)
|
||
|
|
{
|
||
|
|
targetObject.SetActive(false);
|
||
|
|
}
|
||
|
|
|
||
|
|
hideRoutine = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void StopHideRoutine()
|
||
|
|
{
|
||
|
|
if (hideRoutine != null)
|
||
|
|
{
|
||
|
|
StopCoroutine(hideRoutine);
|
||
|
|
hideRoutine = null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|