Unity Webplayer - access online files during execution

713 views
Skip to first unread message

Craig Peebles

unread,
Apr 7, 2013, 7:34:20 AM4/7/13
to unity3dm...@googlegroups.com
Hi everyone,

I have a question for a Unity3D webplayer project I'm working on, and am hoping that someone here could help out.

The build will be hosted on a website (obviously), and I'm trying to get the app to read a text file (CSV) from the same server that the Unity app is hosted on. I'm using the WWW class to request it, but the app always stalls while trying to open the file (it stays stuck at 0% progress). The same app runs fine in the editor, accessing the file from an online source.

The app is to load the CSV file into a hash table on startup - I know I can just hard-code this if need be, but I also want to be able to dynamically access images and other file types from the same server, rather than having to pre-load all the images - so I'd like to get it working if possible. The CSV file is used to supply filenames etc for these other files.

Does anyone have any experience in this kind of file access? Are there other ways of accessing other online sources (PHP, SQL etc)? I've been reading about the sandboxing that Unity builds in, and while I don't understand all of it, I know it will make things more complicated than I initially thought.

Thanks,
Craig.

Craig Peebles

unread,
Apr 7, 2013, 7:43:54 AM4/7/13
to unity3dm...@googlegroups.com
clarification: when I mention PHP and SQL, I mean: Can I use those or other means to solve the original problem (read text, image, other files during runtime. I only need read access to the files.

Thanks,
Craig.

Ben Panda Smith

unread,
Apr 7, 2013, 8:22:07 AM4/7/13
to unity3dm...@googlegroups.com
Hey Craig. You can't access files from the web build. Look into grabbing the file via the WWW class instead. Sorry for the brevity of this response, I am on my phone :-) it is pretty easy let me know of you need some sample code. 

Cheers!
-Ben

--
You received this message because you are subscribed to the Google Groups "Unity 3D Melbourne" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unity3dmelbour...@googlegroups.com.
To post to this group, send email to unity3dm...@googlegroups.com.
Visit this group at http://groups.google.com/group/unity3dmelbourne?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ben Panda Smith

unread,
Apr 7, 2013, 8:32:42 AM4/7/13
to unity3dm...@googlegroups.com
Heh. I should never try to type responses on my phone. What I meant to say was that you should use the www class and access the file as if it were a web page. Tho re reading your initial question it sounds like you are doing that already. Hmmm. 

Cheers!
-Ben


On 07/04/2013, at 9:43 PM, Craig Peebles <craig.n...@gmail.com> wrote:

--

Craig Peebles

unread,
Apr 7, 2013, 8:46:06 AM4/7/13
to unity3dm...@googlegroups.com
Thanks Ben! Yes, I'm using the WWW class to request it based on a URL string, and then reading the contents via www.text.

It works fine in the editor, but fails when copied onto the server and run there. The files will be on the same server, and can even be in the same folder, so I think that gets rid of the security problems, is that right?

Maybe I'll add another part to the question: what are good ways to debug a web build (ie read the console logs)? I've found the log file in the Library folder (on mac), but it doesn't seem to be including everything. Are there any other ways to debug?

Thanks,
Craig.

Ben Panda Smith

unread,
Apr 7, 2013, 9:12:13 AM4/7/13
to unity3dm...@googlegroups.com
Remember that the web player runs on the client side so any files on the server will be considered remote files. You can't access them with file:// instead you have to get them via http:// The files location in proximity to where the web player is hosted is irrelevant and the files need to be publicly accessible.

Cheers!
-Ben

Craig Peebles

unread,
Apr 7, 2013, 9:51:22 AM4/7/13
to unity3dm...@googlegroups.com
Okay, got it working :-) Though I'm a little puzzled as to the solution.

For reference for the group, what I was doing (C#):
void Start()
{
WWW request = new WWW(url);
while(!request.isDone)
{
Debug.Log("Loading..."+request.progress.ToString());
}
myDataString = request.text;
parseData();
}

All fairly straightforward and simple, this was based on an example implementation I found online, and it worked fine running in the editor. It was the while loop that was screwing things up in the remote build. Replace the while loop with "yield return request", and define the Start function as IEnumerator rather than void (Which I didn't think could be done legally), fixed it immediately.

And I had tried moving all the code out of Start() and into another IEnumerator coroutine, and just calling that from Start, but that didn't seem to work either. I must have fudged something in the other coroutine implementation I created.

Anyway, all looks to be working fine now, and the app is loading and displaying the content as it should. Thanks Ben for your help, and I'll leave this here in case anyone else from the group can benefit in the future :-)

Cheers,
Craig.
Reply all
Reply to author
Forward
0 new messages