- Home /
File I/O access from Chrome Native Client
With the latest release of Native Client, it now supports disk I/O. This I/O is different than System.File.IO in that it's asynchronous, more analogous to WWW in Unity terminology. From https://developers.google.com/native-client/beta-docs/fileIO
With either of the techniques described above, you can imagine that file IO does not lend itself to a blocking model. As a result, functions like fread/fwrite don't work as intended. To handle file IO in Native Client you must adjust your file manager paradigm to an asynchronous model.
My question is: is there any way to access this functionality from a Unity project compiled for NaCl? Google does provide an example C++ read/write class at http://code.google.com/p/nativeclient-sdk/source/browse/trunk/src/examples/pong/pong.cc, but I have zero experience with C++. We can't just compile this code to a plugin, because as Unity says: "As with the standard webplayer plugin, native C/C++ plugins are not currently supported by NaCl."
Answer by Julien-Lynge · Feb 24, 2012 at 11:45 PM
I posted this to the forums as well, and a Unity Developer was kind enough to provide an answer:
There is currently no native way to do that in Unity with NaCl. I don't expect that we will directly expose NaCl FileIO APIs any time soon, as they don't map well to existing File.IO functionality (as you described), and exposing a specific NaCl File IO class seems to be too special cased. I think the preferred way for us to expose this would be to add the ability to load plugin DLLs in NaCl, but that is unlikely to happen in the short term either (I think that there are more interesting features to be added to NaCl at this point).
So, right now your only option would indeed be using HTML5 file storage using js. From what I can tell with my limited knowledge of HTML5 file storage, this should generally be possible, though you would probably have to encode the data as text to submit it to Unity.
You can read the whole thing here:
http://forum.unity3d.com/threads/124820-File-I-O-in-Native-Client
Answer by Bunny83 · Feb 23, 2012 at 10:23 PM
This is not a question about if it's possible or not. It just doesn't allow you to access local files! All Unity players (standalone / webplayer / Android) are written in native code. In this case there's no problem for the player itself to access everything on the clients PC. The point is that unity created games / apps that are played in the Webplayer aren't allowed to access files.
The Unity3d Webplayer is a trusted piece of software which has to be installed on the clients PC. A game embedded in a website isn't trusted software.
There is no way for content that runs in the Unity webplayer to access local files. There is, will and should be no way to do something like that.
The best i could think of if some kind of drag and drop support, so the user fully controls what files can be read. Here's a feature request for this functionality
I'm not sure I agree with your assessment here. The other webplayer instances have no I/O access because they're sandboxed - it's just not possible to do. Here, NaCl has provided the functionality - it's not random I/O, it's an intentionally 'safe' way that Google has decided to block off disk space within the Native Client sandbox. I've never seen a statement from Unity that they simply won't allow disk access from webplayer if there are no technical limitations.
Furthermore, at this point we're no longer talking about a strict unity webplayer with the trust issues you imply - according to Unity:
"Unity 3.5 offers support to run Unity Web Player content (.unity3d files) using NaCl to allow content to be run without requiring a plugin install in Chrome."
Thus they're providing a wrapper into Native Client, not a browser plugin, and the app is distributed through the Chrome app store. This is somewhat analogous to the iOS/Android model, and Unity certainly doesn't restrict disk access there.
Answer by jeffssmith1 · Feb 25, 2012 at 03:06 AM
It would be helpful if Unity would post a technical page that explains exactly which NaCl features can be leveraged in Unity and some sample code of how to leverage them.
Being able to read/write from disk is a very powerful NaCl feature and would be extremely useful to Unity developers.
UnityAnswers etiquette is that opinions should be posted as comments rather than an answer. This helps with the flow of the Q&A.
I agree that a page of explanation of Unity in NaCl and what is/isn't implemented would be very useful. However, it sounds like NaCl support is still very much in development/flux, so I don't honestly expect anything for a while.
$$anonymous$$y humble apologies for the breach in etiquette.
Your answer
Follow this Question
Related Questions
Problems with webplayer in chrome on OSX 2 Answers
Why cant some users access my game using Chrome? 1 Answer
Unity Web Player Loses Focus in Chrome 0 Answers
WebPlayer Chrome Extension 0 Answers