Search Unity

Alternative way to call native code from unity

Discussion in 'Windows' started by koblykdmitry, Jan 28, 2014.

  1. koblykdmitry

    koblykdmitry

    Joined:
    Jan 17, 2014
    Posts:
    14
    Hello!

    We found simpler way than in documentation to write native plugins.
    In this way you don't need to create fake dll with same api for other platforms(and no dll at all).

    At first you need to add interface to your unity project:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Text;
    6.  
    7. namespace WPUnityBridge
    8. {
    9.     public interface IWPUnityBridge
    10.     {
    11.         void Purchase(string productId, Action<string> successCallback, Action<Exception> failureCallback);
    12.         void ConsumePurchase(string productId);
    13.     }
    14.  
    15.     public class Singleton
    16.     {
    17.         public static IWPUnityBridge Instance;
    18.     }
    19. }
    20.  
    Then build windows phone solution and create implementation of this interface. For example:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Windows;
    5. using Windows.ApplicationModel.Store;
    6.  
    7. namespace TestNamespace
    8. {
    9.     public class WPUnityBridgeImpl : WPUnityBridge.IWPUnityBridge
    10.     {
    11.         private MainPage _mainPage = null;
    12.  
    13.         public WPUnityBridgeImpl(MainPage mainPage)
    14.         {
    15.             if (mainPage == null)
    16.             {
    17.                 throw new ArgumentNullException("mainPage");
    18.             }
    19.  
    20.             _mainPage = mainPage;
    21.         }
    22.  
    23.         public void ConsumePurchase(string productId)
    24.         {
    25.              try
    26.             {
    27.                 CurrentApp.ReportProductFulfillment(productId);
    28.             }
    29.             catch
    30.             {
    31.             }
    32.         }
    33.  
    34.         public void Purchase(string productId, Action<string> successCallback, Action<Exception> failureCallback)
    35.         {
    36.              _mainPage.Dispatcher.BeginInvoke(async () =>
    37.             {
    38.                 try
    39.                 {
    40.                     string receipt = await CurrentApp.RequestProductPurchaseAsync(productId, true);
    41.                     if (successCallback != null)
    42.                     {
    43.                         successCallback(receipt);
    44.                     }
    45.                     MessageBox.Show("Purchase succeeded");
    46.                 }
    47.                 catch (Exception ex)
    48.                 {
    49.                     if (failureCallback != null)
    50.                     {
    51.                         failureCallback(ex);
    52.                     }
    53.                     MessageBox.Show("Purchase failed:\n" + ex.Message);
    54.                 }
    55.             });
    56.         }
    57.     }
    58. }
    59.  
    Next you need to modify MainPage.xaml.cs Unit_Loaded(), like that:
    Code (csharp):
    1.  
    2. private void Unity_Loaded()
    3. {
    4.     //auto-generated code
    5.     WPUnityBridge.Singleton.Instance = new WPUnityBridgeImpl(this);
    6. }
    7.  
    Now you can get access to your native methods from unity throught WPUnityBridge.Singleton.Instance. For example:
    Code (csharp):
    1. WPUnityBridge.Singleton.Instance.Purchase("super_product", null, null);