u3d 加载PNG做 UI图片

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().sprite  =tempSprite;
    }
    
    // Update is called once per frame
    void Update ()
    {
    
    }
}

 

你可能感兴趣的:(u3d 加载PNG做 UI图片)