zoukankan      html  css  js  c++  java
  • Unity3d-WWW实现图片资源显示以及保存和本地加载

    本文固定连接:http://blog.csdn.net/u013108312/article/details/52712844

    WWW实现图片资源显示以及保存和本地加载

    using UnityEngine;
    using System.Collections;
    using System.IO;
    using UnityEditor;
    
    enum GetPicType
    {
        DownLoad = 0,
        LocalLoad,
    }
    
    public class Picture : MonoBehaviour
    {
        //这里是本地的ip地址
        string url = "http://127.0.0.1:80000/1.jpg";
        /// <summary>
        /// 网络下载的图片
        /// </summary>
        private Texture2D img = null;
        /// <summary>
        /// 本地图片
        /// </summary>
        private Texture2D img2 = null;
        private bool downloadOK = false;
    
        void OnGUI()
        {
            if (this.img != null)
                GUI.DrawTexture(new Rect(0, 0, 200, 300), this.img);
            if (this.img2 != null)
                GUI.DrawTexture(new Rect(320, 0, 200, 300), this.img2);
            if (GUI.Button(new Rect(210, 0, 100, 20), "显示网络图片"))
            {
                    StartCoroutine(this.DownLoadTexture(this.url, GetPicType.DownLoad));
            }
            if (GUI.Button(new Rect(210, 50, 100, 20), "显示本地图片"))
            {
                if (this.downloadOK)
                {
                    StartCoroutine(DownLoadTexture("file://" + Application.streamingAssetsPath + "/1.png", GetPicType.LocalLoad));
                }
                else
                {
                    Debug.LogError("没有下载完毕");
                }
            }
        }
    
        IEnumerator DownLoadTexture(string url, GetPicType getType)
        {
            WWW www = new WWW(url);
            Texture2D tempImage;
            yield return www;
            if (www.isDone && www.error == null)
            {
                switch (getType)
                {
                    case GetPicType.DownLoad:
                        {
                            this.img = www.texture;
                            tempImage = this.img;
                            Debug.Log(tempImage.width + "  " + tempImage.height);
                            break;
                        }
                    case GetPicType.LocalLoad:
                        this.img2 = www.texture;
                        tempImage = this.img;
                        Debug.Log(tempImage.width + "  " + tempImage.height);
                        break;
                    default:
                        tempImage = null;
                        break;
                }
                if (tempImage != null)
                {
                    byte[] data = tempImage.EncodeToPNG();
                    File.WriteAllBytes(Application.streamingAssetsPath + "/1.png", data);
                    this.downloadOK = true;
                }
            }
        }
    }
    
  • 相关阅读:
    iOS多线程编程之NSThread的使用
    在 VS 2013/2015 中禁用 nuget 包的源代码管理
    【转】辽宁北斗TV直播源
    Xamarin Crack
    在VisualStudio 编辑器文本替换中使用正则表达式
    Camtasia 录屏说明
    简化MSI在WIN10的安装
    在WPF中使用变通方法实现枚举类型的XAML绑定
    XAML UserControl的继承
    ArcGIS 10.1 BUG记录
  • 原文地址:https://www.cnblogs.com/alps/p/7795894.html
Copyright © 2011-2022 走看看