Game Development Reference
In-Depth Information
The previous code sets up a delegate that can be used to enact an in-app purchase on the
native platform. When requested in code, it tells whatever object is listening that the user
wishes to purchase an item in the game.
Then, in the platform project, you will hook up to the delegate to perform the action when
the event is raised, for example, on Windows Phone to complete a purchase, you would
need to use the following code:
private void Unity_Loaded()
{
//Hook up the IAP request to platform
InAppPurchase.PurchaseRequested += PurchaseRequested;
}
private void PurchaseRequested(object sender, EventArgs e)
{
var ProductId = (string)sender;
//Purchase requests must be done on UI thread so use
dispatcher
Dispatcher.BeginInvoke(() => PurchaseItem(ProductId));
}
private async void PurchaseItem(string productID)
{
//Do stuff to purchase the item from the store
}
The code hooks up to the previous PurchaseRequested event in the InAp-
pPurchase class, and when a purchase is requested, it is routed to the UI thread and the
item is purchased.
Tip
Each platform handles in-app purchase differently, or you could choose to use a third-
party in-app solution such as Lotaris. The final implementation is up to you.
The example is kept simple just to demonstrate one method for your Unity project to com-
municate with the platform solution.
Search WWH ::




Custom Search