• Unity3D 之IAP


    Published on 2012 年 1 月 5 日, by  in iOS技术.

    本人是一个Unity忠实爱好者,鉴于网上关于Unity的内置付费教程 少之甚少,本人就把自己倒腾过的IAp分享出来,仅供大家参考。

    一、搭建号沙盒环境( 详细请看: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下载

    水平有限,不足之处望大家指正。

  • 相关阅读:
    switch语句
    switch语句
    if语句三种格式
    dowhile语句
    if语句三种格式
    if语句配对
    ansible
    linux系统中网站服务程序(web服务/httpd服务)
    ansible
    ansible
  • 原文地址:https://www.cnblogs.com/lifesteven/p/4415185.html
Copyright © 2020-2023  润新知