DemoManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. private void OnEnable()
  16. {
  17. if (ChangeCameraSaoMiao.Instance)
  18. {
  19. StartCoroutine(DownloadImage("https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/MREdustry_100/DemoShiBieTu.jpg"));
  20. ChangeCameraSaoMiao.Instance.showDemoSaoMiao();
  21. imageTarget.SetActive(false);
  22. TimerMgr.Instance.CreateTimer(()=> {
  23. imageTarget.SetActive(true);
  24. },10f);
  25. }
  26. }
  27. public void jixushibie()
  28. {
  29. StartCoroutine(DownloadImage("https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/MREdustry_100/DemoShiBieTu.jpg"));
  30. ChangeCameraSaoMiao.Instance.showDemoSaoMiao();
  31. imageTarget.SetActive(false);
  32. shibieGo.SetActive(false);
  33. TimerMgr.Instance.CreateTimer(() => {
  34. imageTarget.SetActive(true);
  35. }, 10f);
  36. }
  37. public void tuichushibie()
  38. {
  39. WindowsManager.Instance.show(WindowConfig.windowType.ProjectMain);
  40. WindowsManager.Instance.show(WindowConfig.windowType.Top, false);
  41. }
  42. public GameObject shibieGo;
  43. private void Update()
  44. {
  45. if(im)
  46. {
  47. if (imageTarget.activeSelf)
  48. {
  49. if (im)
  50. {
  51. Destroy(im);
  52. im = null;
  53. shibieGo.SetActive(true);
  54. timetext.text = DateTime.Now.ToString("yyyy 年 M 月 d 日 HH:mm");
  55. ChangeCameraSaoMiao.Instance.close();
  56. }
  57. }
  58. }
  59. }
  60. // 协程方法,用于异步下载图片
  61. IEnumerator DownloadImage(string url)
  62. {
  63. // 使用UnityWebRequestTexture来获取图片纹理
  64. UnityWebRequest m_webrequest = UnityWebRequest.Get(url);
  65. yield return m_webrequest.SendWebRequest();
  66. // 检查下载是否成功
  67. if (m_webrequest.result != UnityWebRequest.Result.Success)
  68. {
  69. // 打印错误信息
  70. Debug.LogError("Failed to download image");
  71. }
  72. else
  73. {
  74. string giftopngpath = Application.persistentDataPath + "/DownLoadXR/DemoShiBieTu.png";
  75. // 从下载处理器获取纹理
  76. Texture2D tex = new Texture2D(1, 1);
  77. tex.LoadImage( m_webrequest.downloadHandler.data);
  78. tex.Apply();
  79. byte[] gifbyte = tex.EncodeToPNG();
  80. //文件流信息
  81. //StreamWriter sw;
  82. Stream sw;
  83. FileInfo file = new FileInfo(giftopngpath);
  84. try
  85. {
  86. if (!Directory.Exists(Application.persistentDataPath + "/DownLoadXR"))
  87. {
  88. Directory.CreateDirectory(Application.persistentDataPath + "/DownLoadXR");
  89. }
  90. if (!file.Exists)
  91. {
  92. sw = file.Create();
  93. //如果此文件存在则打开
  94. //sw = file .Append();
  95. //如果此文件不存在则创建
  96. //以行的形式写入信息
  97. //sw.WriteLine(info);
  98. sw.Write(gifbyte, 0, gifbyte.Length);
  99. sw.Close();
  100. sw.Dispose();
  101. }
  102. }
  103. catch
  104. {
  105. }
  106. yield return new WaitForSeconds(1f) ;
  107. ImageFileSourceData data = new ImageFileSourceData();
  108. data.Name = "DemoShiBieTu";
  109. data.Path = giftopngpath;
  110. data.PathType = PathType.Absolute;
  111. data.Scale = 0.1f;
  112. im = imageTarget.AddComponent<ImageTargetController>();
  113. im.ImageFileSource = data;
  114. ARSaoTuManager.Instance.imageOnFind.LoadTarget(im);
  115. ARSaoTuManager.Instance.cameraDevice.Open();
  116. }
  117. }
  118. ImageTargetController im;
  119. public GameObject imageTarget;
  120. private void OnDisable()
  121. {
  122. if (im)
  123. {
  124. Destroy(im);
  125. im = null;
  126. }
  127. shibieGo.SetActive(false);
  128. }
  129. }