ISAP/Assets/Scripts/UI/UIPeopleInfoItem.cs

82 lines
2.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class UIPeopleInfoItem : MonoBehaviour
{
public Image head;
public Image headBG;
public TextMeshProUGUI infoTxt;
public TextMeshProUGUI lauguage;
public Button beginBtn;
string imgurl;
string headurl;
private RowsItem data;
private void Awake()
{
if (head == null)
{
Debug.LogError(this.gameObject.name + "Head is Null");
return;
}
if (infoTxt == null)
{
Debug.LogError(this.gameObject.name + "infoTxt is Null");
return;
}
if (beginBtn == null)
{
Debug.LogError(this.gameObject.name + "beginBtn is Null");
return;
}
}
public void Init(RowsItem item)
{
this.data = item;
head.sprite = Resources.Load<Sprite>("");
infoTxt.text = data.name + "-" + data.specialty;
lauguage.text = data.languageType;
imgurl = data.image;
StartCoroutine(DownSprite());
beginBtn.onClick.AddListener(OnClickBegin);
}
IEnumerator DownSprite()
{
UnityWebRequest wr = new UnityWebRequest(imgurl);
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));
headBG.sprite = sprite;
head.sprite = sprite;
//transform.GetComponent<Image>().sprite = sprite;
}
}
private void OnClickBegin()
{
//½øÈëÏÂÒ»¸ö½çÃæ
DataManager.Instance.currentSelectPeople = data;
UIManager.Instance.OpenUI(UIType.UIPeopleProgress);
}
}