| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- 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<SceneDto> scenes;
- private List<CampAttachDto> _videos = new List<CampAttachDto>();
- private List<CampAttachDto> _images = new List<CampAttachDto>();
- 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<SceneDto>();
- Video1 = VideoObj.GetComponent<VideoPlayer>();
- Video1.source = VideoSource.Url;
- Image1 = ImageObj.GetComponent<Image>();
- 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<SceneDto>();
- var url = _baseUrl + "/api/services/app/query/GetSceneInfo";
- url += "?no=" + msg.RunningId;
- url += "&sceneNo=" + sceneDto.No;
- var result= url.RequestPost("");
- var newScene = result.Str2Obj<SceneDto>();
- 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<CampAttachDto> 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; }
- }
-
|