TemplateImage.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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, int 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 = 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. GameManager.Instance.AddThead(() =>
  76. {
  77. Debug.Log("加載完成222222222222222222");
  78. _mat =GameManager.zoomByteByOpenCV((byte[])msg.Value);
  79. }, () =>
  80. {
  81. Debug.Log("加載完成111111111111");
  82. texture = GameManager.TextureByMat(_mat);
  83. Sprite sprite = Sprite.Create(texture, new UnityEngine.Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);
  84. m_Image.sprite = sprite;
  85. _mat.release();
  86. _mat = null;
  87. Texture2D tex = transform.Find("Canvas/Image").GetComponent<Image>().sprite.texture;
  88. if (tex.height > tex.width)
  89. {
  90. float bl = ((float)tex.height / (float)tex.width);
  91. transform.Find("Canvas").GetComponent<RectTransform>().sizeDelta = new Vector2(500 / bl, 500);
  92. }
  93. else
  94. {
  95. float bl = ((float)tex.width / (float)tex.height);
  96. transform.Find("Canvas").GetComponent<RectTransform>().sizeDelta = new Vector2(500, 500 / bl);
  97. }
  98. });
  99. // texture.LoadImage((byte[])msg.Value);
  100. // 图片原始比例
  101. }
  102. else
  103. {
  104. Debug.LogError("DGJ Image 下载失败 :" + Data.downLoadPath);
  105. }
  106. }
  107. public void SetLocaImageData(MaterialObjValue value)
  108. {
  109. m_Image.sprite = ResMgr.Instance.Load<Sprite>(value.DownloadPath);
  110. }
  111. public override void HideCollider()
  112. {
  113. base.HideCollider();
  114. }
  115. }