我已经实现了以下代码integrating Unity IAP In Your Game。游戏具有三个购买按钮:消耗品,非消耗品和还原购买。他们在编辑器中工作正常。在Xcode中归档Unity项目后,将其导出到iPhone 5,OS 10.3.3。在iPhone中,每当我单击任何购买按钮void BuyProductID(string productId)
时,功能都会转到其他部分(即BuyProductID FAIL)。我知道,因为如果购买未成功在错误对话框中内置游戏显示,那么我会在此处放置carselect.ShowErrorWhenBuyingDialog();
错误对话框。每次尝试时,都会显示表明未初始化IAP的对话框。
我尝试了多个Internet连接,还恢复了我的iPhone,多次重新安装了应用程序,如果我按与购买相关的三个按钮中的任何一个,它不会提示我输入沙盒信息,它只是向我显示错误对话框。游戏IAP ID是正确的。我正在使用Unity 2017.3.0f3。
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
public class InAppManager : MonoBehaviour, IStoreListener
{
private static IStoreController storeController; // The Unity Purchasing system.
private static IExtensionProvider storeExtensionProvider; // The store-specific Purchasing subsystems.
public static string credit3000 = "3000credit";
public static string unlockAll = "unlockall";
CarSelection carselect;
//public static string kProductIDSubscription = "subscription";
// Apple App Store-specific product identifier for the subscription product.
//private static string kProductNameAppleSubscription = "com.unity3d.subscription.new";
//// Google Play Store-specific product identifier subscription product.
//private static string kProductNameGooglePlaySubscription = "com.unity3d.subscription.original";
void Start()
{
carselect = FindObjectOfType<CarSelection>();
if (storeController == null)
{
InitializePurchasing();
}
}
public void InitializePurchasing()
{
if (IsInitialized())
{
return;
}
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(credit3000, ProductType.Consumable);
builder.AddProduct(unlockAll, ProductType.NonConsumable);
//builder.AddProduct(kProductIDSubscription, ProductType.Subscription, new IDs()
//{
// //{ kProductNameAppleSubscription, AppleAppStore.Name },
// { kProductNameGooglePlaySubscription, GooglePlay.Name },
//});
UnityPurchasing.Initialize(this, builder);
//Debug.Log("abc");
//carselect.ShowBuy300CoinsBT();
//carselect.ShowUnloackAllBT();
}
private bool IsInitialized()
{
// Only say we are initialized if both the Purchasing references are set.
return storeController != null && storeExtensionProvider != null;
}
public void Buy3000Credit()
{
BuyProductID(credit3000);
}
public void BuyUnloackAll()
{
BuyProductID(unlockAll);
}
void BuyProductID(string productId)
{
// If Purchasing has been initialized ...
if (IsInitialized())
{
// ... look up the Product reference with the general product identifier and the Purchasing
// system's products collection.
Product product = storeController.products.WithID(productId);
// If the look up found a product for this device's store and that product is ready to be sold ...
if (product != null && product.availableToPurchase)
{
Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
// ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
// asynchronously.
storeController.InitiatePurchase(product);
}
// Otherwise ...
else
{
// ... report the product look-up failure situation
Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
}
}
// Otherwise ...
else
{
// ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or
// retrying initiailization.
Debug.Log("BuyProductID FAIL. Not initialized.");
carselect.ShowErrorWhenBuyingDialog();
}
}
// Restore purchases previously made by this customer. Some platforms automatically restore purchases, like Google.
// Apple currently requires explicit purchase restoration for IAP, conditionally displaying a password prompt.
public void RestorePurchases()
{
// If Purchasing has not yet been set up ...
if (!IsInitialized())
{
// ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
Debug.Log("RestorePurchases FAIL. Not initialized.");
return;
}
// If we are running on an Apple device ...
if (Application.platform == RuntimePlatform.IPhonePlayer ||
Application.platform == RuntimePlatform.OSXPlayer)
{
// ... begin restoring purchases
Debug.Log("RestorePurchases started ...");
// Fetch the Apple store-specific subsystem.
var apple = storeExtensionProvider.GetExtension<IAppleExtensions>();
// Begin the asynchronous process of restoring purchases. Expect a confirmation response in
// the Action<bool> below, and ProcessPurchase if there are previously purchased products to restore.
apple.RestoreTransactions((result) =>
{
// The first phase of restoration. If no more responses are received on ProcessPurchase then
// no purchases are available to be restored.
Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
});
}
// Otherwise ...
else
{
// We are not running on an Apple device. No work is necessary to restore purchases.
Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
}
}
//
// --- IStoreListener
//
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
// Purchasing has succeeded initializing. Collect our Purchasing references.
//Debug.Log("OnInitialized: PASS");
// Overall Purchasing system, configured with products for this application.
storeController = controller;
// Store specific subsystem, for accessing device-specific store features.
storeExtensionProvider = extensions;
}
public void OnInitializeFailed(InitializationFailureReason error)
{
// Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
if (String.Equals(args.purchasedProduct.definition.id, credit3000, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
// The consumable item has been successfully purchased, add 100 coins to the player's in-game score.
//ScoreManager.score += 100;
Debug.Log("3000 Credit Succefull");
carselect.AddBoughtCoins();
}
// Or ... a non-consumable product has been purchased by this user.
else if (String.Equals(args.purchasedProduct.definition.id, unlockAll, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
Debug.Log("UnloackAll Credit Succefull");
carselect.UnloackAll_RemoveAds();
}
//// Or ... a subscription product has been purchased by this user.
//else if (string.Equals(args.purchasedProduct.definition.id, kProductIDSubscription, StringComparison.Ordinal))
//{
// Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
// // TODO: The subscription item has been successfully purchased, grant this to the player.
//}
// Or ... an unknown product has been purchased by this user. Fill in additional products here....
else
{
Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
}
return PurchaseProcessingResult.Complete;
}
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
FindObjectOfType<CarSelection>().ShowErrorWhenBuyingDialog();
}
}
`