creating assets with Resources instead of AssetDatabase

so i’ve been using AssetDatabase.CreateAsset to save some data (in this case a Texture3D) at runtime from a script. i need to be able to save this asset, and will need to be able to save data in other ways in the future.

apparently, AssetDatabase does not work when you build the game, so you are supposed to use Resources instead. i can’t figure out what the equivalent tool would be to substitute CreateAsset, how can i save an asset to the Resources folder in a build?

You cannot use AssetDatabase in a build. It’s an Editor-only class in the UnityEditor namespace.

You also may not save new assets / resources to an already-built game.

You may perhaps write them instead to the Application.persistentDataPath location.

oh ok, so do i have to have an external path somewhere to save all of the runtime information? what is the c# method for writing to an external file- i need to save a texture3d asset at runtime

File.WriteAllBytes(…)

But … you cannot create new assets. You can save a texture as an image at runtime and load it with ImageConversion.LoadImage() at runtime. But you cannot write a Texture(2D/3D) asset at runtime.

i don’t think i’ll need to make any new assets at runtime, but is there really no way to save a 3d texture?

Unity has some kind of internal asset format for what you call a “3d texture.”

It will be what it is in Unity and nothing else. See the docs for details.

The code to create an asset in that format exists in the UnityEditor namespace.

That creation code will not be in a build.

If you wish to store the contents of what you call a “3d texture” you would need to serialize it yourself in some format, or else use some existing asset such as what you might find on the Unity asset store or on github.

If Texture3D is anything like Texture2D it will have a way to “get all bytes” in some kind of format (raw, png, whatever) that you can then save. And then recreate the texture from those bytes, or from an image (likely via ImageConversion class). Check the Scripting manual.