I created a TileTextureAtlas asset which houses a Texture2D, the Texture2D is added to the asset by AssetDatabase.AddObjectToAsset.
For normal textures, i would like to import them with the TextureImporter to set the texture-type to normal. However, i didn’t figure out a way how to do this. The TextureImporter can import image files, e.g. a .PNG, but not the Texture2D attached to the TileTextureAtlas asset.
So i’m looking for a solution to either save the atlas-texture as a png file into the TileTextureAtlas-asset and then import it from there, or to save it at a different location and then add it to the asset. But AddObjectToAsset does not seem to work for already existing assets.
Is there a way to add an existing asset as subasset to another asset?
Or is there a way to save a .png file into an asset as subasset and then import it from there?
Thanks for any hints.
You can not save a Texture2D as PNG when you actually save it as asset. Assets are always saved in Unity’s internal asset format. Don’t confuse source files (which have to be imported) with already imported assets.
When you copy an image file into a Unity project, Unity has to actually import that file. The actual source file will be used by the importer to create an actual asset out of the source file. This asset will be stored in the assetdatabase (somewhere inside the library folder of your project). That means imported files inside Unity consists of two parts:
- the actual source file (which isn’t used anymore once imported unless it’s reimported)
- the generated asset (Texture2D for images, prefabs for models, AudioClip for any supported audioformat).
When you create a Texture2D procedurally in the editor and save it as Texture2D asset / subasset there will be no source file at all. You just store the Texture2D asset as it is. There’s no “importer” involved.
If you want to create an actual PNG file you have to save your Texture2D as an actual PNG file inside your Assets folder and let Unity import the file. If you want to change importer settings of an actually imported source file you have to use AssetImporter.GetAtPath which will give you the asset importer for the specified source file.
Im not sure you can change the actual data from the game, So i recomend just reading from/to texture2D from System.IO.File.ReadAllBytes() and Texture2D.LoadRawTextureData()