我正在尝试在我的项目中出售物品。但谷歌购买不起作用。在“文本框”中,它说“正在购买”,但那样卡住了,用户无法购买这些物品。
public class StoreMenu : MonoBehaviour, IStoreListener
{
private static StoreMenu Instance { set;get; }
IStoreController controller;
public TextMeshProUGUI Gold_Text;
public string[] product; // I wrote my products in unity side
[SerializeField]
Text StatusInfo;
int GoldNumber;
private void Start()
{
IAPStart();
}
private void Awake()
{
Instance = this;
}
//Build product and IAPPurchasing
private void IAPStart()
{
var module = StandardPurchasingModule.Instance();
ConfigurationBuilder builder = ConfigurationBuilder.Instance(module);
//Adding products
foreach (string item in product)
{
builder.AddProduct(item, ProductType.Consumable);
}
UnityPurchasing.Initialize(this, builder);
}
//Start
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
this.controller = controller;
}
// When cannot get started
public void OnInitializeFailed(InitializationFailureReason error)
{
//OnInitialize Error
StatusInfo.text = ("Error: " + error.ToString());
}
//Purchase failed
public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
{
//WHEN FACE WITH PURCHASE ERROR
StatusInfo.text = ("Error: " + p.ToString());
}
//Purchase result
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
{
GoldNumber = PlayerPrefs.GetInt("Save"); // Getting Golds from save data
//PURCHASE SETTINGS
//Does it check any product name
if (string.Equals(e.purchasedProduct.definition.id, product[0], System.StringComparison.Ordinal))
{
PlayerPrefs.SetInt("Save", GoldNumber + 30);
StatusInfo.text = "30 Gold Purchase Completed";
return PurchaseProcessingResult.Complete;
}
else if (string.Equals(e.purchasedProduct.definition.id, product[1], System.StringComparison.Ordinal))
{
PlayerPrefs.SetInt("Save", GoldNumber + 50);
StatusInfo.text = "50 Gold Purchase Completed";
return PurchaseProcessingResult.Complete;
}
else if (string.Equals(e.purchasedProduct.definition.id, product[2], System.StringComparison.Ordinal))
{
PlayerPrefs.SetInt("Save", GoldNumber + 100);
StatusInfo.text = "100 Gold Purchase Completed";
return PurchaseProcessingResult.Complete;
}
else if (string.Equals(e.purchasedProduct.definition.id, product[3], System.StringComparison.Ordinal))
{
PlayerPrefs.SetInt("Save", GoldNumber + 200);
StatusInfo.text = "200 Gold Purchase Completed";
return PurchaseProcessingResult.Complete;
}
else if (string.Equals(e.purchasedProduct.definition.id, product[4], System.StringComparison.Ordinal))
{
PlayerPrefs.SetInt("Save", GoldNumber + 500);
StatusInfo.text = "500 Gold Purchase Completed";
return PurchaseProcessingResult.Complete;
}
else if (string.Equals(e.purchasedProduct.definition.id, product[5], System.StringComparison.Ordinal))
{
PlayerPrefs.SetInt("Save", GoldNumber + 1000);
StatusInfo.text = "1000 Gold Purchase Completed";
return PurchaseProcessingResult.Complete;
}
else
{
StatusInfo.text = "Waiting For Purchasing...";
return PurchaseProcessingResult.Pending;
}
}
它卡在了这里。在Texbox中,它表示“正在购买”,但没有任何反应。我认为由于此代码“ controller.InitiatePurchase(proc)”而被卡住了。但是我不知道为什么会这样。当项目统一运行时,所有按钮和购买代码都可以正常工作,我可以购买产品。但是在手机上什么也买不到。
//Using here for getting value from buttons.
public void IAPButton(string id)
{
//DOES IT availableToPurchase?s
Product proc = controller.products.WithID(id);
if (proc != null && proc.availableToPurchase)
{
StatusInfo.text = "Buying";
//It stucks over here.
controller.InitiatePurchase(proc);
}
else
{
StatusInfo.text = "Problem";
}
}