赞
踩
1------------------------------
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.UI; public class LoadFromWeb : MonoBehaviour { public Image image; string img_url = "https://pic.baike.soso.com/ugc/baikepic2/6749/20210720102025-811299437_jpeg_3968_2976_3786803.jpg/0"; void Start() { StartCoroutine(RYVACBT_iadufa(img_url)); } IEnumerator RYVACBT_iadufa(string url) { WWW www = new WWW(url); yield return www; if (www.error == null) { print("---load Succeed---"); Texture2D t = www.texture; Sprite csprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0, 0)); byte[] bytedata = t.EncodeToPNG(); //将下载的图片动态保存到本地 File.WriteAllBytes(Application.dataPath + "/a", bytedata); image.sprite = csprite; } } }
2--------------------------------------------
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.UI; using UnityEngine.Networking; public class LoadFromWeb : MonoBehaviour { public Image image; public GameObject cube; string img_url = "https://pic.baike.soso.com/ugc/baikepic2/6749/20210720102025-811299437_jpeg_3968_2976_3786803.jpg/0"; void Start() { StartCoroutine(LoadImage()); } /// <summary> /// 加载图片 /// </summary> /// <returns></returns> IEnumerator LoadImage() { using (UnityWebRequest webRequest = new UnityWebRequest()) { //设置URL webRequest.url = img_url; //设置访问方式 webRequest.method = UnityWebRequest.kHttpVerbGET; //设置下载类型 webRequest.downloadHandler = new DownloadHandlerTexture(); //协程等待 yield return webRequest.SendWebRequest(); if (webRequest.isNetworkError || webRequest.isHttpError) { Debug.Log(webRequest.error); } else { //将图片加载到3D平面 cube.GetComponent<Renderer>().material.mainTexture = DownloadHandlerTexture.GetContent(webRequest); //将图片加载到UI图片 Texture2D texture2D = DownloadHandlerTexture.GetContent(webRequest); image.sprite = Sprite.Create( texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f)); } } } /// <summary> /// 加载视频 /// </summary> public void LoadVideo() { //videoPlayer.url = inputField.text; //videoPlayer.Play(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。