The Mixed Reality Forums here are no longer being used or maintained.
There are a few other places we would like to direct you to for support, both from Microsoft and from the community.
The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.
For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.
If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.
And always feel free to hit us up on Twitter @MxdRealityDev.
Still having a problem with asynchronous calls using Unity5.40f3-HTP with HoloLens
I have previously found that Microsoft's recommended version of Unity which is HoloLens compatible to be version 5.40f3-HTP. I believe currently this is the latest one available. I have spend this morning carefully going through a procedure using as my starting point the much used recipe of following the tutorial of Holograms 101E
https://developer.microsoft.com/en-us/windows/holographic/holograms_101e
We get the usual sort of picture on the emulator:-
Now the next steps are where trouble starts brewing. Firstly define a new C# script in Assets. This time add in a 'using' clause:-
using Windows.Storage;
In the class it self add in 3 static lines:-
static StorageFolder storageFolder = null; static public StorageFile writefile = null; static public StorageFile readfile = null;
In the 'Start()' routine add in:-
void Start() { set_data_folder(); }
Then add a static procedure to the class:-
static async void set_data_folder() { var storageFolder = await KnownFolders.GetFolderForUserAsync(null /* current user */, KnownFolderId.DocumentsLibrary); }
Then save it. Please note this part of the code is not called by any other part of the coding structure. It is only there as a Monobehavioral Class.
Now go back to Unity and proceed by clicking 'Build Settings' then 'Build. The process proceeds to Visual Studio where you 'Rebuild Solution' then 'Start without Debugging' to the Emulator to get the visuals up as normal - no fault here. However when you look at Unity and click on the 'Console' tab and the base of the editor you get this:-
Now I have a GameObject called 'TestCollection' which really is the OrigamiCollection of MicroSoft's Tutorial 101E. In my case, I left out the 'Spatial Sound' step as I did not want music in the background. To all practical extends my version of TestCollection mirrors that of the original. I dragged my newly formed C# script over the TestCollection gameobject so it would be included as a Component.
Add in to your class a yet-to-be-defined IList object which will be defined in Start()
static public IList dataset;
void Start() { set_data_folder(); dataset = new List<string>(); }
Now, I will add some new 'Load and Save' routines that uses the Windows.Store namespace:-
static async public void create_storage(string name) { try { writefile = await storageFolder.CreateFileAsync(name, CreationCollisionOption.OpenIfExists); if (!StorageApplicationPermissions.FutureAccessList.CheckAccess(writefile)) { //datagramsocket.message_to_send = "Unable to access file: " + writefile.Path; return; } //datagramsocket.message_to_send = String.Format("The file '{0}' opened: ", writefile.Name); } catch (Exception ex) { writefile = null; // I/O errors are reported as exceptions. //datagramsocket.message_to_send = String.Format("Error creating file '{0}': {1}", name, ex.Message); } } static async public void read_storage(string name) { try { var item = await storageFolder.TryGetItemAsync(name); readfile = (item != null) ? (StorageFile)item : null; } catch (Exception e) { } if (readfile != null) { dataset = await FileIO.ReadLinesAsync(readfile); } } static async public void write_line(string textline) { string crlf = "" + (char)(13) + (char)(10); if (writefile == null) { create_storage(datascript); } if (writefile == null) return; await FileIO.AppendTextAsync(writefile, textline + crlf); } static public string save_file_to_permanent_store() { StorageApplicationPermissions.FutureAccessList.Clear(); string writetoken = StorageApplicationPermissions.FutureAccessList.Add(writefile, writefile.Name); //datagramsocket.message_to_send = "Saved: " + writefile.Path + " token: " + writetoken; return writetoken; } static async public void read_lines() { if (readfile == null) return; dataset = await FileIO.ReadLinesAsync(readfile); } Now save and go back to Unity then launch using Build Settings/Build. However please look at the errors this time:-
The last step just increased the errors reported by Unity. I then went to 'CursorMesh' which is a component of Cursor in the main Hierarchy in Unity and in the Inspector panel altered the scale to 2,2,2 for x,y,z respectively. Finally I was able to proceed through Build Settings into VS15 and after Rebuild Solution then clicking 'Start without debugging' I was able (without any errors reported) to get to the HoloLens Emulator that should me this view:-
I believe this must be because of the nature that 'async, await' that are all part of C#5 and Unity's own internal compiler currently only goes up to C#4.4. For me this is a bit of a problem and I must find other ways of filing safely without this bother. I have tried other methods but issues arise there too. I am willing to hear from anyone who can advise on this problem or has other ways of avoiding it in the first place.
Best Answer
-
Hi there @Erodge. Now I found the solution is to jiggle your code until it compiles both in Unity and also in your Visual Studio 15. The problem comes that Unity has its own compiler that supports upto C# version 4.4. On VS currently, you may be using a more recent version (C# version 5 or even 6). Now the difficulty is that Universal Windows Platform methods use more recent commands like async, await and Task. These are very useful ingredients in the making of a multithread and parallel developer's environment which are only in more recent versions of C#. So provided you make your code compatible with Unity by sticking in code snippets like this:
#if WINDOWS_UWP static StorageFolder storageFolder = null; static public StorageFile writefile = null; static public StorageFile readfile = null; #endif
and also:-
#if WINDOWS_UWP static async public Task PutToXML<T>(T objectToSave, string filename) { var serializer = new XmlSerializer(typeof(T)); StorageFile cFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); using (var sWriter = new StreamWriter(await cFile.OpenStreamForWriteAsync())) { serializer.Serialize(sWriter, objectToSave); await sWriter.FlushAsync(); } } #endif
You have to make sure that your code makes sense if these parts are left out of the compilation. For instance if PutToXML above is called somewhere in your code, you will have to have a condition
#if WINDOWS_UWP
there too. You can use an #else statement if you want to write a dummy routine alternative to this for Unity. I find it easier just to exclude it in the first place. Be careful if you want to run it inside Unity as it may not work as you intend or even work at all. However the normal way is going through Visual Studio and this is the environment we are most interested in.I hope this helps you to condition your code accordingly so you satisfy both new commands in VS15 and old compiler in Unity. If you have an error in Unity of course it may not let you proceed as well we know.
5
Answers
Hello WheelChairMan, BUMP
I too am having problems. Anyone have a solution yet? I want to do basic IO to a flat file. I'm using the emulator.
Also, for some of my code that Unity complains about I'm #if blocking it out until I get to the compile phase in VS. Then I comment out a #define. This is required for a SocketIO library I'm using.
Hi there @Erodge. Now I found the solution is to jiggle your code until it compiles both in Unity and also in your Visual Studio 15. The problem comes that Unity has its own compiler that supports upto C# version 4.4. On VS currently, you may be using a more recent version (C# version 5 or even 6). Now the difficulty is that Universal Windows Platform methods use more recent commands like async, await and Task. These are very useful ingredients in the making of a multithread and parallel developer's environment which are only in more recent versions of C#. So provided you make your code compatible with Unity by sticking in code snippets like this:
and also:-
You have to make sure that your code makes sense if these parts are left out of the compilation. For instance if PutToXML above is called somewhere in your code, you will have to have a condition
#if WINDOWS_UWP
there too. You can use an #else statement if you want to write a dummy routine alternative to this for Unity. I find it easier just to exclude it in the first place. Be careful if you want to run it inside Unity as it may not work as you intend or even work at all. However the normal way is going through Visual Studio and this is the environment we are most interested in.I hope this helps you to condition your code accordingly so you satisfy both new commands in VS15 and old compiler in Unity. If you have an error in Unity of course it may not let you proceed as well we know.