using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading; using Assets.Plugins.CommonTool; using Assets.Plugins.Log; using Assets.Plugins.WeMessageService; using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; public class MessageClient : MonoBehaviour { public Text txtHeartbeat; public Text txtSceneTitle; public Text txtSceneDesc; public GameObject VideoObj; public GameObject ImageObj; private VideoPlayer Video1; private Image Image1; private string heartbeat = ""; private List scenes; private List _videos = new List(); private List _images = new List(); private SceneDto scene; void Awake() { MessageClientManager.Instance.ReceiveHeartbeat +=R_Heartbeat; MessageClientManager.Instance.ReceiveScene += R_NewScene; //MessageClientManager.Instance.RegisterClient(); } private string _baseUrl = "http://shvber.com:8888"; private string _baseUrl2 = "http://shvber.com:5028"; private bool IsPlaying => VideoObj.activeSelf || ImageObj.activeSelf; // Start is called before the first frame update void Start() { scenes = new List(); Video1 = VideoObj.GetComponent(); Video1.source = VideoSource.Url; Image1 = ImageObj.GetComponent(); VideoObj.SetActive(false); ImageObj.SetActive(false); LogHelper.LogInfo("¿ªÊ¼"); StartCoroutine(VideoStop()); StartCoroutine(GetScene()); StartCoroutine(VideoPlay()); } // Update is called once per frame void Update() { if (txtHeartbeat != null) { txtHeartbeat.text = heartbeat; } if (scene == null) { return; } if ( txtSceneTitle != null ) { txtSceneTitle.text = scene.Name; txtSceneDesc.text = scene.Description; } if (scene.Attaches != null && !IsPlaying) { _images = scene.Attaches.Where(a => a.FileType == "image").ToList(); _videos = scene.Attaches.Where(a => a.FileType == "video").ToList(); } scene = null; } void R_Heartbeat(string text) { heartbeat = text; } void R_NewScene(WeMessage msg) { LogHelper.LogInfo(msg.Content); var sceneDto = msg.Content.Str2Obj(); var url = _baseUrl + "/api/services/app/query/GetSceneInfo"; url += "?no=" + msg.RunningId; url += "&sceneNo=" + sceneDto.No; var result= url.RequestPost(""); var newScene = result.Str2Obj(); if (newScene != null) { scenes.Add(newScene); } } private int total = 60 * 30; IEnumerator GetScene() { while (true) { if (scenes.Any()) { if (IsPlaying) { while (IsPlaying) { yield return new WaitForSeconds(0.5f); } } scene = scenes.FirstOrDefault(); scenes.Remove(scene); } else { scene = null; } yield return new WaitForSeconds(5f); } // ReSharper disable once IteratorNeverReturns } IEnumerator VideoPlay() { while (true) { if (!VideoObj.activeSelf && _videos.Any()) { var v = _videos.FirstOrDefault(); if (v != null) { var url = _baseUrl2 + v.FilePath; _videos.Remove(v); VideoObj.SetActive(true); Video1.url = url; } } if (!ImageObj.activeSelf && _images.Any()) { var v = _images.FirstOrDefault(); if (v != null) { var url = _baseUrl2 + v.FilePath; _images.Remove(v); AsyncImageDownload.Instance.SetAsyncImage(url, ImageObj); ImageObj.SetActive(true); } } yield return new WaitForSeconds(0.5f); } } IEnumerator VideoStop() { int i1 = 0; //int i2 = 0; while (true) { if (Video1.frame>0 && Video1.frame + 1 == (long) Video1.frameCount) { Video1.Stop(); //Video1.url = ""; VideoObj.SetActive(false); } if (ImageObj.activeSelf) { i1++; if (i1 >= total) { i1 = 0; ImageObj.SetActive(false); } } yield return 0; } } private void OnApplicationQuit() { StopAllCoroutines(); } } public class SceneDto { public string No { get; set; } public string Name { get; set; } public string Description { get; set; } public string Path { get; set; } public List Attaches { get; set; } } public class CampAttachDto { public string AttachNo { get; set; } public string CampNo { get; set; } public string FileType { get; set; } public string FileTitle { get; set; } public string FileExt { get; set; } public string FileName { get; set; } public string FilePath { get; set; } }