VideoViewerManager.cs
3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine.Video;
/**
* 功能描述: 视频查看组件
* @author wangQc
* @date 2025/6/4 09:17
*/
public class VideoViewerManager : MonoBehaviour
{
public static VideoViewerManager Instance;
public GameObject totalPanel;
public GameObject videoPanel;
public Image videoPlayImage;
public Image backImage;
public DanmakuManager danmakuManager;
public Image likeHeadImage;
public Text likeNameText;
public Image headImage;
public TextMeshProUGUI nickname;
public TextMeshProUGUI followCountText;
public TextMeshProUGUI contentText;
private VideoPlayer videoPlayer;
void Awake()
{
Instance = this;
totalPanel.SetActive(false);
videoPlayImage.gameObject.SetActive(false);
var videoPlayImageBtn = videoPlayImage.GetComponent<Button>();
videoPlayImageBtn.onClick.AddListener(() =>
{
videoPlayImage.gameObject.SetActive(false);
videoPlayer.Play();
});
videoPlayer = videoPanel.GetComponentInChildren<VideoPlayer>();
var videoPanelBtn = videoPanel.GetComponent<Button>();
videoPanelBtn.onClick.AddListener(() =>
{
if (videoPlayer.isPlaying)
{
videoPlayer.Pause();
//显示播放图标
videoPlayImage.gameObject.SetActive(true);
}
else
{
videoPlayer.Play();
//隐藏播放图标
videoPlayImage.gameObject.SetActive(false);
}
});
var backBtn = backImage.GetComponent<Button>();
backBtn.onClick.AddListener(Hide);
}
public void ShowVideo(Moment moment)
{
totalPanel.SetActive(true);
//设置视频源
videoPlayer.source = VideoSource.Url;
videoPlayer.url = moment.video;
//播放
videoPlayer.Play();
//发送弹幕(横屏)
danmakuManager.AddDanmakuBatch(moment.videoDanmakuList,2f,1);
//视频点赞人头像
StartCoroutine(ImageLoader.LoadImage(moment.videoLikeHeadImg, likeHeadImage));
//视频点赞人列表
if (moment.likeNameList != null && moment.likeNameList.Count > 0)
{
likeNameText.text = $"{moment.likeNameList[0]}等{moment.likeNameList.Count}个朋友 ❤";
}
else
{
likeNameText.text = ""; // 或者设为默认值,比如 "暂无点赞"
}
//视屏号头像、昵称
StartCoroutine(ImageLoader.LoadImage(moment.avatarUrl, headImage));
nickname.text = moment.username;
followCountText.text = $"{moment.videoFollowCount}个朋友关注";
contentText.text = moment.text;
}
public void Hide()
{
totalPanel.SetActive(false);
videoPlayImage.gameObject.SetActive(false);
RenderTexture rt = videoPlayer.targetTexture;
RenderTexture.active = rt;
GL.Clear(true, true, Color.clear);
//清除弹幕
danmakuManager.ClearDanmaku();
}
}