//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";
// 开启协程去本地拿图片
StartCoroutine (LoadPictureFromLocal ());
}
//优先加载本地资源 读取较快 没有的话在去网上下载
IEnumerator LoadPictureFromLocal ()
{
if (File.Exists (m_imagePath)) {
Debug.Log ("图片已经存在");
// 直接加载本地图片资源
Texture2D texture = (Texture2D)Resources.Load ("NetPicture", typeof(Texture2D));
// 创建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 不显示下载好的图片
//使用协程来解决本地没有图片时下载完后game不显示图片
if (m_image.sprite != null) {
StopCoroutine (LoadPictureFromLocal ());
} else {
StartCoroutine (LoadPictureFromLocal ());
}
}
//根据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);//转化为jpg图,可以压缩20倍左右
//form.AddBinaryData("file", imagebytes, + ",file:" + fileName, "image/png");
//添加文件(输入对象的名字、二进制数组、文件对象类型)
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-----
/// 请求网络图片 根据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------
/// 请求网络图片 根据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);
}
}
}