using System.Collections; using System.Collections.Generic; using TMPro; using Unity.VisualScripting; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using UnityEngine.Video; public class UIFile : UIBase { public GameObject video; public GameObject image; public GameObject URL; public GameObject music; public GameObject text; public Button close; string url; private void Awake() { close.onClick.AddListener(() => { UIManager.Instance.DestoryUI(UIType.UIFile); }); } public void Init(FileType file, string url = null) { this.url = url; switch (file) { case FileType.text: text.SetActive(true); ShowText(); break; case FileType.video: video.SetActive(true); ShowVideo(); break; case FileType.image: image.SetActive(true); ShowImage(); break; case FileType.URL: URL.SetActive(true); ShowURL(); break; case FileType.music: music.SetActive(true); ShowMusic(); break; } } void ShowImage() { StartCoroutine(DownSprite()); } IEnumerator DownSprite() { UnityWebRequest wr = new UnityWebRequest(url); DownloadHandlerTexture texD1 = new DownloadHandlerTexture(true); wr.downloadHandler = texD1; yield return wr.SendWebRequest(); int width = 280; int high = 100; 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)); image.GetComponent().sprite = sprite; //transform.GetComponent().sprite = sprite; } } void ShowMusic() { AudioSource audio = music.GetComponent(); audio.clip = Resources.Load("UIFile/Music/music1").GetComponent(); audio.transform.Find("musicInfo").GetComponent().text = audio.clip.name; Button btn = music.GetComponent