DemoManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using easyar;
  6. using TMPro;
  7. using UnityEngine;
  8. using UnityEngine.Networking;
  9. using XRTool.Util;
  10. using static easyar.ImageTargetController;
  11. public class DemoManager : MonoBehaviour
  12. {
  13. public TextMeshProUGUI timetext;
  14. public bool isinit;
  15. public bool isDianbiao;
  16. public GameObject AISelect;
  17. private void OnEnable()
  18. {
  19. AISelect.SetActive(true);
  20. isDianbiao = false;
  21. }
  22. public void jixushibie()
  23. {
  24. StartCoroutine(DownloadImage("https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/MREdustry_100/DemoShiBieTu.jpg"));
  25. ChangeCameraSaoMiao.Instance.showDemoSaoMiao();
  26. imageTarget.SetActive(false);
  27. shibieGo.SetActive(false);
  28. TimerMgr.Instance.CreateTimer(() => {
  29. imageTarget.SetActive(true);
  30. }, 10f);
  31. }
  32. public void tuichushibie()
  33. {
  34. WindowsManager.Instance.show(WindowConfig.windowType.ProjectMain);
  35. WindowsManager.Instance.show(WindowConfig.windowType.Top, false);
  36. }
  37. public GameObject shibieGo;
  38. private void Update()
  39. {
  40. if(im&& isDianbiao)
  41. {
  42. if (imageTarget.activeSelf)
  43. {
  44. if (im)
  45. {
  46. Destroy(im);
  47. im = null;
  48. shibieGo.SetActive(true);
  49. timetext.text = DateTime.Now.ToString("yyyy 年 M 月 d 日 HH:mm");
  50. ChangeCameraSaoMiao.Instance.close();
  51. }
  52. }
  53. }
  54. }
  55. // 协程方法,用于异步下载图片
  56. IEnumerator DownloadImage(string url)
  57. {
  58. // 使用UnityWebRequestTexture来获取图片纹理
  59. UnityWebRequest m_webrequest = UnityWebRequest.Get(url);
  60. yield return m_webrequest.SendWebRequest();
  61. // 检查下载是否成功
  62. if (m_webrequest.result != UnityWebRequest.Result.Success)
  63. {
  64. // 打印错误信息
  65. Debug.LogError("Failed to download image");
  66. }
  67. else
  68. {
  69. string giftopngpath = Application.persistentDataPath + "/DownLoadXR/DemoShiBieTu.png";
  70. // 从下载处理器获取纹理
  71. Texture2D tex = new Texture2D(1, 1);
  72. tex.LoadImage( m_webrequest.downloadHandler.data);
  73. tex.Apply();
  74. byte[] gifbyte = tex.EncodeToPNG();
  75. //文件流信息
  76. //StreamWriter sw;
  77. Stream sw;
  78. FileInfo file = new FileInfo(giftopngpath);
  79. try
  80. {
  81. if (!Directory.Exists(Application.persistentDataPath + "/DownLoadXR"))
  82. {
  83. Directory.CreateDirectory(Application.persistentDataPath + "/DownLoadXR");
  84. }
  85. if (!file.Exists)
  86. {
  87. sw = file.Create();
  88. //如果此文件存在则打开
  89. //sw = file .Append();
  90. //如果此文件不存在则创建
  91. //以行的形式写入信息
  92. //sw.WriteLine(info);
  93. sw.Write(gifbyte, 0, gifbyte.Length);
  94. sw.Close();
  95. sw.Dispose();
  96. }
  97. }
  98. catch
  99. {
  100. }
  101. yield return new WaitForSeconds(1f) ;
  102. ImageFileSourceData data = new ImageFileSourceData();
  103. data.Name = "DemoShiBieTu";
  104. data.Path = giftopngpath;
  105. data.PathType = PathType.Absolute;
  106. data.Scale = 0.1f;
  107. im = imageTarget.AddComponent<ImageTargetController>();
  108. im.ImageFileSource = data;
  109. ARSaoTuManager.Instance.imageOnFind.LoadTarget(im);
  110. ARSaoTuManager.Instance.cameraDevice.Open();
  111. }
  112. }
  113. ImageTargetController im;
  114. public GameObject imageTarget;
  115. public GameObject deviceGo;
  116. private void OnDisable()
  117. {
  118. if (im)
  119. {
  120. Destroy(im);
  121. im = null;
  122. }
  123. shibieGo.SetActive(false);
  124. deviceGo.SetActive(false);
  125. }
  126. public void SeleckDianBiao()
  127. {
  128. isDianbiao = true;
  129. AISelect.SetActive(false);
  130. if (ChangeCameraSaoMiao.Instance)
  131. {
  132. StartCoroutine(DownloadImage("https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/MREdustry_100/DemoShiBieTu.jpg"));
  133. ChangeCameraSaoMiao.Instance.showDemoSaoMiao();
  134. imageTarget.SetActive(false);
  135. TimerMgr.Instance.CreateTimer(() => {
  136. imageTarget.SetActive(true);
  137. }, 10f);
  138. }
  139. }
  140. public void SeleckDevice()
  141. {
  142. //ShowInfoTipManager.Instance.showTip("暂未开放,敬请期待");
  143. //TimerMgr.Instance.CreateTimer(() => { ShowInfoTipManager.Instance.closeTip(); }, 2f);
  144. isDianbiao = false;
  145. AISelect.SetActive(false);
  146. deviceGo.gameObject.SetActive(true);
  147. TopManager.Instance.AIExitIconState(true);
  148. // 设备识别
  149. }
  150. public void GoHome()
  151. {
  152. WindowsManager.Instance.show(WindowConfig.windowType.ProjectMain);
  153. }
  154. public void GoDemo()
  155. {
  156. // WindowsManager.Instance.show(WindowConfig.windowType.Demo);
  157. }
  158. public void GoRTC()
  159. {
  160. WindowsManager.Instance.show(WindowConfig.windowType.RTC);
  161. }
  162. public void GoDaoHang()
  163. {
  164. WindowsManager.Instance.show(WindowConfig.windowType.DaoHang);
  165. }
  166. public void GoXunJian()
  167. {
  168. WindowsManager.Instance.show(WindowConfig.windowType.XunJianLB);
  169. }
  170. public void AIDeviceExit()
  171. {
  172. deviceGo.SetActive(false);
  173. AISelect.SetActive(true);
  174. // Ui 显示 隐藏
  175. TopManager.Instance.AIExitIconState(false);
  176. }
  177. }