TemplateImage.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.IO;
  6. using OpenCVForUnity.CoreModule;
  7. using OpenCVForUnity.ImgprocModule;
  8. using OpenCVForUnity.ImgcodecsModule;
  9. using OpenCVForUnity.UnityUtils;
  10. public class TemplateImage : BaseTemPlate
  11. {
  12. private Image m_Image;
  13. public Image M_Image
  14. {
  15. get
  16. {
  17. if(m_Image ==null)
  18. {
  19. m_Image = CacheTransform.Find("Canvas/Image").GetComponent<Image>();
  20. }
  21. return m_Image;
  22. }
  23. set
  24. {
  25. }
  26. }
  27. protected override void OnEnable()
  28. {
  29. base.OnEnable();
  30. if (GameManager.Instance.IsRuning && m_Image.sprite == null && Data != null)
  31. {
  32. // DownloadManager.Instance.GetImage(Data, m_Image);
  33. DownLoadMaterial data = Data;
  34. //data.downLoadPath = Data.downLoadPath;
  35. //data.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(Data.downLoadPath);
  36. //data.updataTime = GameManager.Instance.m_SceneValue.updateTime;
  37. //data.type = "1";
  38. MsgHandler.AddListener(data.downLoadPath, HandleMsg);
  39. DownloadResManager.Instance.DownLoad(data);
  40. }
  41. }
  42. protected override void OnAwake()
  43. {
  44. base.OnAwake();
  45. m_Image = CacheTransform.Find("Canvas/Image").GetComponent<Image>();
  46. HideCollider();
  47. }
  48. public override void SetData(MaterialObjValue value, long updateTime)
  49. {
  50. base.SetData(value, updateTime);
  51. if (!GameManager.Instance.IsRuning)
  52. {
  53. //DownloadManager.Instance.AddDownloadData(Data);
  54. }
  55. else
  56. {
  57. // DownloadManager.Instance.GetImage(Data, m_Image);
  58. }
  59. DownLoadMaterial data = new DownLoadMaterial();
  60. data.downLoadPath = value.DownloadPath;
  61. data.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
  62. data.updataTime = updateTime;//GameManager.Instance.m_SceneValue.updateTime;
  63. data.type = "1";
  64. MsgHandler.AddListener(data.downLoadPath, HandleMsg);
  65. DownloadResManager.Instance.DownLoad(data);
  66. if (Data == null)
  67. Data = data;
  68. }
  69. private void HandleMsg(Msg msg)
  70. {
  71. if(msg.Value!=null)
  72. {
  73. Texture2D texture = new Texture2D(1, 1);
  74. OpenCVForUnity.CoreModule.Mat _mat = null;
  75. Loom.RunAsync(() =>
  76. {
  77. Debug.Log("加載完成222222222222222222");
  78. _mat = GameManager.zoomByteByOpenCV((byte[])msg.Value);
  79. Debug.Log("1111111");
  80. Loom.QueueOnMainThread(() =>
  81. {
  82. Debug.Log("加載完成111111111111");
  83. texture = GameManager.TextureByMat(_mat);
  84. Sprite sprite = Sprite.Create(texture, new UnityEngine.Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);
  85. m_Image.sprite = sprite;
  86. _mat.release();
  87. _mat = null;
  88. Texture2D tex = transform.Find("Canvas/Image").GetComponent<Image>().sprite.texture;
  89. if (tex.height > tex.width)
  90. {
  91. float bl = ((float)tex.height / (float)tex.width);
  92. transform.Find("Canvas").GetComponent<RectTransform>().sizeDelta = new Vector2(500 / bl, 500);
  93. }
  94. else
  95. {
  96. float bl = ((float)tex.width / (float)tex.height);
  97. transform.Find("Canvas").GetComponent<RectTransform>().sizeDelta = new Vector2(500, 500 / bl);
  98. }
  99. });
  100. });
  101. // texture.LoadImage((byte[])msg.Value);
  102. // 图片原始比例
  103. }
  104. else
  105. {
  106. Debug.LogError("DGJ Image 下载失败 :" + Data.downLoadPath);
  107. }
  108. }
  109. public void SetLocaImageData(MaterialObjValue value)
  110. {
  111. m_Image.sprite = ResMgr.Instance.Load<Sprite>(value.DownloadPath);
  112. }
  113. public override void HideCollider()
  114. {
  115. base.HideCollider();
  116. }
  117. }