123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Net.Http;
- using LitJson;
- using Paroxe.PdfRenderer;
- using UnityEngine;
- using UnityEngine.Experimental.GlobalIllumination;
- using UnityEngine.UI;
- using UnityEngine.Video;
- using XRTool.Util;
- public class RoomFile : UnitySingleton<RoomFile>
- {
- public Button openFileListBtn;
- public GameObject fileListView;
- public Button clearAllBtn;
- public Button deleteFileBtn;
- public Button downLoadFileBtn;
- public Button returnBtn;
- public GameObject msgRemind;
- //public Text msgRemindText;
- //public Text showfileCountText;
- public Text fileNameText;
- public Texture2D defaulttex;
- public RawImage showFileTex;
- public RectTransform defaulttexrect;
- public RawImage showVideoTex;
- public VideoPlayer videoPlayer;
- public RectTransform videorect;
- public PDFViewer pdfview;
- public GameObject[] ShowFileViewList;
-
- public static int fileUpdataCount = 0;
- public Action<int> UpdataCountAction;
- private Action ClearAllfileAction;
- public static Action<FileConfig> ClickFileItemAction;
- public static Action<FileConfig> IfFilePrefabConfigListAction;//点击知道是否实例化的监听
- public static Action<FileConfig> ClickOnPrefabAction;
- public static Action<FileConfig> HideChooseKuangAction;
- public static Action<FileConfig> HidefileChooseAction;
- private FileConfig downLoadConfig;
- protected override void Awake()
- {
- base.Awake();
- WSHandler.Rtc.OnReceiveFile += ReceiveFile;
- UpdataCountAction += OnUpdataCount;
- WSHandler.Rtc.OnClearFile += ClearFile;
- WSHandler.Rtc.OnCoordinate += Coordinate;
- WSHandler.Rtc.OnGetuploadCert += GetuploadCert;
- WSHandler.Rtc.OnSendFile += SendFileSucess;
- ClickFileItemAction += ClickFileItme;
- openFileListBtn.onClick.AddListener(ClickOnOpenFileListBtn);
- clearAllBtn.onClick.AddListener(ClickOnClearAllBtn);
- returnBtn.onClick.AddListener(ClickOnReturnBtn);
- ClearAllfileAction += ClearAllfile;
- WSHandler.Rtc.OnClearFileSucess += ClearFileSucess;
- downLoadFileBtn.onClick.AddListener(DownLoadFile);
- }
- private void ClickOnReturnBtn()
- {
- fileListView.gameObject.SetActive(false);
- }
- private void SendFileSucess(JsonData data)
- {
- FileConfig fileConfig = new FileConfig();
- fileConfig.PeerId = data["data"]["data"]["peerId"].ToString();
- fileConfig.From = data["data"]["data"]["from"].ToString();
- fileConfig.CreateTime = data["data"]["data"]["createTime"].ToString();
- fileConfig.Url = data["data"]["data"]["url"].ToString();
- fileConfig.Path = data["data"]["data"]["path"].ToString();
- fileConfig.FileName = data["data"]["data"]["fileName"].ToString();
- InstantFileConfig(fileConfig);
- }
- private void GetuploadCert(JsonData data)
- {
- if (data["data"]["code"].ToString() == "200")
- {
- //string path = "C:/Users/peitengfei/Desktop/tupian_1.png";
- string cert = data["data"]["data"]["cert"].ToString();
- if (test.path != "")
- {
- NetWorkHeaders.Instance.Upload(cert, test.path, UploadResult);
- }
- if (SaveShot.bytes != null)
- {
- NetWorkHeaders.Instance.Upload(cert, SaveShot.bytes, UploadResult);
- }
- }
- }
- private void UploadResult(string result)
- {
- Debug.Log("上传" + result);
- JsonData data = null;
- string code = "";
- try
- {
- data = JsonMapper.ToObject(result);
- code = data["code"].ToString();
- }
- catch (Exception e)
- {
- Debug.LogError(e.Data);
- return;
- }
- switch (code)
- {
- case "200":
- string url = data["data"]["url"].ToString();
- string path = data["data"]["path"].ToString();
- string fileName = data["data"]["fileName"].ToString();
- WSHandler.Rtc.sendFile(url, path, fileName);
- break;
- case "6007":
- TipMgr.Instance.ShowTip("文件不合法");
- break;
- }
- }
- private void OnUpdataCount(int num)
- {
- msgRemind.SetActive(true);
- //msgRemindText.text = num.ToString();
- }
- private void ReceiveFile(JsonData data)
- {
- FileConfig fileConfig = new FileConfig();
- fileConfig.PeerId = data["data"]["peerId"].ToString();
- fileConfig.From = data["data"]["from"].ToString();
- fileConfig.CreateTime = data["data"]["createTime"].ToString();
- fileConfig.Url = data["data"]["url"].ToString();
- fileConfig.Path = data["data"]["path"].ToString();
- fileConfig.FileName = data["data"]["fileName"].ToString();
- InstantFileConfig(fileConfig);
- }
- private void InstantFileConfig(FileConfig fileConfig)
- {
- if (FileList.Instance)
- {
- FileList.Instance.AddItem(fileConfig);
- }
- }
- public void Init()
- {
- fileListView.gameObject.SetActive(false);
- if (FileList.Instance)
- {
- fileUpdataCount = 0;
- msgRemind.SetActive(false);
- //Debug.Log(RemoteRtc.Instance.fileConfiglist + "BBB");
- FileList.Instance.Init(RemoteRtc.Instance.fileConfiglist);
- }
- HideFile();
- fileNameText.gameObject.SetActive(false);
- clearAllBtn.gameObject.SetActive(false);
- deleteFileBtn.gameObject.SetActive(false);
- downLoadFileBtn.gameObject.SetActive(false);
- }
- public void OpenInit()
- {
- fileUpdataCount = 0;
- msgRemind.SetActive(false);
- }
- public void ShowClearBtn()
- {
- clearAllBtn.gameObject.SetActive(true);
- }
- public void HideClearBtn()
- {
- clearAllBtn.gameObject.SetActive(false);
- }
- private void ClickOnOpenFileListBtn()
- {
- fileListView.gameObject.SetActive(true);
- OpenInit();
- }
- private void ClickOnClearAllBtn()
- {
- // PopForms.Instance.ShowPublic(PopType.PopTwo, RtcStrConfig.clearallFile, "确定", ClearAllfileAction);
- ClearAllfile();
- deleteFileBtn.gameObject.SetActive(false);
- fileNameText.text = "";
- downLoadFileBtn.gameObject.SetActive(false);
- }
- private void ClearAllfile()
- {
- HideClearBtn();
- WSHandler.Rtc.clearFile();
- }
- private void ClearFileSucess(JsonData data)
- {
- if (data["data"]["code"].ToString() == "200")
- {
- ClearAllFileItem();
- }
- }
- private void ClearFile(JsonData data)
- {
- if (data["method"].ToString() == "clearFile")
- {
- ClearAllFileItem();
- }
- }
- public void ClearAllFileItem()
- {
- if (RemoteRtc.Instance && RemoteRtc.Instance.fileConfiglist != null && RemoteRtc.Instance.fileConfiglist.Count > 0)
- {
- RemoteRtc.Instance.fileConfiglist.Clear();
- }
- if (FileList.Instance)
- {
- msgRemind.SetActive(false);
- fileUpdataCount = 0;
- FileList.Instance.DestoryAll();
- }
- ClearAllFilePrefab();
- }
- public void ClearAllFilePrefab()
- {
- HideFile();
- }
- private void ClickFileItme(FileConfig fileConfig)
- {
- if (fileConfig != null)
- {
- fileNameText.gameObject.SetActive(true);
- fileNameText.text = fileConfig.FileName;
-
- if (UserInfo.activateType == 2 || UserInfo.activateType == 3)
- {
- downLoadFileBtn.gameObject.SetActive(true);
- //downLoadFileBtn.onClick.RemoveAllListeners();
- //downLoadFileBtn.onClick.AddListener()
- //
- }
- if (RoomMainInfo.isCreator == "0")
- {
- deleteFileBtn.gameObject.SetActive(true);
- deleteFileBtn.onClick.RemoveAllListeners();
- deleteFileBtn.onClick.AddListener(() => ClickOnDeleteBtn(fileConfig.Path));
- }
- switch (fileConfig.FileType)
- {
- case RoomFileType.png:
- ShowFile(0);
- ShowImage(fileConfig);
- break;
- case RoomFileType.jpg:
- ShowFile(0);
- ShowImage(fileConfig);
- break;
- case RoomFileType.jpeg:
- ShowFile(0);
- ShowImage(fileConfig);
- break;
- case RoomFileType.mp4:
- ShowFile(1);
- ShowMp4(fileConfig);
- break;
- case RoomFileType.pdf:
- ShowFile(2);
- ShowPdf(fileConfig);
- break;
- case RoomFileType.zip:
- ShowFile(3);
- break;
- }
- downLoadConfig = fileConfig;
-
- }
- }
- public void ShowFile(int index)
- {
- for (int i = 0; i < ShowFileViewList.Length; i++)
- {
- if (index == i)
- {
- if (i == 0)
- {
- showFileTex.texture = defaulttex;
- }
- ShowFileViewList[i].SetActive(true);
- }
- else
- {
- ShowFileViewList[i].SetActive(false);
- }
- }
- }
- public void HideFile()
- {
- for (int i = 0; i < ShowFileViewList.Length; i++)
- {
- ShowFileViewList[i].SetActive(false);
- }
- showFileTex.texture = defaulttex;
- }
- private void ClickOnDeleteBtn(string path)
- {
- if (!string.IsNullOrEmpty(path))
- {
- deleteFileBtn.gameObject.SetActive(false);
- WSHandler.Rtc.deleteFile(path);
- }
- }
- public void DeleteSucess(FileConfig fileConfig)
- {
- HideFile();
- switch (fileConfig.FileType)
- {
- case RoomFileType.zip:
- break;
- }
- }
- private void Coordinate(JsonData data)
- {
- string action = data["data"]["action"].ToString();
- switch (action)
- {
- case "10001":
- if (RemoteRtc.Instance)
- {
- float Valuex = float.Parse(data["data"]["params"]["x"].ToString());
- float Valuey = float.Parse(data["data"]["params"]["y"].ToString());
- RoomMainForms.Instance.webTex.autoFocusPoint = new Vector2(Valuex, 1 - Valuey);
- //TextMesh textMesh = obj.transform.Find("Text").GetComponent<TextMesh>();
- //textMesh.text = "**" + RemoteRtc.Instance.webTex.autoFocusPoint;
- }
- break;
- }
- }
- public void ShowImage(FileConfig fileConfig)
- {
- if (!string.IsNullOrEmpty(fileConfig.Url))
- {
- string url = fileConfig.Url;
- if (!fileConfig.Url.Contains("http"))
- {
- url = "https://" + fileConfig.Url;
- }
- NetWorkHeaders.Instance.getNetTexture(url, null, (Texture tex) => {
- if (tex)
- {
- Adaption(tex, defaulttexrect);
- showFileTex.texture = tex;
- }
- });
- }
- }
- public void ShowMp4(FileConfig fileConfig)
- {
- if (!string.IsNullOrEmpty(fileConfig.Url))
- {
- string url = fileConfig.Url;
- if (!fileConfig.Url.Contains("http"))
- {
- url = "https://" + fileConfig.Url;
- }
- videoPlayer.url = url;
- StartCoroutine(enumerator(videoPlayer, videorect));
- }
- }
- public void ShowPdf(FileConfig fileConfig)
- {
- if (!string.IsNullOrEmpty(fileConfig.Url))
- {
- string url = fileConfig.Url;
- if (!fileConfig.Url.Contains("http"))
- {
- url = "https://" + fileConfig.Url;
- }
- pdfview.FileURL = url;
- pdfview.LoadDocument();
- }
- }
- private void Adaption(Texture tex, RectTransform rectTransform)
- {
- float standard_width = 1920f;
- float standard_height = 1080f;
- float video_width = tex.width;
- float video_height = tex.height;
- //Debug.Log(tex.width + "***" + tex.height);
- if (standard_width <= video_width && standard_height > video_height)
- {
- float video_aspect = standard_width / video_width;
- rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
- }
- else if (standard_width > video_width && standard_height <= video_height)
- {
- float video_aspect = standard_height / video_height;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
- }
- else if (standard_width > video_width && standard_height > video_height)
- {
- if (standard_width / video_width > standard_height / video_height)
- {
- float video_aspect = standard_height / video_height;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- }
- else
- {
- float video_aspect = standard_width / video_width;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- }
- }
- else
- {
- if (standard_width / video_width > standard_height / video_height)
- {
- float video_aspect = standard_height / video_height;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- }
- else
- {
- float video_aspect = standard_width / video_width;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- }
- }
- }
- private IEnumerator enumerator(VideoPlayer videoPlayer, RectTransform rectTransform)
- {
- while (!videoPlayer.isPrepared)
- {
- yield return null;
- }
- float standard_width = 1920f;
- float standard_height = 1080f;
- float video_width = videoPlayer.texture.width;
- float video_height = videoPlayer.texture.height;
- //Debug.Log(video_width + "***" + video_height);
- if (standard_width <= video_width && standard_height > video_height)
- {
- float video_aspect = standard_width / video_width;
- rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
- }
- else if (standard_width > video_width && standard_height <= video_height)
- {
- float video_aspect = standard_height / video_height;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
- }
- else if (standard_width > video_width && standard_height > video_height)
- {
- if (standard_width / video_width > standard_height / video_height)
- {
- float video_aspect = standard_height / video_height;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- }
- else
- {
- float video_aspect = standard_width / video_width;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- }
- }
- else
- {
- if (standard_width / video_width > standard_height / video_height)
- {
- float video_aspect = standard_height / video_height;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- //Debug.Log("BBB" + video_aspect + "///" + video_width * video_aspect + "***" + video_height * video_aspect);
- }
- else
- {
- float video_aspect = standard_width / video_width;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- //Debug.Log("CCC" + video_aspect + "///" + video_width * video_aspect + "***" + video_height * video_aspect);
- }
- }
- }
- protected override void OnDestroy()
- {
- ClickFileItemAction -= ClickFileItme;
- WSHandler.Rtc.OnReceiveFile -= ReceiveFile;
- UpdataCountAction -= OnUpdataCount;
- ClearAllfileAction -= ClearAllfile;
- WSHandler.Rtc.OnClearFileSucess -= ClearFileSucess;
- WSHandler.Rtc.OnClearFile -= ClearFile;
- WSHandler.Rtc.OnCoordinate -= Coordinate;
- WSHandler.Rtc.OnGetuploadCert -= GetuploadCert;
- WSHandler.Rtc.OnSendFile -= SendFileSucess;
- }
- private void DownLoadFile()
- {
- if (downLoadConfig == null)
- return;
- string downloadUrl = "https://" + downLoadConfig.Url;
- // string fileName = Application.persistentDataPath + "/DownLoad/" + fileConfig.FileName;
- StartCoroutine(DownLoadFileCorutine(downloadUrl, downLoadConfig.FileName));
- }
- private IEnumerator DownLoadFileCorutine(string downloadUrl, string fileName)
- {
- string filePath = GetFilePath(fileName);
- filePath = "/storage/emulated/0/DownLoad/" + fileName;
- Debug.Log("DownLoadFileCorutine " + filePath);
- Debug.Log("Application.persistentDataPath " + Application.persistentDataPath);
- using (WWW www = new WWW(downloadUrl))
- {
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- // 下载成功,保存文件到本地
- File.WriteAllBytes(filePath, www.bytes);
- Debug.Log("File downloaded to: " + filePath);
- }
- else
- {
- Debug.Log("Download failed: " + www.error);
- }
- }
- }
- private string GetFilePath(string fileName)
- {
- string downloadPath = Path.Combine(Application.persistentDataPath, "DownLoad");
- // 检测文件夹是否存在
- if (!Directory.Exists(downloadPath))
- {
- // 文件夹不存在,创建一个新的文件夹
- Directory.CreateDirectory(downloadPath);
-
- }
- string filePath = Path.Combine(downloadPath, fileName);
- return filePath;
- }
- }
|