Unity 下载图片并保存(WWW)

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

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class DownLoadImage : MonoBehaviour {

    public static DownLoadImage _instance;
    public List playerTextureList = new List();
    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 fi1 = new List();

        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);
    }
}

你可能感兴趣的:(unity文件,资源操作,Unity)