When the button is pressed, it saves the skin texture's file name to JSON.
Is the preferred skin a preference of the user? Then did you consider to use PlayerPrefs instead? It provides you with a simple, persistent key-value store.
Then, later, when I want to figure out what to use, I read that field from JSON and call Resources.Load() on the file name.
This sounds like a perfect use-case for Addressables.
With Addressables, you get an input on top of the inspector of every asset where you can specify a key for that asset:
This can be the filename, or it can be any other string. You can then load that asset at runtime using that key. Advantages of addressables over Resources is:
- The assets don't need to be in a folder named Resources. You can make any asset in your project addressable. You can even move those assets around in your project. As long as you don't change the key, the addressable system will still find them.
- Addressables handle asset bundles in a transparent matter. When you registered asset bundles which contain addressable assets, then you can just use
Addressables.LoadAssetAsync
and it will automatically figure out which asset bundle contains the asset you want. - You can even put asset bundles on the web. The addressable system will then download those asset bundles automatically.
- You can also tag addressables with Labels. This allows you to mark all your weapon skin textures and then load them all at runtime. When you need the keys of those assets, then you might want to use a two-step process where you first use
Addressables.LoadResourceLocationsAsync
to get the asset locations and then load each asset withAddressables.LoadAssetAsync(location)
. The resource locations contain the key of the addressable, so it allows you to associate each addressable with its key. A code example can be found in the documentation article loading multiple assets, subsection "Correlating loaded assets to their keys".
So tl;dr:
- Make the skins addressables. Give each addressable an unique key, and assign the label "weapon_skin" to all of them.
- In the menu, load all the addressables with the label "weapon_skin", so you can instantiate a button for each of them.
- In the menu, store the key of the chosen skins in PlayerPrefs.
- In the game, load the addressable with the key you retrieve from PlayerPrefs.