• 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
  • 相关阅读:
    WebService安全 文件夹 目录安全性 身份验证与访问控制
    爱情故事[转载自:学狼网]
    未来中国最受宠的人才
    [引]SQL Server : 系统存储过程
    VS 安装项目 :通过文本框得到用户输入 以及 安装后运行某程序(如打开C:\\a.html)
    人人都能成为百万富翁
    成功原来这样简单
    精典谚语
    利用WinForm 更好的实现Web安装程序的更多功能
    升级安装包的制作
  • 原文地址:https://www.cnblogs.com/lanrenqilanming/p/7997426.html
Copyright © 2020-2023  润新知