VideoViewerManager.cs 3.01 KB
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 TextMeshProUGUI 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);
        
        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,0.4f,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();
    }
}