Unity uses IAP to access Apple Store

Use version Unity 2019.4.7 IAP version 2.3.0 Preface take notes to avoid forgetting to learn Unity official document lin...

Use version Unity 2019.4.7
IAP version 2.3.0
Preface take notes to avoid forgetting to learn
Unity official document link UnityIAP official document
First, you need to register at the Apple Developer Center
This is done with reference to official documents Apple Developer Center configuration tutorial
Then import IAP into Unity

You can directly click the button as shown in the figure, or click windows - > General - > services to open it. Select your own organization and click Create. Then select in app purchasing for the line surface. Install IAP

After installation, you can refer to the official tutorial for setting. Here's how to step on the pit.
This is code initialization. This section is not required during Unity test, but it needs to be set when packaging IOS package, because IOS will automatically skip the automatic initialization setting of Unity (it should be described like this. It must be added anyway)

using UnityEngine; using UnityEngine.Purchasing; public class MyIAPManager : IStoreListener { public IStoreController controller; private IExtensionProvider extensions; public MyIAPManager() { var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); builder.AddProduct("com.buyufengbao.1001", ProductType.Consumable, new IDs { {"com.buyufengbao.1001", AppleAppStore.Name} }); UnityPurchasing.Initialize(this, builder); } public void SetStoreID() { } /// <summary> /// Called when Unity IAP is ready to make purchases. /// </summary> public void OnInitialized(IStoreController controller, IExtensionProvider extensions) { Debug.LogError("Initialization succeeded"); this.controller = controller; this.extensions = extensions; } /// <summary> /// Called when Unity IAP encounters an unrecoverable initialization error. /// /// Note that this will not be called if Internet is unavailable; Unity IAP /// will attempt initialization until it becomes available. /// </summary> public void OnInitializeFailed(InitializationFailureReason error) { Debug.LogError(error); } /// <summary> /// Called when a purchase completes. /// /// May be called at any time after OnInitialized(). /// </summary> public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e) { Debug.LogError(e.ToString()); Debug.LogError("Purchase successful"); //todo directly accesses the server interface here and tells them what to buy? return PurchaseProcessingResult.Complete; } /// <summary> /// Called when a purchase fails. /// </summary> public void OnPurchaseFailed(Product i, PurchaseFailureReason p) { Debug.LogError(p); } }

The following codes are configured according to the background data of the apple store. They need to be consistent with the background data of apple, otherwise they will return items such as the product ID cannot be found (it must be consistent anyway)

var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); builder.AddProduct("com.buyufengbao.1001", ProductType.Consumable, new IDs { {"com.buyufengbao.1001", AppleAppStore.Name} }); UnityPurchasing.Initialize(this, builder);

Purchase success and failure will call different functions

public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e) { Debug.LogError(e.ToString()); Debug.LogError("Purchase successful"); //todo directly accesses the server interface here and tells them what to buy? return PurchaseProcessingResult.Complete; } /// <summary> /// Called when a purchase fails. /// </summary> public void OnPurchaseFailed(Product i, PurchaseFailureReason p) { Debug.LogError(p); }

After knowing this, we need to initialize first

using UnityEngine; public class TestInit : MonoBehaviour { MyIAPManager myIAPManager; // Start is called before the first frame update void Start() { myIAPManager= new MyIAPManager(); } public void OnPurchaseClicked(string productId) { myIAPManager.controller.InitiatePurchase(productId); } }

Hang TestInit on an object for initialization

Create a new button to add a click event. The click event is set to the OnPurchaseClicked method of the above code, and the parameter is the commodity ID parameter
In this way, you can package it into XCode, and then package the real machine test. Remember to use the sandbox test account for testing. When the server returns to purchase successfully, remember to interact with its own server, and remember to keep the interactive data confidential.

8 September 2021, 17:32 | Views: 5812

Add new comment

For adding a comment, please log in
or create account

0 comments