Texture reseting on runtime

I’ve been following an procedural generation tutorial by Sebastian Lague and i am up to episode 6.
(Playlist link: https://www.youtube.com/playlist?list=PLFt_AvWsXl0eBW2EiBtl_sxmDtSgZBxB3)
on procedural generation. For my mesh Material I am using Unlit/texture which works when in editor, although it then goes to black afterwards when I run it and white when I don’t. Once I click on the editor to regenerate the texture it comes back, but I want it to keep its texture from the editor runtime.

1 Answer

1

If you want to generate a texture using some editor code and save it as an asset, then write some editor tool that produces the texture and then save it, either by using UnityEditor.AssetDatabase (to save an Unity asset) or indirectly by creating a source file (eg. by writing an image file, you can use ImageConversion.EncodeToPNG to generate the bytes. In the latter, you will need to refresh the AssetDatabase and then reassign the references to the newly imported asset (because Unity simply detects it as a new asset in the assets folder).
Note that in both cases, this will write a texture to file, so it will be loaded as normal asset by the built game.

If you want to procedurally generate the texture in runtime (so no loading of pre-generated texture happens) then you just need to ensure that the generator runs before the texture is used - and in the case of switching from edit to play mode in editor, you might want to hook to that event - but that really depends on what is triggering your generator in the first place - and whether you don’t want the pre-baked approach mentioned above.