87 lines
2.4 KiB
C#
87 lines
2.4 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.Video;
|
||
|
||
public class VideoUtility : MonoBehaviour
|
||
{
|
||
const string back = "Default", front = "UI";
|
||
[SerializeField] private VideoPlayer player;
|
||
[SerializeField] private AudioSource audioSource;
|
||
[SerializeField] private VideoClip fon, ads, ending;
|
||
[SerializeField] private Canvas canvas;
|
||
|
||
public static float adsLength => (float)instance.ads.length;
|
||
public static float victoryLength => (float)instance.ending.length;
|
||
|
||
private static VideoUtility instance;
|
||
public void Initialize()
|
||
{
|
||
if (instance == null) instance = this;
|
||
PlayFon();
|
||
}
|
||
|
||
public static void PlayFon()
|
||
{
|
||
instance.canvas.sortingLayerName = back;
|
||
instance.player.isLooping = true;
|
||
instance.player.clip = instance.fon;
|
||
|
||
}
|
||
public static void PlayAds()
|
||
{
|
||
var v = instance;
|
||
v.canvas.sortingLayerName = front;
|
||
|
||
v.player.Stop();
|
||
v.player.isLooping = false;
|
||
v.player.clip = v.ads;
|
||
|
||
// ВАЖНО: убедимся, что аудио трек включён и привязан к источнику
|
||
v.player.audioOutputMode = VideoAudioOutputMode.AudioSource;
|
||
v.player.EnableAudioTrack(0, true);
|
||
v.player.SetTargetAudioSource(0, v.audioSource);
|
||
v.audioSource.Stop();
|
||
|
||
v.StartCoroutine(v.PrepareAndPlay());
|
||
|
||
|
||
}
|
||
private IEnumerator PrepareAndPlay()
|
||
{
|
||
player.Prepare();
|
||
while (!player.isPrepared) yield return null;
|
||
|
||
// Для AudioSource-режима часто помогает запускать оба
|
||
audioSource.Play();
|
||
player.Play();
|
||
}
|
||
public static void PlayEnding()
|
||
{
|
||
var v = instance;
|
||
v.canvas.sortingLayerName = front;
|
||
|
||
v.player.Stop();
|
||
v.player.isLooping = false;
|
||
v.player.clip = v.ending;
|
||
|
||
// Те же аудио-настройки, что и в PlayAds
|
||
v.player.audioOutputMode = VideoAudioOutputMode.AudioSource;
|
||
v.player.EnableAudioTrack(0, true);
|
||
v.player.SetTargetAudioSource(0, v.audioSource);
|
||
v.audioSource.Stop();
|
||
|
||
v.StartCoroutine(v.PrepareAndPlay());
|
||
|
||
}
|
||
|
||
|
||
private void Update()
|
||
{
|
||
if (Input.GetKeyDown(KeyCode.Alpha5)) PlayFon();
|
||
if (Input.GetKeyDown(KeyCode.Alpha6)) PlayAds();
|
||
if (Input.GetKeyDown(KeyCode.Alpha7)) PlayEnding();
|
||
}
|
||
|
||
}
|