Unity3D教程:Unity3D游戏内的付费

一、搭建号沙盒环境

  二、IAP付费流程图:

总体流程图如下:

Unity3D教程:Unity3D游戏内的付费

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

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小姑娘很大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值