using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO; using OpenCVForUnity.CoreModule; using OpenCVForUnity.ImgprocModule; using OpenCVForUnity.ImgcodecsModule; using OpenCVForUnity.UnityUtils; public class TemplateImage : BaseTemPlate { private Image m_Image; public Image M_Image { get { if(m_Image ==null) { m_Image = CacheTransform.Find("Canvas/Image").GetComponent(); } return m_Image; } set { } } protected override void OnEnable() { base.OnEnable(); if (GameManager.Instance.IsRuning && m_Image.sprite == null && Data != null) { // DownloadManager.Instance.GetImage(Data, m_Image); DownLoadMaterial data = Data; //data.downLoadPath = Data.downLoadPath; //data.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(Data.downLoadPath); //data.updataTime = GameManager.Instance.m_SceneValue.updateTime; //data.type = "1"; MsgHandler.AddListener(data.downLoadPath, HandleMsg); DownloadResManager.Instance.DownLoad(data); } } protected override void OnAwake() { base.OnAwake(); m_Image = CacheTransform.Find("Canvas/Image").GetComponent(); HideCollider(); } public override void SetData(MaterialObjValue value, long updateTime) { base.SetData(value, updateTime); if (!GameManager.Instance.IsRuning) { //DownloadManager.Instance.AddDownloadData(Data); } else { // DownloadManager.Instance.GetImage(Data, m_Image); } DownLoadMaterial data = new DownLoadMaterial(); data.downLoadPath = value.DownloadPath; data.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath); data.updataTime = updateTime;//GameManager.Instance.m_SceneValue.updateTime; data.type = "1"; MsgHandler.AddListener(data.downLoadPath, HandleMsg); DownloadResManager.Instance.DownLoad(data); if (Data == null) Data = data; } private void HandleMsg(Msg msg) { if(msg.Value!=null) { Texture2D texture = new Texture2D(1, 1); OpenCVForUnity.CoreModule.Mat _mat = null; Loom.RunAsync(() => { Debug.Log("加載完成222222222222222222"); _mat = GameManager.zoomByteByOpenCV((byte[])msg.Value); Debug.Log("1111111"); Loom.QueueOnMainThread(() => { Debug.Log("加載完成111111111111"); texture = GameManager.TextureByMat(_mat); Sprite sprite = Sprite.Create(texture, new UnityEngine.Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f); m_Image.sprite = sprite; _mat.release(); _mat = null; Texture2D tex = transform.Find("Canvas/Image").GetComponent().sprite.texture; if (tex.height > tex.width) { float bl = ((float)tex.height / (float)tex.width); transform.Find("Canvas").GetComponent().sizeDelta = new Vector2(500 / bl, 500); } else { float bl = ((float)tex.width / (float)tex.height); transform.Find("Canvas").GetComponent().sizeDelta = new Vector2(500, 500 / bl); } }); }); // texture.LoadImage((byte[])msg.Value); // 图片原始比例 } else { Debug.LogError("DGJ Image 下载失败 :" + Data.downLoadPath); } } public void SetLocaImageData(MaterialObjValue value) { m_Image.sprite = ResMgr.Instance.Load(value.DownloadPath); } public override void HideCollider() { base.HideCollider(); } }