How to enable bridge desktop Unity App (lower .Net version app) with latest IAP APIs for Windows Store

1. Create C# class library and follow this link to create purchase method:

https://docs.microsoft.com/en-us/windows/uwp/monetize/in-app-purchases-and-trials#desktop

here is the sample code:

public static string PurchaseAddon (…..)

{
     ….
     purchase(o);
     ….

}

static async void purchase(object o)
{  
    
     IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)storeContext;
     var ptr = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
     initWindow.Initialize(ptr);   
     var result = await storeContext.RequestPurchaseAsync((o as PurchaseObject).Productid);
     if (result.ExtendedError != null)
     {
        MessageBox.Show( result.ExtendedError.Message,result.ExtendedError.HResult.ToString());
     }
      switch (result.Status)
     {
         …..
      }          
}

2. Install DLLExport nuget, and claim [DllExport] in the C# project:

image

3. In Unity script, import the above library:

[DllImport(“IAPWrapper”)]
    extern static IntPtr PurchaseAddon (….);

4. Exactly follow below five steps to test IAP:

https://docs.microsoft.com/en-us/windows/uwp/monetize/in-app-purchases-and-trials#testing

Important: The RequestPurchaseAsync must be raised from UI thread, refer to https://docs.microsoft.com/en-us/uwp/api/Windows.Services.Store.StoreContext

Thanks,

Freist

(30)

Leave a Reply

Your email address will not be published. Required fields are marked *