Question by
Snownova · Oct 20, 2016 at 12:00 PM ·
c#scripting problemtexture2dimporting assetsprocedural-generation
Using TextureImporter for texture2d generated in script with no path
Ok, so I'm working on something where I want to create a cursor Texture2d by overlaying one texture2d on top of another. Both files are from the Resources folder so external. My issue is that the resulting texture, mergedTexture does not have TextureType = Cursor. Since it is generated, it has no path, so I can't figure out how to use TextureImporter to change its TextureType.
private void loadMyBuildCursor() {
string tilePath = "Assets/Resources/" + this.iconImgURL + ".png";
Texture2D tileTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(tilePath, typeof(Texture2D));
TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(tilePath);
importer.textureType = TextureImporterType.Cursor;
importer.npotScale = TextureImporterNPOTScale.None;
importer.isReadable = true;
importer.spritePixelsPerUnit = 128;
AssetDatabase.ImportAsset(tilePath);
Texture2D mergedTexture = new Texture2D(tileTexture.width, tileTexture.height, TextureFormat.ARGB32, false);
mergedTexture.SetPixels(tileTexture.GetPixels());
//do stuff with merged texture, removed for bloat, so no it's not just tileTexture
mergedTexture.Apply();
this.buildCursor = mergedTexture;
}
Comment
Hi, were you able to find an answer to this? Can Texture Importer be used at runtime?
Hi Shaahmad, I have not gotten an answer for this. However the Texture Importer can be used at runtime, the code above works perfectly, it just throws a warning whenever I use the created image as a cursor, but the cursor displays just fine.