using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; public class FileItem : MonoBehaviour { public FileInfoData currentData; public GameObject titlePanel; public GameObject contentPanel; public TextMeshProUGUI titleTxt; public TextMeshProUGUI contentTxt; public Image img; private void Awake() { } public void Init(FileInfoData data) { currentData = data; if (currentData != null) { titleTxt.text = currentData.title; contentTxt.text = currentData.content; if (string.IsNullOrEmpty(currentData.title)) { titlePanel.SetActive(false); } if (string.IsNullOrEmpty(currentData.content)) { contentPanel.SetActive(false); } switch (data.type) { case FileType.text: break; case FileType.image: StartCoroutine(DownSprite()); break; } } else { Debug.LogError("文件信息为空"); } } IEnumerator DownSprite() { UnityWebRequest wr = new UnityWebRequest(currentData.url); DownloadHandlerTexture texD1 = new DownloadHandlerTexture(true); wr.downloadHandler = texD1; yield return wr.SendWebRequest(); int width = 400; int high = 300; if (!wr.isNetworkError) { Texture2D tex = new Texture2D(width, high); tex = texD1.texture; //保存本地 //Byte[] bytes = tex.EncodeToPNG(); //File.WriteAllBytes(Application.dataPath + "/" + data.name + ".png", bytes); Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f)); img.sprite = sprite; img.gameObject.SetActive(true); //transform.GetComponent().sprite = sprite; UIManager.Instance.GetUIBase(UIType.UIFile).gameObject.GetComponent().Refrush(); } } private void OnDestroy() { } } public class FileInfoData { public string title; public string content; public FileType type; public string url; public DataItem2 question; }