Cannot return values from async methods?!?

Its either just too late at night else why can Unity not find 'Task’in the likes of:

private async Task asyncExists(string path) {…

Ive searched high and low on this and there is nothing. Is my machine just screwed up, because this seems a major flaw if you cant return values.

Sorry, using System.Threading.Tasks then :wink:

I don’t think Unity supports threading and/or async… i’m probably wrong though.

Hello,

which platform are you trying to use the code for? It will only work on Windows Store Apps, and you’ll have to wrap the code in #if NET_FX/#endif.

On other platforms, you cannot use async/.NET 4.5 code in scripts. If you want to use it for windows phone, you’ll have to write that code in separate visual studio solution and compile it to DLL, so unity can use it as a plugin.

I have this issue too but not on Windows 8.1 Apps, only on Windows Phone 8 apps.

Why would this code compile fine when exporting a Windows 8.1 App but not a Windows Phone 8 app?

I’ve got my code using async Task inside #if UNITY_METRO and #if UNITY_WP8 defines.

I just don’t understand why the Windows 8.1 app compilation is fine with this but the Windows Phone compilation is not.

That’s because we’re compiling scripts using Mono on Windows Phone.

Oh, does that mean I can go back to using my iOS and Android method of writing files?

Depends. It still won’t let you use methods that microsoft “forbids” using on Windows Phone. Which method were you talking about?

Just the usual File.IO File.WriteAllBytes() etc. If not then it will be kind of annoying to have to write a plugin to have to use the new StorageFile classes that windows 8.1 uses just to do file io. the new StorageFile functions are all async so we need the Task to be available to use them in conjunction with Unity.

For WriteAllBytes on windows phone and windows store apps we made this API:

You can use it like this:

#if UNITY_WINRT
using File = UnityEngine.Windows.File;
#else
using File = System.IO.File;
#endif

void MyMethod()
{
    var text = "Hello, world!";
    var utf8 = System.Text.Encoding.UTF8;

    File.WriteAllBytes("Hello.txt", utf8.GetBytes(text));
}

This should work on all platforms.

The ‘async’ way :slight_smile:

        public static void WriteAllBytes(string path, byte[] data)
        {
#if NETFX_CORE
            path = FixPath(path);
            var thread = PathIO.WriteBytesAsync(path, data).AsTask();
            thread.Wait();
#else
            throw new NotImplementedException();
#endif
        }

Hi Tautvydas

I’m trying this simplest script for (Windows Store Apps platform) to write data to a bin file but it keeps failing with the message
on the VS Simulator :-

File.WriteAllBytes - failed to open TestFile.bin for writing

#if UNITY_WINRT
using File = UnityEngine.Windows.File;
#else	
using File = System.IO.File;
#endif
	
public class BinaryTest2 : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
		Debug.Log ("Creating and writing to file...");
		Byte[] myData = new Byte[4] ;
		File.WriteAllBytes ("TestFile.bin", myData);

	}

any ideas why ? I’m using the latest Unity 4.3.3.

Regards
Shashank

Hi,

Windows Store Apps don’t allow you to write to anywhere you want on disk, except for a few special places. I suggest you try writing the data in this folder:

Thanks, worked like a charm :slight_smile: