zoukankan      html  css  js  c++  java
  • Unity3D 之IAP

    本人是一个Unity忠实爱好者,鉴于网上关于Unity的内置付费教程 少之甚少,本人就把自己倒腾过的IAp分享出来,仅供大家参考。
    一、搭建号沙盒环境( 详细请看:http://xiaominghimi.blog.51cto.com/2614927/706415)

    二、IAP付费流程图:

    总体流程图如下:
    <ignore_js_op>111.png 

    详细流程图分为带服务端验证和不带服务端验证,本文研究的是带服务端验证,流程图如下:

    <ignore_js_op>12.png 

    在Unity中制作IAP主要思想和OC是一样的,只需更改输入接口和输出接口,所以本文主要围绕如何通过C#以插件的形式,在OC跟C#之间建立连接,本质是非托管跟托管之间的连接(托管是可以再公共语言运行库(CLR)上运行的)。

    三、接来下我以代码的形式,简短的将整个过程贯穿起来。

    首先点击付费按钮之后,调用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进行一次验证,返回给客户端验证结果,客户端在更新虚拟货币信息。

    四、核心代码
    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/
  • 相关阅读:
    软工结对作业
    软工个人博客作业-软件案例分析
    软工个人项目作业
    软工个人博客作业
    软工第一次个人作业
    2019OO第三单元作业总结
    2019OO第二单元作业总结
    提问回顾以及个人总结
    软工结对项目——图形交点PLUS
    个人博客作业-软件案例分析
  • 原文地址:https://www.cnblogs.com/lifesteven/p/4421368.html
Copyright © 2011-2022 走看看