zoukankan      html  css  js  c++  java
  • unity读取Texture文件并转为Sprit

    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class ImageTest : MonoBehaviour
    {
        /// <summary>
        /// Image控件
        /// </summary>
        private Image image;
    
        void Start()
        {
            image = this.transform.Find("Image").GetComponent<Image>();
    
            //为不同的按钮绑定不同的事件
            this.transform.Find("LoadByWWW").GetComponent<Button>().onClick.AddListener
            (
               delegate () { LoadByWWW(); }
            );
    
            this.transform.Find("LoadByIO").GetComponent<Button>().onClick.AddListener
            (
              delegate () { LoadByIO(); }
            );
        }
    
        /// <summary>
        /// 以IO方式进行加载
        /// </summary>
        private void LoadByIO()
        {
           // double startTime = (double)Time.time;
            //创建文件读取流
            FileStream fileStream = new FileStream(Application.dataPath+ "/UI/Basic Information/Common/Add.png", FileMode.Open, FileAccess.Read);
            fileStream.Seek(0, SeekOrigin.Begin);
            //创建文件长度缓冲区
            byte[] bytes = new byte[fileStream.Length];
            //读取文件
            fileStream.Read(bytes, 0, (int)fileStream.Length);
            //释放文件读取流
            fileStream.Close();
            fileStream.Dispose();
            fileStream = null;
    
            //创建Texture
            int width = 300;
            int height = 372;
            Texture2D texture = new Texture2D(width, height);
            texture.LoadImage(bytes);
    
            //创建Sprite
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            image.sprite = sprite;
    
            //startTime = (double)Time.time - startTime;
            //Debug.Log("IO加载用时:" + startTime);
        }
    
        /// <summary>
        /// 以WWW方式进行加载
        /// </summary>
        private void LoadByWWW()
        {
            StartCoroutine(Load());
        }
    
        IEnumerator Load()
        {
            double startTime = (double)Time.time;
            //请求WWW
            //WWW www = new WWW("file://D:\test.jpg");
            string path= (Application.dataPath + "/UI/Basic Information/Common/Add.png");
            WWW www=new WWW("file://"+path);
            yield return www;
            if (www != null && string.IsNullOrEmpty(www.error))
            {
                //获取Texture
                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;
    
                startTime = (double)Time.time - startTime;
                Debug.Log("WWW加载用时:" + startTime);
            }
        }
    }

    原文链接http://blog.csdn.net/qinyuanpei/article/details/48262583
  • 相关阅读:
    linux sed
    低版本的 opencv库的 vs2010 打开 高版本opencv
    跨,跨,跨,我的2013半年总结
    收集用户行为
    文章17周项目2--通过基准线结合(三个数字排序(指针参数))
    Ubuntu下一个python的BeautifulSoup和rsa安装方法---信息检索project2部分:微博爬行要求python包裹
    HTML_ul无序列表
    左右Cwnd::Create()功能出现afxwin1.inl line:21错误的解决方案
    rac下一个/tmp/bootstrap权限问题
    C 删除字符串1字符串2
  • 原文地址:https://www.cnblogs.com/lanrenqilanming/p/7997426.html
Copyright © 2011-2022 走看看