TextureImporterType.Sprite does not work
I partly followed a live training video on creating a watch folder that converts dropped image asset to sprites via the texture importer. The video link text shows that this code needs nothing else to work but that seems to be no longer the case so I think this is a bug.
I have also looked up this on various forums, including right here and there are some people asking the same thing with no answers or some people suggesting to use reimport which then sends the watch folder script into an endless loop because you're reimporting the asset which triggers the watch folder and then reimports again.
So the code is supposed to process the imported texture and set its type as Sprite. What actually happens is that it appears to process the texture to a sprite in the inspector but the actual asset in the Asset/ folder remains as a texture, there is no sprite element added to it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class LevelMenuProcessor : AssetPostprocessor {
void OnPostprocessTexture(Texture2D texture)
{
string lowerCaseAssetPath = assetPath.ToLower();
bool isInLevelMenuFolder = lowerCaseAssetPath.IndexOf("/levelmenu/") != -1;
Debug.Log(lowerCaseAssetPath);
if (isInLevelMenuFolder)
{
TextureImporter texImp = (TextureImporter)assetImporter;
texImp.textureType = TextureImporterType.Sprite;
GameObject LevelMenu = new GameObject();
LevelMenu.name = "Level Menu";
Sprite LevelMenuSprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector3(0.5f, 0.5f), 100.0f);
LevelMenu.AddComponent<SpriteRenderer>().sprite = LevelMenuSprite;
}
}
}
Is this a bug or has the API changed since the video was made in 2016? Any idea how to fix?
Thank you :)
Your answer
Follow this Question
Related Questions
Is it possible to create a texture that hides a specific texture? 0 Answers
SetPixel and cloning sprite.texture 0 Answers
Unity display image from assets folder 0 Answers
Switch Texture Type To Sprite. 0 Answers