So I’m making a game where you can customize your character by putting sprites into the streamingAssets folder, and the game will load the sprites into the game.
Right now, each sprite has to be saved as an individual image and imported one-by-one, but I’m trying to make a system where you can make just import 1 large image, and the game will automatically apply spritesheet settings.
Here’s what I’ve got right now. It checks the streamingAssets folder for any image with the filename containing the word “head”, and if so, it turns it into one of the optional sprites for your character’s head.
But as you can see, at this rate I’ll have to do the same for every single body part. So how can I take a sprite from streamingAssets and apply metadata for a spritesheet?
.
public void LoadFiles()
{
path = Application.streamingAssetsPath;
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo fileinfo = dir.GetFiles();
foreach (FileInfo files in fileinfo)
if (files.Name.Contains(“head”))
{
//converts fileinfo into sprite
byte bytes = File.ReadAllBytes(files.FullName);//Location is set before hand
Debug.LogError("byte array");
Texture2D imgTexture = new Texture2D(900, 900, TextureFormat.DXT1, false);
Debug.LogError("textture2d");
imgTexture.filterMode = FilterMode.Bilinear;
imgTexture.LoadImage(bytes);
Sprite head1 = Sprite.Create(imgTexture, new Rect(0, 0, 900, 900), new Vector2(0f, 0f), 1.0f);
}
}
}