游戏内付费及各种付费插件

来源:互联网 发布:java垃圾桶 编辑:程序博客网 时间:2024/04/30 00:11

一、搭建号沙盒环境( 详细请看:http://xiaominghimi.blog.51cto.com/2614927/706415)

二、IAP付费流程图:

总体流程图如下:

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

在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#的桥梁,具体代码如下:

Source code

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)]; }Source code

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 [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:
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购买代码

Source code

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下载

Unity3D中国移动联通联合付费插件Touch-Pay(人人为我/我为人人)

http://blog.csdn.net/hiramtan/article/details/8995333

先附上开源项目地址:https://github.com/hiramtan/Unity3D-Touch-Pay-Plugin

废话一下:中国移动/中国联通联合推出手机付费SDK“Touch-Pay”,这大大方便了开发者,不用再分别为移动/联通平台的项目嵌入付费SDK。

两年来上线GooglePlay或AppStore都是使用的Unity3D商店中的付费插件(最近才发现github上有开源的付费插件);而上线国内移动/联通/电信平台,以前做的是按照官方开发文档将SDK嵌入然后导jar包到Unity3D中(估计其他公司也是各自为营)。

既然网上大牛成群,如果大家能够做一个Unity3D的付费插件供大家使用(就像开源的GooglePlay/Appstore付费插件一样),那就太好了。

小弟不才,建立了一个目录,希望大牛们(新手当然也可,重在参与)能够参与。


Unity3D Android google 和亚马逊内置付费插件团购 
本人发起prime31团购,打算购入两个插件: 

Amazon In App Purchase Plugin --对于亚马逊平台内置付费的 

In App Billing Plugin    ---对于google play内置付费的 


支付宝无线快捷支付客户端DEMO下载地址:http://download.alipay.com/public/api/base/WS_SECURE_PAY.zip


0 0