本人是一個Unity忠實愛好者,鑑於網上關於Unity的內置付費教程 少之甚少,本人就把本身倒騰過的IAp分享出來,僅供你們參考。php
1、搭建號沙盒環境( 詳細請看:http://xiaominghimi.blog.51cto.com/2614927/706415)html
2、IAP付費流程圖:ios
整體流程圖以下:json
詳細流程圖分爲帶服務端驗證和不帶服務端驗證,本文研究的是帶服務端驗證,流程圖以下:服務器
在Unity中製做IAP主要思想和OC是同樣的,只需更改輸入接口和輸出接口,因此本文主要圍繞如何經過C#以插件的形式,在OC跟C#之間創建鏈接,本質是非託管跟託管之間的鏈接(託管是能夠再公共語言運行庫(CLR)上運行的)。ide
3、接來下我以代碼的形式,簡短的將整個過程貫穿起來。函數
首先點擊付費按鈕以後,調用StoreKit.Install(產品的部分ID);//完整這樣com.XXX.XXXX.iap.50,此處填com.XXX.XXXX.iap。StoreKit.Install(產品的部分ID)會調用插件裏_StoreKitInstall(productIdPrefix),_StoreKitInstall(productIdPrefix)跟OC創建起了鏈接,調用相應的OC函數,最後會在OC一個變量中保存產品的部分ID信息。ui
其次當用戶點了某一個購買按鈕,向OC發送一次請求,當OC受到請求後,會向App store發送請求,驗證當前產品ID是否合法,合法的話,會返回BaseKey,productID,OrderId信息。 UnitySendMessage(「Config」, 「BuyComplate_CallBack」, [json UTF8String]);經過這個函數,完成OC和C#一次回調。以json的形式返回給C#產品的訂單信息。(UnitySendMessage函數中Config是放置購買腳本的GameObject,BuyComplate_CallBack是購買腳本里面的回調函數)this
最後,當客戶端收到產品訂單後,傳給本地服務器,本地服務器拿到產品訂單後,再跟App store進行一次驗證,返回給客戶端驗證結果,客戶端在更新虛擬貨幣信息。url
4、核心代碼
StoreKitPluginEntry.mm和StoreKit.cs是鏈接OC和C#的橋樑,具體代碼以下:
StoreKitPluginEntry.mm
static IAPTransactionObserver *observer; static NSString* CreateNSString (const char* string) { return [NSString stringWithUTF8String:(string ? string : "")]; } extern "C" void _StoreKitInstall(const char *productIdPrefix) { if (observer == nil) { observer = [[IAPTransactionObserver alloc] initWithProductIdPrefix:CreateNSString(productIdPrefix)]; } } extern "C" void _StoreKitBuy(const char *productName) { [observer queuePayment:CreateNSString(productName)]; }
StoreKit.cs
static string productIdPrefix_; public static void Install(string productIdPrefix) { productIdPrefix_ = productIdPrefix; #if UNITY_IPHONE && !UNITY_EDITOR _StoreKitInstall(productIdPrefix); #endif } public static void Buy(string productName) { #if UNITY_IPHONE && !UNITY_EDITOR _StoreKitBuy(productName); #endif } #if UNITY_IPHONE [DllImport("__Internal")] private static extern void _StoreKitInstall(string productIdPrefix); [DllImport ("__Internal")] private static extern void _StoreKitBuy(string productName); #endif
using System.Runtime.InteropServices; [DllImport ("__Internal", EntryPoint="DoSomething")]static extern void DoSomething ();
The 「__Internal」 library name will instruct Mono not to look this up in an external library, but to try to satisfy the symbol referenced (DoSomething) in the current executable image.
Buy.cs購買代碼
public void BuyComplate_CallBack(string result){ string url=""; print("result:"+ result); url+="m=XXX&a=XXX&uid="+player.PlayerID; Hashtable json=(Hashtable)MiniJSON.JsonDecode(result);//json解析器 productInfo=json["productID"].ToString().Substring(productInfo.Length+1);//截取購買的類型 WWWForm resultPost=new WWWForm();//因爲json字節過長,不能採用get方式提交,因此選用Post方式提交 resultPost.AddField("basyKey",json["BaseKey"].ToString()); resultPost.AddField("OrderId",json["OrderId"].ToString()); resultPost.AddField("productID",json["productID"].ToString()); StartCoroutine(BuyComplate(url,str,resultPost)); } /*驗證是否購買成功,若是成功,更新虛擬貨幣數量*/ IEnumerator BuyComplate(string url,string productId,WWWForm buyInfo)// { WWW productInfo=new WWW(url,buyInfo); yield return productInfo; //print("data:"+productInfo.text); if(productInfo.error==null) { Hashtable result=(Hashtable)MiniJSON.JsonDecode(productInfo.text); if(result["status"].ToString()=="ok") { switch(productId) { case "tier1":player.Gemstone+=50;break; } } } }
到此,Unity之IAP講述完畢,如下附上原工程和對應的Json解析器。ECPurchase和 testIap下載
水平有限,不足之處望你們指正。