using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using static LangChaoRommMinIo; public class PngJpgItem : BaseFilePrefabItem { public RawImage mainImage; public RectTransform rectTransform; public override void Init(FileConfig fileConfig) { base.Init(fileConfig); //LangChaoRommMinIo.Instance.getFile(MQTTManager.Instance.roomId, fileConfig.Bucket, fileConfig.ObjectName, (RoomFileData rfd) => { // Debug.Log("rfd==>" + rfd.url); // Debug.Log("rfd==>" + rfd.bytes.Length); // Texture2D texture = new Texture2D(10, 10); // texture.LoadImage(rfd.bytes);//流数据转换成Texture2D // texture.Apply(); // Adaption(texture); // mainImage.texture = texture; //}); if (!string.IsNullOrEmpty(fileConfig.Url)) { string url = fileConfig.Url; if (!fileConfig.Url.Contains("http")) { url = "https://" + fileConfig.Url; } } LangChaoRommMinIo.Instance.getFile(MQTTManager.Instance.roomId, fileConfig.Bucket, fileConfig.ObjectName, (RoomFileData rfd) => { Debug.Log("rfd==>" + rfd.url); Debug.Log("rfd==>" + rfd.bytes.Length); string url = rfd.url; mainImage.texture = GetSprite(rfd.bytes).texture; //NetWorkHeaders.Instance.getNetTexture(url, null, (Texture tex) => //{ // if (tex) // { // Adaption(tex); // mainImage.texture = tex; // } //}); }); } public Sprite GetSprite(Byte[] bytes) { //先创建一个Texture2D对象,用于把流数据转成Texture2D Texture2D texture = new Texture2D(10, 10); texture.LoadImage(bytes);//流数据转换成Texture2D //创建一个Sprite,以Texture2D对象为基础 Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero); return sp; } public void Adaption(Texture tex) { float standard_width = 150f; float standard_height = 84f; float video_width = tex.width; float video_height = tex.height; //Debug.Log(tex.width + "***" + tex.height); if (standard_width < video_width && standard_height > video_height) { float video_aspect = standard_width / video_width; rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect); } else if (standard_width > video_width && standard_height < video_height) { float video_aspect = standard_height / video_height; rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height); } else if (standard_width > video_width && standard_height > video_height) { if (standard_width / video_width > standard_height / video_height) { float video_aspect = standard_height / video_height; rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect); } else { float video_aspect = standard_width / video_width; rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect); } } else { if (standard_width / video_width > standard_height / video_height) { float video_aspect = standard_height / video_height; rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect); } else { float video_aspect = standard_width / video_width; rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect); } } } }