untiy 外部加載圖片

在unity裏面創建StreamingAssets文件夾,

參數是路徑,下面是圖片的尺寸

    private Sprite LoadImage(string path)
    {
        Texture2D texture = new Texture2D(701, 395);
        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            fs.Seek(0, SeekOrigin.Begin);
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, (int)fs.Length);
            texture.LoadImage(bytes);
        }
        return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章