using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class DownloadPicFromURL : MonoBehaviour
{
public RawImage myImage; // 需要被賦予紋理的UI圖像,将場景中的RawImage拖拽指派給該腳本
private GameObject myPlane; // 需要被賦予紋理的平面
void Start()
{
// 通過Find方法指派
myPlane = GameObject.Find("Plane");
StartCoroutine(DownloadImage("http://rd7ckb9qt.hn-bkt.clouddn.com/step2.png"));
}
IEnumerator DownloadImage(string MediaUrl)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
Debug.Log(request.error);
else
{
// RawImage賦予紋理
myImage.texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
// 平面賦予紋理
myPlane.GetComponent<Renderer>().material.mainTexture = ((DownloadHandlerTexture)request.downloadHandler).texture;
}
}
}