loading assets which are not in a unity package

Hello,

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.

Can anyone help me please?

regards,

Tony

I don’t know too much about this side of things, but here is a thread where “SavaB” created a runtime .obj importer.

Might help =)

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.

–Eric

thanks guys,

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.

thanks!

Use Application.dataPath.

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.

–Eric

Thanks Eric,

But that doesn’t solve the problem. I tried the following:

Debug.Log(Application.dataPath);
WWW www = new WWW ("file://" + Application.dataPath + "test.JPG");

When I run the code I get the following output:
C:/Documents and Settings/tony/My Documents/InterfaceTest/Assets

Which is correct but I still get the following error that the file cannot be found even though I am sure it’s in the correct place:

“Couldn’t open file /Documents and Settings/tony/My Documents/InterfaceTest/Assetstest.JPG”

Note how the path in the error message is missing the C: . Exactly like before.

I don’t think I’m doing anything wrong? I’m developing on Windows is this a PC specific Unity bug?

regards,

Tony

Look at that more closely, especially near the end.

–Eric

Hi again,

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?

I’m an idiot :sweat_smile: Turns out it was a simple problem with my path. The code works fine as expected. Thanks for the help and patience!

regards,

Tony

hi,

tonyoakden

how did you solve this problem. I’m having same problem. Can you help me out??

Hi,

tonyoakden

I have done the same.

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.

Any idea??

I am getting the same error, how did you solve it tonyoakden? Can you please elaborate?

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.