LoadAssetAtPath problem! (Solved)

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

Asset paths are relative to the project folder. Are you trying to load something from outside the project?

Hi Andeeee

Thanks for the help, no the paths are all relative and correct. Seems to be a timing issue, when the textures are copied Unity doesn’t import them right then and there, so I rejigged the importer code a little and got it working with a delayed setting of the texture.

All working now :slight_smile:
Thanks again
Chris

How would one load something from outside the project?

http://answers.unity3d.com/questions/7256/load-texture-at-run-time-from-file