NewN_UAVPlane/Assets/Zion/Scripts/Sync1/FunctionSync_Media.cs

255 lines
7.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using System.Text;
using System;
/// <summary>
/// 多媒体同步适用于图片ppt视频集成在一个材质上
/// </summary>
public class FunctionSync_Media : MonoBehaviour
{
public static Dictionary<string, FunctionSync_Media> functionSync_MediaDic = new Dictionary<string, FunctionSync_Media>();
/// <summary>
/// type编号如果为ppt索引
/// </summary>
public string Id;
private VideoPlayer videoPlayer;
private Renderer renderer;
/// <summary>
/// 关闭的texture
/// </summary>
private Texture nullTexture;
/// <summary>
/// 当前视频播放时间
/// </summary>
[HideInInspector]
public double PlayTime;
private int currentTextureNum;
private int currentPPTnum;
[HideInInspector]
public int currentPPTsubNum;
private int currentVideoNum;
[HideInInspector]
public byte videoState;
/// <summary>
/// id流
/// </summary>
private byte[] idbytes;
public void Init()
{
renderer = GetComponent<Renderer>();
videoPlayer = GetComponent<VideoPlayer>();
nullTexture = renderer.material.mainTexture;
Id = "media_" + gameObject.name;
idbytes = Encoding.UTF8.GetBytes(Id);
functionSync_MediaDic.Add(Id, this);
}
float time = 0;
private void Update()
{
//学生回拉,老师不在视频不播放
if (!GameManage.Instance.is单机模式)
{
//if (!LoadManage.Instance.loadPackage.isOwner)
//{
// if (videoPlayer.isPlaying && videoPlayer.time > PlayTime + 1)
// {
// videoPlayer.time = PlayTime + 1;
// }
//}
//else
{
if (videoPlayer.isPlaying && videoState==1)
{
time += Time.deltaTime;
if (time > 1)
{
time = 0;
PlayTime = videoPlayer.time;
//发送时间
SetVideo(currentVideoNum, 1, PlayTime);
}
}
}
}
}
public void SetTexture(int num)
{
List<byte> tmp = new List<byte>();
//id
tmp.AddRange(BitConverter.GetBytes(idbytes.Length));
tmp.AddRange(idbytes);
//图片
tmp.Add(0);
tmp.AddRange(BitConverter.GetBytes(num));
LoadManage.Instance.RSclient.Send(LoadManage.Instance.currentRoomArea, 10010, tmp.ToArray());
}
public void SetPPT(int num,int index)
{
List<byte> tmp = new List<byte>();
//id
tmp.AddRange(BitConverter.GetBytes(idbytes.Length));
tmp.AddRange(idbytes);
//ppt
tmp.Add(2);
tmp.AddRange(BitConverter.GetBytes(num));
tmp.AddRange(BitConverter.GetBytes(index));
LoadManage.Instance.RSclient.Send(LoadManage.Instance.currentRoomArea, 10010, tmp.ToArray());
}
public void SetVideo(int num,byte isOpen,double playtime)
{
List<byte> tmp = new List<byte>();
//id
tmp.AddRange(BitConverter.GetBytes(idbytes.Length));
tmp.AddRange(idbytes);
//视频
tmp.Add(1);
tmp.AddRange(BitConverter.GetBytes(num));
tmp.Add(isOpen);
tmp.AddRange(BitConverter.GetBytes(playtime));
LoadManage.Instance.RSclient.Send(LoadManage.Instance.currentRoomArea, 10010, tmp.ToArray());
}
public void CallBack(byte[] data,bool isJoinRoom=false)
{
int num=BitConverter.ToInt32(data,1+4+idbytes.Length);
if (data[4 + idbytes.Length] == 0)
{
currentTextureNum = num;
//type,num
//图片
videoPlayer.Stop();
//下载
//StartCoroutine(GetTexture(MediaPanel.instance.dicPicture[num].url, (a,tex) =>
//{
// if (a)
// {
// renderer.material.mainTexture = tex;
// }
// else
// {
// Debug.LogError("下载图片失败:" + num);
// }
//}));
}
else if(data[4 + idbytes.Length] == 1)
{
currentVideoNum = num;
videoState = data[5+4+idbytes.Length];
//if (videoPlayer.url != MediaPanel.instance.dicVideo[num].url)
//{
// videoPlayer.url = MediaPanel.instance.dicVideo[num].url;
//}
//type,num,开关,播放时间
//视频
if (data[5 + 4 + idbytes.Length] ==0)
{
//关闭
videoPlayer.Stop();
PlayTime = 0;
}
else if(data[5 + 4 + idbytes.Length] ==1)
{
//开启
videoPlayer.playbackSpeed = 1;
if (!videoPlayer.isPlaying)
{
videoPlayer.Play();
}
PlayTime = videoPlayer.time;
}
else if(data[5 + 4 + idbytes.Length] ==2)
{
//暂停
videoPlayer.playbackSpeed = 0;
PlayTime = videoPlayer.time;
}
if (isJoinRoom)
{
videoPlayer.time = BitConverter.ToDouble(data, 6 + 4 + idbytes.Length);
}
else
{
//if (!LoadManage.Instance.loadPackage.isOwner)
//{
// videoPlayer.time = BitConverter.ToDouble(data, 6 + 4 + idbytes.Length);
//}
}
}
else if(data[4 + idbytes.Length] == 2)
{
currentPPTnum = num;
//type,num,索引
//PPT
int index = BitConverter.ToInt32(data,5 + 4 + idbytes.Length);
currentPPTsubNum = index;
videoPlayer.Stop();
if (index < 0)
{
//关闭ppt
renderer.material.mainTexture = nullTexture;
}
else
{
//无索引
//下载
//StartCoroutine(GetTexture(MediaPanel.instance.dicPPT[num].subTextures[index], (a, tex) =>
//{
// if (a)
// {
// //pptTexDic[num].ppttextureDic.Add(index, tex);
// renderer.material.mainTexture = tex;
// }
// else
// {
// Debug.LogError("下载PPT图片失败" + num);
// }
//}));
}
}
}
/// <summary>
/// 下载图片
/// </summary>
/// <returns></returns>
IEnumerator GetTexture(string url1, Action<bool,Texture> callback)
{
WWW www1 = new WWW(url1);
yield return www1;
if (www1.isDone)
{
if (!string.IsNullOrEmpty(www1.error))
{
Debug.LogError(www1.error);
MessagePanel.ShowMessage("获取图片失败:" + www1.error, GameObject.Find("Canvas").transform, a => { });
callback.Invoke(false,null);
}
else
{
Debug.Log("下载图片成功");
callback.Invoke(true,www1.texture);
}
}
}
}
/// <summary>
/// ppt信息
/// </summary>
public class PPTtextureDate
{
public Dictionary<int, Texture> ppttextureDic = new Dictionary<int, Texture>();
}