码迷,mamicode.com
首页 > 编程语言 > 详细

Unity两中方式加载图片

时间:2017-08-12 17:05:10      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:sprite   alt   ini   log   logs   texture   unit   initial   get   

 



看到草羊发的图片,也是真懒。
想起来当时写天气预报现在都忘干净了,好记性不如烂笔头,还是应该多记。自己打下来好了
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;

public class readPictures : MonoBehaviour
{


    private Image image;
    private string loadpath = "D:/SOmeOther/NGUI/Assets/StreamingAssets/test.jpg"; //IO方式加载的路径
    private string picpathWWW = "test.jpg"; //WWW的加载方式路径

    // Use this for initialization
    private void Start()
    {
        image = GetComponent<Image>();
        //IO方法加载速度快
//        LoadByIO();
        //WWW 加载速度慢
        LoadByWWW();

    }

    private void LoadByIO()
    {
        double startTime = (double) Time.time;
        //创建文件流
        FileStream fileStream = new FileStream(loadpath, FileMode.Open, FileAccess.Read);
        fileStream.Seek(0, SeekOrigin.Begin);
        //创建文件长度的缓冲区
        byte[] bytes = new byte[fileStream.Length];
        //读取文件
        fileStream.Read(bytes, 0, (int) fileStream.Length);
        //释放文件读取liu
        fileStream.Close();
        fileStream.Dispose();
        fileStream = null;

        //创建Texture
        int width = 300;
        int height = 372;
        Texture2D texture2D = new Texture2D(width, height);
        texture2D.LoadImage(bytes);

        Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height),
            new Vector2(0.5f, 0.5f));
        image.sprite = sprite;
        double time = (double) Time.time - startTime;
        Debug.Log("IO加载用时:" + time);
    }


    private void LoadByWWW()
    {
        StartCoroutine(Load());
    }


    private string url = "file://D:/SOmeOther/NGUI/Assets/StreamingAssets/";
//    private string url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502532130856&di=7135149ed906483861efdfc9770def3b&imgtype=0&src=http%3A%2F%2Fwww.newasp.net%2Fattachment%2Fsoft%2F2017%2F0811%2F144057_83971519.png"; 这里当然可以换做网络图片的URL 就加载网络图片了
    private IEnumerator Load()
    {
        double startTime = (double) Time.time;

        WWW www = new WWW(url + picpathWWW);//只能放URL
//        WWW www = new WWW(url);//只能放URL 这里可以换做网络的URL
        yield return www;
        if (www!=null && string.IsNullOrEmpty(www.error))
        {

            Texture2D texture = www.texture;
            //创建 Sprite
            Sprite sprite = Sprite.Create(texture,new Rect(0,0,texture.width,texture.height),new Vector2(0.5f,0.5f) );

            image.sprite = sprite;
            double time = (double)Time.time - startTime;
            Debug.Log("WWW加载用时:" + time);
        }
    }


}

 

技术分享

Unity两中方式加载图片

标签:sprite   alt   ini   log   logs   texture   unit   initial   get   

原文地址:http://www.cnblogs.com/guoguoguo/p/7350547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!