- Home /
Add existing asset to asset or save PNG into existing asset
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.
Answer by Bunny83 · Apr 12, 2018 at 02:57 PM
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.
Thanks for you answer. That's exactly my problem: i have an asset, the TileAtlas, which stores a Texture2D (the AtlasTexture) amongst other data. The AtlasTexture is created from the TileAtlas-Script (by assembling many Textures to one big texture). Sometimes the TileAtlas needs to store a set of normal-textures. However, there is no TextureFormat.DXTnm i could set for the AtlasTexture. Thus, the only way to set the AtlasTexture to DXTnm is to get its TextureImporter (and then set the textureimport-type to normal). But i can't get the TextureImporter of the Texture2D subasset (the AtlasTexture) (as you pointed out). InvalidCastException. Thus my thought of solution was to save the AtlasTexture to png (which is no problem at all), then import this PNG, set the importertype to normal and then add it to the Asset again (with AddObjectToAsset). But this doesn't work, since i can't add the imported PNG, which is a main-asset now, to the TileAtlas-Asset. ("Can't add an existing asset to another asset")... That's where i look for a solution/workaround, since i don't want to save the AtlasTexture separate from the TileAtlas.
Well, Unfortunately (afaik) the actual importing procedure is not really available to us. So we can not just let Unity do the conversion. A Texture2D is just pure image data. What kind of data you store in a Texture2D is up to you. You just have to make sure it's stored in the correct format so the shader uses it correctly. You may want to have a look at this question.
The actual required normal map format highly depends on your target platform, your Unity version and of course the used shader.
Already did that... the normal map looks good, if the light shines from the right direction and just dark, if the light shines from another direction. I don't know enough about normalmaps, tangentspace, shaders, opengl, directx etc. to simply find out what's wrong. That's why i wanted to have this handled by the importer. When i used the same normalmap with a texture2d imported by Unities importer, it looked well, no matter the light direction. Also, i assume, that Unities importer would handle all the platform-dependent differences on building. Anyways, thanks a lot for your input!
Answer by hugodeheld · Apr 12, 2018 at 11:59 AM
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()
Thanks for your answer. However, it's not meant to do it from within the game, but from within the editor. (AssetDatabase can only be used in the editor)
Your answer
![](https://koobas.hobune.stream/wayback/20220612160605im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to get TextureImporterType from script? 1 Answer
Create an asset and keep reference? 1 Answer
How to set Sprite or Texture2D AssetPath in editor script? 1 Answer
List of supported texture formats 1 Answer
Texture2DArray Size Problem 0 Answers