- Home /
Importing Textures not working quite right with LoadAssetAtPath?
Hi All
Iam trying to import textures manually from a text file with the code:
Texture2D ImportTexture(string path) { string destpath = System.IO.Directory.GetCurrentDirectory() + "/";
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(assetPath) + "/Textures");
string assetpath = Path.GetDirectoryName(assetPath) + "/Textures/" + Path.GetFileName(path);
File.Copy(path, destpath + assetpath, true);
AssetDatabase.Refresh();
return (Texture2D)AssetDatabase.LoadAssetAtPath(assetpath, typeof(Texture2D));
}
The problem being the textures get added and show up in Unity but the method LoadAssetAtPath returns a 'null' even though the texture is there, but oddly if I do a reimport then LoadAssetAtPath works fine and returns the Asset. Am I missing a step somewhere? Thanks Chris
Answer by Mike 3 · Jun 11, 2010 at 09:36 AM
It takes time for the asset importer to import the file, which I think happens asynchronously before calling back into the user importer scripts
You could try waiting a short while before trying again
Answer by inewland · Aug 28, 2014 at 09:40 PM
I fixed this by calling Refresh() twice, like:
AssetDatabase.Refresh();
AssetDatabase.Refresh();
Answer by James 4.du · Jun 11, 2010 at 07:46 AM
Yeah I get the same thing. I need to reimport once in a while. Must be a bug.
Your answer
![](https://koobas.hobune.stream/wayback/20220612122353im_/https://answers.unity.com/themes/thub/images/avi.jpg)