網(wǎng)絡(luò)圖片加載--WWW類

//www

// 功能:

// 掛載對象:

//注意:

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

using UnityEditor;

using System.IO;

public class DownloadimageScript : MonoBehaviour

{

// 顯示下載好的圖片

public Image m_image;

//const 防止下面修改

private const string m_image_URL = "http://www.33lc.com/article/UploadPic/2012-9/201291314425876155.jpg";

private string m_imagePath;

void Start ()

{

// 如果這張圖片存在 直接去本地拿 如果不存在就去下載 下載完后保存到本地 這樣下次就能直接用

m_imagePath = Application.dataPath + "/Resources/NetPicture.jpg";

// 開啟協(xié)程去本地拿圖片

StartCoroutine (LoadPictureFromLocal ());

}

//優(yōu)先加載本地資源 讀取較快 沒有的話在去網(wǎng)上下載

IEnumerator LoadPictureFromLocal ()

{

if (File.Exists (m_imagePath)) {

Debug.Log ("圖片已經(jīng)存在");

// 直接加載本地圖片資源

Texture2D texture = (Texture2D)Resources.Load ("NetPicture", typeof(Texture2D));

// 創(chuàng)建sprite

Sprite m_sprte = Sprite.Create (texture, new Rect (0, 0, texture.width, texture.height), new Vector2 (0.5f, 0.5f));

m_image.sprite = m_sprte;

} else {

Debug.Log ("圖片文件開始下載");

StartCoroutine (PictureDownloding (m_image_URL));

}

yield return new WaitForSeconds (2f);

AssetDatabase.Refresh ();// 需要引用unityEditor ?來解決Project 不顯示下載好的圖片

//使用協(xié)程來解決本地沒有圖片時下載完后game不顯示圖片

if (m_image.sprite != null) {

StopCoroutine (LoadPictureFromLocal ());

} else {

StartCoroutine (LoadPictureFromLocal ());

}

}

//根據(jù)URL下載圖片并加載

IEnumerator PictureDownloding (string url)

{

// 方式1

WWW www = new WWW (url);

while (www.isDone == false) {

print ("下載圖片中" + www.progress);

yield return null;

}

yield return www;

if (www.texture != null && string.IsNullOrEmpty (www.error)) {

print ("正在寫入本地圖片");

byte[] pngData = www.texture.EncodeToPNG ();

File.WriteAllBytes (m_imagePath, pngData);

}

}

}

//UnityWebRequest

using System.Collections;

using System.Collections.Generic;

using System.IO;

using UnityEngine;

using UnityEngine.Networking;

using UnityEngine.UI;

public class SendMessageToFlask : MonoBehaviour {

? ? public Texture2D texture;

? ? public RawImage rawImage;

void Start () {

? ? ? ? //StartCoroutine(Get());

? ? ? ? StartCoroutine(ImagePost());

? ? }


? ? IEnumerator Get()

? ? {

? ? ? // string url = "http://192.168.6.2:5656/info/?name=zhang&age=18";

? ? ? ? string url = "http://8.129.176.192:5656/";

? ? ? ? UnityWebRequest webRequest = UnityWebRequest.Get(url);

? ? ? ? yield return webRequest.Send();


? ? ? ? if (webRequest.isError)

? ? ? ? ? ? Debug.Log(webRequest.error);

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? Debug.Log(webRequest.downloadHandler.text);

? ? ? ? }

? ? }

? ? IEnumerator ImagePost()

? ? {

? ? ? ? string url = "http://192.168.6.2:5656/receive_image_bytes/";

? ? ? ? //string url = "http://39.108.165.190:5000/receive_image_bytes/";

? ? ? ? yield return new WaitForEndOfFrame();

? ? ? ? WWWForm form = new WWWForm();

? ? ? ? byte[] imagebytes = texture.EncodeToJPG(50);//轉(zhuǎn)化為jpg圖,可以壓縮20倍左右

? ? ? ? //form.AddBinaryData("file", imagebytes,? + ",file:" + fileName, "image/png");

? ? ? ? //添加文件(輸入對象的名字、二進制數(shù)組、文件對象類型)

? ? ? ? form.AddField("project", "flask01");?

? ? ? ? form.AddField("img_name", "flask01.jpg");

? ? ? ? form.AddBinaryData("file", imagebytes,"image/jpg");

? ? ? ? UnityWebRequest webRequest = UnityWebRequest.Post(url, form);

? ? ? ? yield return webRequest.Send();

? ? ? ? if (webRequest.isError)

? ? ? ? ? ? Debug.Log(webRequest.error);

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? Debug.Log(webRequest.downloadHandler.text);

? ? ? ? ? ? StartCoroutine(LoadImageFromFlask(webRequest.downloadHandler.text));

? ? ? ? ? ? //StartCoroutine(PictureDownloding(webRequest.downloadHandler.text));

? ? ? ? }

? ? }

? ? /// <summary>

? ? /// -----UnityWebRequest-----

? ? /// 請求網(wǎng)絡(luò)圖片 根據(jù)URL下載圖片

? ? /// </summary>

? ? /// <param name="url"></param>

? ? /// <returns></returns>

? ? IEnumerator LoadImageFromFlask(string url) {

? ? ? ? yield return new WaitForEndOfFrame();

? ? ? ? UnityWebRequest webRequest = new UnityWebRequest(url);

? ? ? ? DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);

? ? ? ? webRequest.downloadHandler = downloadTexture;

? ? ? ? yield return webRequest.Send();

? ? ? ? //int width = 300;

? ? ? ? //int high = 300;

? ? ? ? //Texture2D texture = new Texture2D(width, high);

? ? ? ? if (!(webRequest.isError))

? ? ? ? {

? ? ? ? ? ? //texture = downloadTexture.texture;

? ? ? ? ? ? rawImage.texture= downloadTexture.texture;

? ? ? ? }

? ? }


? ? /// <summary>

? ? /// -----WWW------

? ? /// 請求網(wǎng)絡(luò)圖片 根據(jù)URL下載圖片

? ? /// </summary>

? ? /// <param name="url"></param>

? ? /// <returns></returns>

? ? IEnumerator PictureDownloding(string url)

? ? {

? ? ? ? WWW www = new WWW(url);

? ? ? ? while (www.isDone == false)

? ? ? ? {

? ? ? ? ? ? print("下載圖片中" + www.progress);

? ? ? ? ? ? yield return null;

? ? ? ? }

yield return www;

? ? ? ? if (www.texture != null && string.IsNullOrEmpty(www.error))

? ? ? ? {

? ? ? ? ? ? string myjson = www.text;


? ? ? ? ? ? print("正在寫入本地圖片");

? ? ? ? ? ? byte[] pngData = www.texture.EncodeToPNG();

? ? ? ? ? string m_imagePath = Application.dataPath + "/Resources/NetPicture.jpg";

? ? ? ? ? ? File.WriteAllBytes(m_imagePath, pngData);

? ? ? ? }

? ? }


? ? IEnumerator Post()

? ? {

? ? ? ? WWWForm form = new WWWForm();

? ? ? ? //鍵值對

? ? ? ? form.AddField("key", "value");

? ? ? ? form.AddField("name", "mafanwei");

? ? ? ? form.AddField("blog", "qwe25878");

? ? ? ? UnityWebRequest webRequest = UnityWebRequest.Post("http://www.baidu.com", form);

? ? ? ? yield return webRequest.Send();


? ? ? ? if (webRequest.isError)

? ? ? ? ? ? Debug.Log(webRequest.error);

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? Debug.Log(webRequest.downloadHandler.text);

? ? ? ? }

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容