How to allow users to import images from their disk within player build

I am working on a ship builder for my 2D space game, where players can customize every aspect of their ships. However, players will need to provide their own ship sprites. What is the best way to achieve this?

I have already explored two possibilities:

  • Using Addressables: Players would need to download the Unity editor, create an asset bundle of sprites, and then import it into the game. However, downloading the entire Unity editor and handling the import/export process might be too complex for most players.

  • Loading Images Directly: I found a method for loading images directly into the game from here, though I haven’t tested it yet. Will this approach impact performance or compatibility?

public static Texture2D LoadPNG(string filePath) {

    Texture2D tex = null;
    byte[] fileData;

    if (File.Exists(filePath))     {
        fileData = File.ReadAllBytes(filePath);
        tex = new Texture2D(2, 2);
        tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
    }
    return tex;
}

I appreciate any insights or recommendations you can provide.

You quoted the wrong code fragment. :wink:

Same thread has the up-to-date answer, you should be using ImageConversion.LoadImage to which you can directly feed a byte array of a JPG or PNG file.

Performance shouldn’t concern you here as the player selects an image and even if that takes 500 ms it would be expected sort of. From then on it’s not any different than any other Texture2D / Sprite.

1 Like