ISAP/Assets/Scripts/UI/UIFile.cs

146 lines
3.7 KiB
C#

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<Image>().sprite = sprite;
//transform.GetComponent<Image>().sprite = sprite;
}
}
void ShowMusic()
{
AudioSource audio = music.GetComponent<AudioSource>();
audio.clip = Resources.Load("UIFile/Music/music1").GetComponent<AudioClip>();
audio.transform.Find("musicInfo").GetComponent<TextMeshProUGUI>().text = audio.clip.name;
Button btn = music.GetComponent<Button>();
btn.onClick.AddListener(() =>
{
if (audio.isPlaying == true)
{
audio.Pause();
audio.transform.Find("text").GetComponent<TextMeshProUGUI>().text = "²¥·Å";
}
else
{
audio.Play();
audio.transform.Find("text").GetComponent<TextMeshProUGUI>().text = "ÔÝÍ£";
}
});
}
void ShowText()
{
}
void ShowURL()
{
}
void ShowVideo()
{
VideoPlayer player = video.GetComponent<VideoPlayer>();
//Button btn = video.GetComponent<Button>();
//btn.onClick.AddListener(() =>
//{
// if (player.isPlaying == true)
// {
// player.Pause();
// player.transform.Find() = "²¥·Å";
// }
// else
// {
// player.Play();
// player.text = "ÔÝÍ£";
// }
//});
if (string.IsNullOrEmpty(url))
{
player.source = VideoSource.VideoClip;
player.clip = Resources.Load("UIFile/Video/video1").GetComponent<VideoClip>();
}
else
{
player.source = VideoSource.Url;
}
}
}