Hello ,I’m trying to integrating Unity inapp serivice and got the next problem:
I created project on google play (now it is only in Alpha-testing with no-publishing) and i created in-app content for purchasing.
In Editor it work well,but on device,it go to “OnInitializeFailed” and err is “InitializationFailureReason.NoProductsAvailable”,so i don’t know how to fix it.
Help ,please.
Editor Pic:
Device Pic:
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.UI;
// Deriving the Purchaser class from IStoreListener enables it to receive messages from Unity Purchasing.
public class Purchaser : MonoBehaviour, IStoreListener
{
private static IStoreController m_StoreController; // The Unity Purchasing system.
private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.
// Product identifiers for all products capable of being purchased:
// "convenience" general identifiers for use with Purchasing, and their store-specific identifier
// counterparts for use with and outside of Unity Purchasing. Define store-specific identifiers
// also on each platform's publisher dashboard (iTunes Connect, Google Play Developer Console, etc.)
// General product identifiers for the consumable, non-consumable, and subscription products.
// Use these handles in the code to reference which product to purchase. Also use these values
// when defining the Product Identifiers on the store. Except, for illustration purposes, the
// kProductIDSubscription - it has custom Apple and Google identifiers. We declare their store-
// specific mapping to Unity Purchasing's AddProduct, below.
public static string Buy_gold_10usd = "com.whypower.gamecenter.google.10usd";
public Text _showLog = null;
void Start()
{
// If we haven't set up the Unity Purchasing reference
if (m_StoreController == null)
{
// Begin to configure our connection to Purchasing
InitializePurchasing();
}
}
public void InitializePurchasing()
{
// If we have already connected to Purchasing ...
if (IsInitialized())
{
// ... we are done here.
return;
}
// Create a builder, first passing in a suite of Unity provided stores.
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
// Add a product to sell / restore by way of its identifier, associating the general identifier
// with its store-specific identifiers.
builder.AddProduct(Buy_gold_10usd, ProductType.Consumable);
// Kick off the remainder of the set-up with an asynchrounous call, passing the configuration
// and this class' instance. Expect a response either in OnInitialized or OnInitializeFailed.
UnityPurchasing.Initialize(this, builder);
}
private bool IsInitialized()
{
// Only say we are initialized if both the Purchasing references are set.
return m_StoreController != null && m_StoreExtensionProvider != null;
}
public void Buygold001()
{
// Buy the consumable product using its general identifier. Expect a response either
// through ProcessPurchase or OnPurchaseFailed asynchronously.
BuyProductID(Buy_gold_10usd);
}
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 = m_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.
m_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.");
}
}
//
// --- 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.
m_StoreController = controller;
// Store specific subsystem, for accessing device-specific store features.
m_StoreExtensionProvider = extensions;
ShowLog("初始化成功");
// 这里可以获取您在AppStore和Google Play 上配置的商品;
ProductCollection products = m_StoreController.products;
Product[ ] all = products.all;
for (int i = 0; i < all.Length; i++)
{
Debug.Log(all<em>.metadata.localizedTitle + "|" + all<em>.metadata.localizedPriceString + "|" + all_.metadata.localizedDescription + "|" + all*.metadata.isoCurrencyCode);*_</em></em>
<em><em><em><em>ShowLog("商品:" + all<em>.metadata.localizedTitle + "|" + all<em>.metadata.localizedPriceString + "|" + all_.metadata.localizedDescription + "|" + all*.metadata.isoCurrencyCode);*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*public void OnInitializeFailed(InitializationFailureReason error)*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*{*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*// Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*ShowLog("初始化失败: " + error);*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*switch (error)*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*{*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*case InitializationFailureReason.AppNotKnown:*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*Debug.LogError("Is your App correctly uploaded on the relevant publisher console?");*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*break;*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*case InitializationFailureReason.PurchasingUnavailable:*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*Debug.Log("Billing disabled! Ask the user if billing is disabled in device settings.");*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*break;*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*case InitializationFailureReason.NoProductsAvailable:*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*Debug.Log("No products available for purchase! Developer configuration error; check product metadata!");*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*break;*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*{*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*// A consumable product has been purchased by this user.*_</em></em></em></em></em></em>
<em><em><em><em><em><em><em>*if (String.Equals(args.purchasedProduct.definition.id, Buy_gold_10usd, StringComparison.Ordinal))*</em></em></em></em></em></em></em>
<em><em><em><em><em><em>_*{*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*// The consumable item has been successfully purchased, add 100 coins to the player's in-game score.*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*ShowLog("购买成功:" + args.purchasedProduct.definition.id);*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*ShowLog("购买成功:" + args.purchasedProduct.definition.storeSpecificId);*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*ShowLog("购买成功:" + args.purchasedProduct.transactionID);*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*ShowLog("购买成功:" + args.purchasedProduct.receipt);*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*// Or ... an unknown product has been purchased by this user. Fill in additional products here....*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*else*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*{*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*// Return a flag indicating whether this product has completely been received, or if the application needs*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*// to be reminded of this purchase at next app launch. Use PurchaseProcessingResult.Pending when still*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*// saving purchased products to the cloud, and when that save is delayed.*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*return PurchaseProcessingResult.Complete;*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*{*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*// A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*// this reason with the user to guide their troubleshooting actions.*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*ShowLog("购买失败:" + failureReason);*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*public void ShowLog(string logT)*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*{*_</em></em></em></em></em></em>
<em><em><em><em><em><em><em>*if (string.IsNullOrEmpty(_showLog.text))*</em></em></em></em></em></em></em>
<em><em><em><em><em><em>_*{*_</em></em></em></em></em></em>
<em><em><em><em><em><em><em>*_showLog.text = logT + "\n";*</em></em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*else*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*{*_</em></em></em></em></em></em>
<em><em><em><em><em><em><em>*_showLog.text += logT + "\n";*</em></em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*}*_</em></em></em></em></em></em>
<em><em><em><em><em><em>_*```*_</em></em></em></em></em></em>




