I’d like to be able to load assets which are not in a unity style package. E.g. stored in a normal windows directory as separate files. The reason for this is I’d like to ship an executable and then allow users to use their own images (jpg, png etc) in the game without having to import them into Unity. Seems reasonable but I can’t work out how to do it.
JPG and PNG are simple; you can just use the WWW class for that with a few lines of code. Use file:// instead of http:// for local access. For other image formats you’d have to load the bytes and parse them yourself.
the WWW class is definitely the way to go for me. However I’ve run up against a problem. If I place the file I want to load in the root of the project and load it with the following code it works fine:
WWW www = new WWW ("file://TestFile.JPG");
while ([url]www.isDone[/url] != true);
But if I try to place it in a directory of the root then I get an error saying the file can’t be found. The code looks like this:
WWW www = new WWW ("file://TestDirectory/TestFile.JPG");
while ([url]www.isDone[/url] != true);
and the error looks like this:
“Couldn’t open file /TestFile.JPG”
Note how according to the error the directory isn’t in the path, but it is in the string which is getting passed to the WWW constructor! Further experiments revealed that whatever directory immediately follows the “file://” part of the path doesn’t show up in the error message so presumably is being ignored, subsequent directories do, but even if I try and put some dummy directory in there it still won’t load the file.
Anyone got this working and can tell me what I am doing wrong?
BTW I realize I could improve efficiency by using a co-routine for this but performance isn’t an issue for me at the moment.
It’s not a matter of efficiency; if you were getting the files from the web, your code would freeze up in an infinite loop without using coroutines. However, you don’t need isDone at all when loading local files.
Thanks Eric that is a bug but only one I introduced when copying my code to the forum. I’ve corrected as below and the problem still exists:
Debug.Log(Application.dataPath);
WWW www = new WWW("file://" + Application.dataPath + "/test1.JPG");
while ([url]www.isDone[/url] != true);
Texture2D thisTexture = [url]www.texture;[/url]
And here is the error:
Note that the error I am getting has the first part of the path is missing even though it is specified in the parameter I am passing to the WWW class. Has anyone got file:// with www working on a PC running windows? and if so what am I doing wrong?
In my case image loads into the unity environment as well as in exe.
But when i move exe and data folder along with image folder to desktop or to any other machine. It doesn’t load and instead of image it shows me big question mark.
I’m using Application.dataPath. And i’m working on Windows Platform.
And also when i print Application.dataPath it shows my project path(where my resource files are located i.e. asset folder path) and not the current folder path.
Same issue here. The path seems correct but WWW can’t open the file for some reason.
I’m using Uri to build my path:
var path = Path.Combine(Application.dataPath, "img.png");
var uri = new Uri(path);
var www = new WWW(uri.AbsoluteUri);
while (!www.isDone) { } // don't freak out, I'm intentionally not using yield, doing this in editor
www.LoadImageIntoTexture((Texture2D)texture);
Wherever my path was (local to project or desktop, etc), www just won’t budge.