|
using System.Collections.Generic; |
|
using System.IO; |
|
using UnityEngine; |
|
|
|
/* |
|
TODO: If we are in WebGL or in android, we must do a WebRequest to get the data. |
|
TODO: Make it Async for the loading. |
|
*/ |
|
|
|
public static class StreamingAssetsImageLoader |
|
{ |
|
const float PIXELS_PER_UNIT = 100f; // By default, 1 unit in Unity means 100 pixels |
|
static readonly Vector2 PIVOT = new Vector2(0.5f, 0.5f); // const wont work on struct like Vector2, so we use static readonly |
|
|
|
public static List<Texture2D> LoadTextures2dFromFolder(string relativeFolderPath = "", SearchOption searchOption = SearchOption.AllDirectories) |
|
{ |
|
if (relativeFolderPath != "" && !StreamingAssetsPathHelper.IsStreamingAssetsPath(relativeFolderPath)) |
|
{ |
|
return null; |
|
} |
|
|
|
string absoluteFolderPath = Path.Combine(Application.streamingAssetsPath, relativeFolderPath); |
|
|
|
List<Texture2D> texture2dList = new List<Texture2D>(); |
|
|
|
string[] filePaths = Directory.GetFiles(absoluteFolderPath, "*.*", searchOption); // It doesn't detect the folders path |
|
|
|
string pathExtension; |
|
|
|
string relativeFilePath; |
|
|
|
// Get and convert the image files |
|
foreach (string filePath in filePaths) |
|
{ |
|
pathExtension = Path.GetExtension(filePath); |
|
|
|
if (StreamingAssetsPathHelper.IsImageExtension(pathExtension)) |
|
{ |
|
relativeFilePath = filePath.Replace(Application.streamingAssetsPath, ""); // Convert to relative path |
|
relativeFilePath = relativeFilePath.Remove(0, 1); // Remove the root |
|
texture2dList.Add(LoadTexture2dFromPath(relativeFilePath)); |
|
} |
|
} |
|
|
|
return texture2dList; |
|
} |
|
|
|
public static Texture2D LoadTexture2dFromPath(string relativeFolderPath) |
|
{ |
|
if (!StreamingAssetsPathHelper.IsStreamingAssetsPath(relativeFolderPath)) |
|
{ |
|
return null; |
|
} |
|
|
|
string absoluteFolderPath = Path.Combine(Application.streamingAssetsPath, relativeFolderPath); |
|
|
|
//Converts desired path into byte array |
|
byte[] pngBytes = File.ReadAllBytes(absoluteFolderPath); |
|
|
|
//Creates texture and loads byte array data to create image |
|
Texture2D texture2D = new Texture2D(2, 2); |
|
texture2D.LoadImage(pngBytes); |
|
|
|
texture2D.name = Path.GetFileNameWithoutExtension(absoluteFolderPath); |
|
|
|
return texture2D; |
|
} |
|
|
|
public static Sprite LoadSpriteFromPath(string relativeFolderPath) |
|
{ |
|
Texture2D texture2d = LoadTexture2dFromPath(relativeFolderPath); |
|
|
|
return ConvertTexture2dToSprite(texture2d, PIVOT, PIXELS_PER_UNIT); |
|
} |
|
|
|
public static List<Sprite> LoadSpritesFromFolder(string relativeFolderPath = "", SearchOption searchOption = SearchOption.AllDirectories) |
|
{ |
|
List<Texture2D> texture2dList = LoadTextures2dFromFolder(relativeFolderPath, searchOption); |
|
|
|
List<Sprite> spriteList = new List<Sprite>(); |
|
|
|
foreach (Texture2D texture2d in texture2dList) |
|
{ |
|
spriteList.Add(ConvertTexture2dToSprite(texture2d, PIVOT, PIXELS_PER_UNIT)); |
|
} |
|
|
|
return spriteList; |
|
} |
|
|
|
private static Sprite ConvertTexture2dToSprite(Texture2D texture2D, Vector2 pivot, float pixelsPerUnit = 100f) |
|
{ |
|
if (texture2D == null) |
|
{ |
|
return null; |
|
}; |
|
|
|
Sprite fromTex = Sprite.Create(texture2D, new Rect(0.0f, 0.0f, texture2D.width, texture2D.height), pivot, pixelsPerUnit); |
|
|
|
fromTex.name = texture2D.name; |
|
|
|
return fromTex; |
|
} |
|
} |