I am working on an app which needs to read .obj format files in the hololens at runtime.
Precisely, What I want to do is to save the .obj file in the hololens, when the app runs, it should access the file through the hololens file system. Putting the file in asset folder and loading by Unity editor is not what I want.
I have searched several forums and topic threads, many give quite similar solutions. I follow some sample codes and write my own codes to read files in hololens. However, it doesn't work.
The relevant parts of my program are as follows:
At the head of script file, import namespace
#if WINDOWS_UWP
using Windows.Storage;
using Windows.Storage.Pickers;
using System.Threading.Tasks;
#endif
The script which load obj file receives Stream type parameter. So I implement this function "OpenFileForRead", which receives a folder path and a file name as parameter, returns file stream.
private static Stream OpenFileForRead(string folderName, string fileName)
{
Stream stream = null;
#if WINDOWS_UWP
Task task = new Task(
async () => {
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folderName);
StorageFile file = await folder.GetFileAsync(fileName);
stream = await file.OpenStreamForReadAsync();
});
task.Start();
task.Wait();
#endif
return stream;
}
And I call this function as follows:
#if WINDOWS_UWP
string objname = "Model.obj";
Stream stream = OpenFileForRead(ApplicationData.Current.RoamingFolder.Path, objname);
loadedObj = new OBJLoader().Load(stream);
#endif
"ApplicationData.Current.RoamingFolder.Path" is mapped to "LocalAppData\SOMEAPP\RoamingState" in the hololens.
So when I deployed this app in hololens, I will upload the "Model.obj" file in device portal, to this app's RoamingState folder. Then I run this app, under ideal circumstance, it should work, unfortunately it doesn't.
The current situation is, the app can be successfully deployed to hololens and run. Other Gameobjects can function normally in hololens, though it fails to load the .obj file into scene.
By the way, " new OBJLoader().Load(stream); " has been tested in Unity Editor, which can successfully read and load .obj file in local folder on my PC. So it is most likely that the problem lies in the way I read files in hololens. But I surely follows the solutions given by other similar topics. I am confused and have no idea what exactly is going wrong?
Here are some websites I have referenced and copied some codes from:
https://longqian.me/2017/02/08/hololens-file-transfer/
https://forums.hololens.com/discussion/1862/how-to-deploy-and-read-data-file-with-app
https://github.com/Maximilian-Winter/ObjHololensViewer
https://github.com/jmher019/Hololens-OBJRenderer
Nearly all above use similar codes to read files in hololens, though I can't get it through on my device, it is so frustrating!
Untiy Editor: 2018.4.0f1
Hololens: 1st gen