Exemplo n.º 1
0
    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    {
        var productId = args.purchasedProduct.definition.id;

        IapProductData product = null;

        if (Products.TryGetValue(productId, out product))
        {
            var currencies = product.currencies;
            foreach (var currency in currencies)
            {
                Save.AddCurrency(currency.id, currency.amount);
            }
            var items = product.items;
            foreach (var item in items)
            {
                Save.AddPurchasedItem(item.name);
            }
        }

        // Return a flag indicating whether this product has completely been received, or if the application needs
        // to be reminded of this purchase at next app launch. Use PurchaseProcessingResult.Pending when still
        // saving purchased products to the cloud, and when that save is delayed.
        PurchaseResult(true);
        return(PurchaseProcessingResult.Complete);
    }
    public override void OnInspectorGUI()
    {
        IapProductData productData = (IapProductData)target;

        serializedObject.Update();

        EditorGUILayout.LabelField(new GUIContent("Product ID:", "Select a product from the IAP catalog"));

#if UNITY_PURCHASING
        var catalog = ProductCatalog.LoadDefaultCatalog();

        m_ValidIDs.Clear();
        m_ValidIDs.Add(kNoProduct);
        foreach (var product in catalog.allProducts)
        {
            m_ValidIDs.Add(product.id);
        }

        int currentIndex = string.IsNullOrEmpty(productData.productId) ? 0 : m_ValidIDs.IndexOf(productData.productId);
        int newIndex     = EditorGUILayout.Popup(currentIndex, m_ValidIDs.ToArray());
        if (newIndex > 0 && newIndex < m_ValidIDs.Count)
        {
            m_ProductIDProperty.stringValue = m_ValidIDs[newIndex];
        }
        else
        {
            m_ProductIDProperty.stringValue = string.Empty;
        }

        if (GUILayout.Button("IAP Catalog..."))
        {
            ProductCatalogEditor.ShowWindow();
        }
#else
        m_ProductIDProperty.stringValue = string.Empty;
        var defaultColor = GUI.color;
        GUI.color = Color.red;
        GUILayout.Label("You must install Unity Purchasing");
        GUI.color = defaultColor;
#endif

        DrawPropertiesExcluding(serializedObject, new string[] { "m_Script" });

        serializedObject.ApplyModifiedProperties();
    }