Published on January 5, 2012, by Wuyuan in iOS technology.
I am a unity loyal enthusiast, in view of the online unity of the built-in paid tutorials little less, I have to share their Daoteng IAP, only for your reference.
First, build the sandbox environment (see more: http://xiaominghimi.blog.51cto.com/2614927/706415)
Second, the IAP payment Flowchart:
The overall flowchart is as follows:
Detailed flowchart is divided into the service-side verification and without service-side verification, the study is with server-side verification, the flowchart is as follows:
The main idea of making IAP in unity is the same as OC, just change the input interface and output interface, so this article focuses on how to establish a connection between OC and C # in the form of a plug-in in C #, which is essentially a connection between unmanaged and managed (managed to run on the common language Runtime (CLR)).
Third, the next I in the form of code, a brief process of the whole.
First click on the Pay button, call Storekit.install (part of the product ID);//complete such com.xxx. xxxx.iap.50, fill in the COM.XXX.XXXX.IAP here. Storekit.install (part ID of the product) calls the plugin _storekitinstall (productidprefix), _storekitinstall (Productidprefix) establishes a connection with OC, The corresponding OC function is called and the part ID information of the product is saved in a variable in OC.
Second, when the user points to a purchase button, send a request to OC, when OC is requested, will send a request to the App Store, verify that the current Product ID is legal, the legal, will return Basekey,productid,orderid information. Unitysendmessage ("Config", "Buycomplate_callback", [JSON utf8string]); This function completes the OC and C # callbacks once. Returns the order information for a C # product in JSON form. (the config in the Unitysendmessage function is the callback function in the purchase script to place the purchase script Gameobject,buycomplate_callback)
Finally, when the client receives the product order and passes it to the local server, the local server gets the product order, authenticates with the App Store, returns to the client to verify the results, and the client updates the virtual currency information.
Iv. Core Code
STOREKITPLUGINENTRY.MM and StoreKit.cs are the bridges connecting OC and C #, with the following 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< Span class= "Br0" > Initwithproductidprefix:creatensstring (productidprefix< Span class= "Br0" >) ]; }}extern "C" void _storekitbuy (const char *productname< Span class= "Br0" >) {[observer queuepayment:creatensstring< Span class= "Br0" > (Productname) ];}
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")] is a managed and unmanaged bridge. The following is a description of [DllImport ("__internal")] from the Mono official website. The runtime lookup the symbol in the current executable, use the spec Ial Library Name
__internalLike this, in your DllImport attribute:
using System< Span class= "pun". runtime. Interopservices; [dllimport (, entrypoint= "dosomething" )] static extern void< Span class= "PLN" > dosomething ();
The "__internal" library name would instruct Mono not-to-look-this-in-external library, but to try to satisfy the SYM Bol referenced (dosomething) in the current executable image.
Buy.cs Purchase 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 parser ProductInfo=json["ProductID"]. Tostring(). Substring(ProductInfo. Length+1);Type of intercept purchase Wwwform Resultpost=New Wwwform();Because JSON bytes are too long to be submitted in Get mode, post is used to submit resultpost. AddField("Basykey"Json["Basekey"]. Tostring()); Resultpost. AddField("OrderId"Json["OrderId"]. Tostring()); Resultpost. AddField("ProductID"Json["ProductID"]. Tostring()); Startcoroutine(buycomplate(URLStr, Resultpost));}/* Verify if the purchase was successful and, if successful, update the virtual currency number */ienumerator Buycomplate(String URL, String productId, Wwwform Buyinfo)//{WWW ProductInfo=New WWW(URL, Buyinfo); yieldReturn ProductInfo;Print ("Data:" +productinfo.text);If(ProductInfo. Error==Null){Hashtable result=(Hashtable (Productinfo.text) ; If[ "status" ]== "OK" {switch (Productid{ Span class= "KW1" >case "Tier1" :p layer+=50;break;} }}} /span>
Here, the IAP of unity is complete, with the original project and the corresponding JSON parser appended below. Ecpurchase and TESTIAP Download
The level is limited, the insufficiency looks everybody to correct.
Unity3d's IAP