System.IO.Path and AssetDatabase not working on build

I have a script that works right now. When I click on an object it works out the directory the texture is in, finds all the other textures in that folder then replaces it with the next one. This works great, however when I try to build the game as a stand alone it give me an error.

ArgumentException: Invalid path
System.IO.Path.GetDirectoryName (System.String path)
raycast_checker.Update () (at Assets/raycast_checker.js:16)

Assets/raycast_checker.js(15,30): BCE0005: Unknown identifier: 'AssetDatabase'.

Error building Player because scripts had compiler errors

now that relates to these lines

var FullPathName = AssetDatabase.GetAssetPath(myObjectsTexture);
var DirName = System.IO.Path.GetDirectoryName(FullPathName);

I have no idea what I can do to get around this it seems AssetDatabase is just an Editor script, yet it works in my script that isn’t in the Editor folder. Any idea on what I can do to get this to work when I build it?

2 Answers

2

Look in your Resources folder and you might see why…

I’ve got the same problem, the built resource folder contains 2 files.

I’m looking for a solution myself, I’d like to be able to let my artists drop textures into a folder post-build to change textures… I have my reasons why I this would be handy, of course.

I think they never intended for people to be using the functions this way… They probably only expected people to create and use text files to store saved game data, not for people to hot-swap textures.

AssetDatabase is an editor API. If your script works in the editor, then it’s just luck that it’s not in the Editor folder. You’ll need to find a different way.

(And that way is to use the WWW class to load the new assets from disk, and run around the scene finding what objects use the “old” texture, and swap them for the new one. There’s no run-time hot swapping, but you can certainly load/change textures at run time.)