Unity3D 之IAP

本人是一個Unity忠實愛好者,鑑於網上關於Unity的內置付費教程 少之甚少,本人就把本身倒騰過的IAp分享出來,僅供你們參考。
1、搭建號沙盒環境( 詳細請看:http://xiaominghimi.blog.51cto.com/2614927/706415)

2、IAP付費流程圖:

整體流程圖以下:
<ignore_js_op>111.png 

詳細流程圖分爲帶服務端驗證和不帶服務端驗證,本文研究的是帶服務端驗證,流程圖以下:

<ignore_js_op>12.png 

在Unity中製做IAP主要思想和OC是同樣的,只需更改輸入接口和輸出接口,因此本文主要圍繞如何經過C#以插件的形式,在OC跟C#之間創建鏈接,本質是非託管跟託管之間的鏈接(託管是能夠再公共語言運行庫(CLR)上運行的)。

3、接來下我以代碼的形式,簡短的將整個過程貫穿起來。

首先點擊付費按鈕以後,調用StoreKit.Install(產品的部分ID);//完整這樣com.XXX.XXXX.iap.50,此處填com.XXX.XXXX.iap。StoreKit.Install(產品的部分ID)會調用插件裏_StoreKitInstall(productIdPrefix),_StoreKitInstall(productIdPrefix)跟OC創建起了鏈接,調用相應的OC函數,最後會在OC一個變量中保存產品的部分ID信息。

其次當用戶點了某一個購買按鈕,向OC發送一次請求,當OC受到請求後,會向App store發送請求,驗證當前產品ID是否合法,合法的話,會返回BaseKey,productID,OrderId信息。 UnitySendMessage(「Config」, 「BuyComplate_CallBack」, [json UTF8String]);經過這個函數,完成OC和C#一次回調。以json的形式返回給C#產品的訂單信息。(UnitySendMessage函數中Config是放置購買腳本的GameObject,BuyComplate_CallBack是購買腳本里面的回調函數)

最後,當客戶端收到產品訂單後,傳給本地服務器,本地服務器拿到產品訂單後,再跟App store進行一次驗證,返回給客戶端驗證結果,客戶端在更新虛擬貨幣信息。

4、核心代碼
StoreKitPluginEntry.mm和StoreKit.cs是鏈接OC和C#的橋樑,具體代碼以下:
  1. StoreKitPluginEntry.mm
  2. static IAPTransactionObserver *observer;
  3. static NSString* CreateNSString (const char* string) {
  4.     return [NSString stringWithUTF8String:(string ? string : "")];
  5. }
  6. extern "C" void _StoreKitInstall(const char *productIdPrefix) {
  7.     if (observer == nil) {
  8.         observer = [[IAPTransactionObserver alloc] initWithProductIdPrefix:CreateNSString(productIdPrefix)];
  9.     }
  10. }
  11. extern "C" void _StoreKitBuy(const char *productName) {
  12.     [observer queuePayment:CreateNSString(productName)];
  13. }
複製代碼
  1. StoreKit.cs
  2. static string productIdPrefix_;
  3. public static void Install(string productIdPrefix) {
  4. productIdPrefix_ = productIdPrefix;
  5. #if UNITY_IPHONE && !UNITY_EDITOR
  6. _StoreKitInstall(productIdPrefix);
  7. #endif
  8. }
  9. public static void Buy(string productName) {
  10. #if UNITY_IPHONE && !UNITY_EDITOR
  11. _StoreKitBuy(productName);
  12. #endif
  13. }
  14. #if UNITY_IPHONE
  15. [DllImport("__Internal")]
  16. private static extern void _StoreKitInstall(string productIdPrefix);
  17. [DllImport ("__Internal")]
  18. private static extern void _StoreKitBuy(string productName);
  19. #endif
複製代碼
  1. [DllImport ("__Internal")]
複製代碼
是託管跟非託管的橋樑。如下是Mono官網對 [DllImport ("__Internal")] 的說明 

To make the runtime lookup the symbol in the current executable, use the special library name __Internal like this, in your DllImport attribute:
  1. 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購買代碼
  1. public void BuyComplate_CallBack(string result){
  2. string url="";
  3. print("result:"+ result);
  4. url+="m=XXX&a=XXX&uid="+player.PlayerID;
  5. Hashtable json=(Hashtable)MiniJSON.JsonDecode(result);//json解析器
  6. productInfo=json["productID"].ToString().Substring(productInfo.Length+1);//截取購買的類型
  7. WWWForm resultPost=new WWWForm();//因爲json字節過長,不能採用get方式提交,因此選用Post方式提交
  8. resultPost.AddField("basyKey",json["BaseKey"].ToString());
  9. resultPost.AddField("OrderId",json["OrderId"].ToString());
  10. resultPost.AddField("productID",json["productID"].ToString());
  11. StartCoroutine(BuyComplate(url,str,resultPost));
  12. }
  13. /*驗證是否購買成功,若是成功,更新虛擬貨幣數量*/
  14. IEnumerator BuyComplate(string url,string productId,WWWForm buyInfo)//
  15. {
  16. WWW productInfo=new WWW(url,buyInfo);
  17. yield return productInfo;
  18. //print("data:"+productInfo.text);
  19. if(productInfo.error==null)
  20. {
  21. Hashtable result=(Hashtable)MiniJSON.JsonDecode(productInfo.text);
  22. if(result["status"].ToString()=="ok")
  23. {
  24. switch(productId)
  25. {
  26. case "tier1":player.Gemstone+=50;break;
  27. }
  28. }
  29. }
  30. }
複製代碼
到此,Unity之IAP講述完畢,如下附上原工程和對應的Json解析器。ECPurchase和 testIap下載
水平有限,不足之處望你們指正。

原文連接 http://blog.chukong-inc.com/index.php/2012/01/05/unity3d-%e4%b9%8biap/
相關文章
相關標籤/搜索