码迷,mamicode.com
首页 > 其他好文 > 详细

u3d 加载PNG做 UI图片

时间:2015-10-13 19:03:29      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.UI;

public class UITexLoader : MonoBehaviour 
{

    public string _Path ="Assets/pic/01/";
    public string _texname;
    
    private Texture2D m_Tex;
   

   
    private void LoadFromFile(string path,string _name)
    {
        m_Tex = new Texture2D(1, 1);
        m_Tex.LoadImage(ReadPNG(path+ _name));
    }

    private byte[] ReadPNG(string path)
    {
        FileStream fileStream = new FileStream(path, FileMode.Open, System.IO.FileAccess.Read);

        fileStream.Seek(0, SeekOrigin.Begin);

        byte[] binary = new byte[fileStream.Length]; //创建文件长度的buffer
        fileStream.Read(binary, 0, (int)fileStream.Length);

        fileStream.Close();

        fileStream.Dispose();

        fileStream = null;

        return binary;
    }


    // Use this for initialization
    void Start()
    {
        
        LoadFromFile(_Path,_texname);
        Sprite tempSprite = new Sprite();
        tempSprite = Sprite.Create(m_Tex, new Rect(0, 0, m_Tex.width, m_Tex.height), new Vector2(0, 0));
        GetComponent<Image>().sprite  =tempSprite;
    }
    
    // Update is called once per frame
    void Update ()
    {
    
    }
}

 

u3d 加载PNG做 UI图片

标签:

原文地址:http://www.cnblogs.com/dragon2012/p/4875394.html

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