174 lines
4.5 KiB
C#
174 lines
4.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using DG.Tweening;
|
|
using UnityEngine.Video;
|
|
|
|
/// <summary>
|
|
/// 视屏播放面板
|
|
/// </summary>
|
|
public class UI_VideoPanel : BasePanel
|
|
{
|
|
public CanvasGroup canvasGroup; //屏幕显示
|
|
private GameObject Player;
|
|
public Button CloseBtn; //外围关闭按钮
|
|
|
|
[Header("播放")]
|
|
public Button UpPauseBtn; //中间暂停按钮
|
|
public Button DownPauseBtn; //下方暂停按钮
|
|
public Slider VolumeSlider; //音量进度条
|
|
public Sprite[] PauseOrPlaySprite;
|
|
public VideoPlayer VideoPlayer; //视屏播放
|
|
|
|
[Header("音量")]
|
|
public Button SourceBtn; //音量Btn
|
|
public AudioSource audioSource;
|
|
public Sprite[] volumeOffOrOn; //声音图标
|
|
|
|
private bool isMuted = false;
|
|
private float lastVolume = 1f;
|
|
private bool isDragging = false;
|
|
//public Button Btn
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
OnInit();
|
|
}
|
|
|
|
public void OnInit()
|
|
{
|
|
canvasGroup = GetComponent<CanvasGroup>();
|
|
canvasGroup.alpha = 0f; // 默认隐藏
|
|
canvasGroup.interactable = false;
|
|
canvasGroup.blocksRaycasts = false;
|
|
// ===== 音频绑定 =====
|
|
VideoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
|
|
VideoPlayer.SetTargetAudioSource(0, audioSource);
|
|
SourceBtn = GetControl<Button>("音量Btn");
|
|
DownPauseBtn = GetControl<Button>("下方暂停Btn");
|
|
|
|
Player = GameObject.FindGameObjectWithTag("Player");
|
|
}
|
|
|
|
public override void ShowMe()
|
|
{
|
|
base.ShowMe();
|
|
FadeIn(0.5f);
|
|
Player = GameObject.FindGameObjectWithTag("Player");
|
|
}
|
|
|
|
public override void HideMe()
|
|
{
|
|
base.HideMe();
|
|
FadeOut(0.5f);
|
|
}
|
|
protected override void OnClick(string btnPath)
|
|
{
|
|
base.OnClick(btnPath);
|
|
switch (btnPath)
|
|
{
|
|
case "CloseBtn":
|
|
FadeOut(0.5f);
|
|
//Player.GetComponent<FirstPersonController>().enabled = true;
|
|
break;
|
|
case "中间暂停按钮":
|
|
Debug.Log("中间暂停按钮");
|
|
PlayOrPause();
|
|
break;
|
|
case "下方暂停Btn":
|
|
Debug.Log("下方暂停Btn");
|
|
PlayOrPause();
|
|
break;
|
|
case "音量Btn":
|
|
Debug.Log("音量Btn");
|
|
if (isMuted)
|
|
{
|
|
VolumeSlider.value = 0;
|
|
}
|
|
else
|
|
{
|
|
VolumeSlider.value = VolumeSlider.maxValue;
|
|
}
|
|
UpdateVolumeIcon();
|
|
break;
|
|
}
|
|
}
|
|
|
|
protected override void OnChangeSlider(string sliderPath, float value)
|
|
{
|
|
base.OnChangeSlider(sliderPath, value);
|
|
switch (sliderPath)
|
|
{
|
|
case "进度条Slider":
|
|
Debug.Log("进度条Slider");
|
|
break;
|
|
case "音量Slider":
|
|
Debug.Log("音量Slider");
|
|
audioSource.volume = value;
|
|
if (value > 0)
|
|
{
|
|
isMuted = false;
|
|
lastVolume = value;
|
|
}
|
|
UpdateVolumeIcon();
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 声音图标(静音)
|
|
/// </summary>
|
|
void UpdateVolumeIcon()
|
|
{
|
|
Image volumeIcon = SourceBtn.GetComponent<Image>();
|
|
volumeIcon.sprite = audioSource.volume == 0 ? volumeOffOrOn[0] : volumeOffOrOn[1];
|
|
}
|
|
|
|
/// <summary>
|
|
/// 下方暂停按钮
|
|
/// </summary>
|
|
public void PlayOrPause()
|
|
{
|
|
if (VideoPlayer.isPlaying)
|
|
{
|
|
VideoPlayer.Pause();
|
|
}
|
|
else
|
|
{
|
|
VideoPlayer.Play();
|
|
}
|
|
|
|
UpdatePlayPauseIcon();
|
|
}
|
|
//暂停图标切换
|
|
void UpdatePlayPauseIcon()
|
|
{
|
|
|
|
Image playPauseIcon = DownPauseBtn.GetComponent<Image>();
|
|
playPauseIcon.sprite = VideoPlayer.isPlaying
|
|
? PauseOrPlaySprite[0] // 正在播放 → 显示“暂停”
|
|
: PauseOrPlaySprite[1]; // 已暂停 → 显示“播放”
|
|
}
|
|
|
|
/// <summary>
|
|
/// 渐显
|
|
/// </summary>
|
|
public void FadeIn(float time)
|
|
{
|
|
canvasGroup.DOFade(1f, time);
|
|
canvasGroup.interactable = true;
|
|
canvasGroup.blocksRaycasts = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 渐隐
|
|
/// </summary>
|
|
public void FadeOut(float time)
|
|
{
|
|
canvasGroup.DOFade(0f, time);
|
|
canvasGroup.interactable = false;
|
|
canvasGroup.blocksRaycasts = false;
|
|
}
|
|
}
|