Importing and slicing Sprite Sheets through scripts

Hey!
I’m trying my best to make my game moddable/more scalable and in doing so I want to Load some spritesheets from the resources folder so that a user (or me) could replace them (it’s also not clear to me how the users will be able to add their own .pngs and where I should find them).

The approach I’ve settled on is to have My default spritesheets in a resource folder in my project, and check at startup if any other folder besides “Default” contains the required sprites and load them instead. I know this is counterindicated but my sprite sheets are very small (pixel art) and I figure that it wouldn’t impact loading time too much. The kind of modding I’d like to achieve would be like Kerbal Space Program, where each mod is a folder in the game directory.

However, the problem I run into is that my spritesheets load fine as I filled the import settings in the editor, but when loading the custom ones I don’t seem to have the option to load them as sprite sheet or detail the size of each sub-sprite. No matter what I would still need to modify the pixel-per-units of individual spritesheets as I have a custom LOD system that switches between different spritesheets depending on camera proximity.

I tried looking into TextureImporterSettings but SpriteImportMode only accepts single or manual.

I’m considering instead having all sprites being singles and load them dynamically through naming conventions or through an atlas file (like a .csv or something like that) to details which files belong together. However, that seems less than ideal and doesn’t play that well in the scalability/ ease of modding I’m aiming for).

If you have any insights on how to do this, or what solution I can pursue if I sacrifice certain parts of what I’m aiming for, it would mean the world to me.

Thanks a lot!

There might be something useful to you here:
https://discussions.unity.com/t/755993/5

Looks Good!

Thanks for the link

The only thing I’m running into now is that TextureImporter & TextureImporterSettings seems to be reserved for the editor.
When trying to build the project I get the following build errors:

error CS0246: The type or namespace name 'TextureImporter' could not be found (are you missing a using directive or an assembly reference?)
error CS0246: The type or namespace name 'TextureImporterSettings' could not be found (are you missing a using directive or an assembly reference?)

Yes - Importers are editor only. If you want to create the equivalent of new sprite sheets at runtime you will have to write your own class that returns a Sprite reference for use as usual based on whatever you want - here’s another thread that might help:

1 Like

Awesome!
Thanks a lot, this definitely helped a great deal!
I didn’t know Sprite.Create() existed and it was the missing piece of my puzzle!

Good day to you :smile:

1 Like