Legacy Documentation: Version 4.6
Language: English
AppCallbacks class
Windows Store Apps: Missing .NET Types

Windows Store Apps: WinRT API in C# scripts

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

It is possible to use WinRT API directly in Unity scripts. However there are limitations and requirements for this:

  • Scripts must be written in C#, it’s not possible to use WinRT API from UnityScript or Boo
  • Scripts must be compiled using Microsoft’s compiler, not Mono. This requires to set compilation overrides to “Use .NET Core” or “Use .NET Core Partially”, in the later case scripts must not be under “Plugins” or “Standard Assets” folders
  • Because the same script code is also used by Unity Editor (which always uses Mono), all code that uses WinRT API must be under NETFX_CORE define

Note, that NETFX_CORE is defined by Visual Studio when compiling code for Windows Store Apps, so it can be used in any C# code, not just Unity scripts.

Bellow is an example for getting advertising is using WinRT API directly:

using UnityEngine;
public class WinRTAPI : MonoBehaviour {
    void Update() {
        auto adId = GetAdvertisingId();
        // ...
    }

    string GetAdvertisingId() {
        #if NETFX_CORE
            return Windows.System.UserProfile.AdvertisingManager.AdvertisingId;
        #else
            return "";
        #endif
    }
}
AppCallbacks class
Windows Store Apps: Missing .NET Types