Unity 下载图片并保存(WWW)

来源:互联网 发布:绝地求生 dx12优化 编辑:程序博客网 时间:2024/05/16 14:20

首先通过扫描仪扫描图片保存到指定文件夹下,再通过WWW类下载保存到指定的其他文件夹下使用

using System.Collections;using System.Collections.Generic;using System.IO;using UnityEngine;public class DownLoadImage : MonoBehaviour {    public static DownLoadImage _instance;    public List<Texture2D> playerTextureList = new List<Texture2D>();    public MeshRenderer[] meshRenderers;    private string httpSavePath;    private bool isDownLoading;    private int index = 1;    void Awake()    {        _instance = this;    }    void Start()    {        httpSavePath = @"E:\xampp\htdocs\vr\";        StartCoroutine(GetImage());    }    IEnumerator GetImage()    {        while (true)        {            if (isDownLoading)                break;            print("wait......");            yield return new WaitForSeconds(0.5f);            print("DownLoading....");        }        while (true)        {            if(index > 4)                break;            yield return StartCoroutine(Iamge1("http://192.168.1.103/vr/vrp_00000" + index + ".jpg"));            Debug.Log("Iamge" + index + "finish");            index++;        }    }    void Update()    {        CheckImageNumber();    }    void CheckImageNumber()    {        DirectoryInfo di1 = new DirectoryInfo(httpSavePath);        List<FileInfo> fi1 = new List<FileInfo>();        foreach (FileInfo item in di1.GetFiles())        {            fi1.Add(item);        }        if (fi1.Count >= 4)            isDownLoading = true;        if (playerTextureList.Count >= 4)        {            for (int i = 0; i < playerTextureList.Count; i++)            {                meshRenderers[i].material.mainTexture = playerTextureList[i];            }        }    }        IEnumerator Iamge1(string path)    {        WWW www = new WWW(path);        yield return www;        Texture2D texture = www.texture;        playerTextureList.Add(texture);    }}

原创粉丝点击