Unity IAP Purchase Code.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.UI;
class IAPPurchase : MonoBehaviour, IStoreListener{
public static IStoreController m_constroller = null;
public static IExtensionProvider m_provider = null;
public static string itemCode1 = "item-produc-1";
public static string itemCode2 = "item-produc-2";
public Button button1;
public Button button2;
string item1Price = "";
string item2Price = "";
// 초기화 해주는 곳.
public void OnInitialized (IStoreController controller, IExtensionProvider extensions){
if(IsInitialized()) return;
Debug.Log ("OnInitialized: PASS");
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(itemCode1,ProductType.Consumable);
builder.AddProduct(itemCode2,ProductType.Consumable);
m_controller = controller;
m_provider = extensions;
// ISO currency Code + Store LocalPrcice
// example item Price = 0.99 , Currency Code = USA
// => USD 0.99
item1Price = m_StoreController.products.WithID (itemCode1).metadata.isoCurrencyCode + " "
+ m_StoreController.products.WithID (itemCode1).metadata.localizedPrice.ToString ();
item2Price = m_StoreController.products.WithID (itemCode1).metadata.isoCurrencyCode + " "
+ m_StoreController.products.WithID (itemCode2).metadata.localizedPrice.ToString ();
}
// 상품 구매 코드를 파라미터로 받아 Purchase 한다.
private bool BuyProductId(string productId){
if(IsInitialized()){
Product p = m_constroller.products.WithID(productId);
if(p != null && p.availableToPurchase){
m_constroller.InitiatePurchase(p);
return true;
}
}
else{
Debug.LogError("Error");
}
return false;
}
public void OnInitialized(IStoreController controller, IExtensionProvider extensions){
m_constroller = controller;
m_provider = extensions;
Debug.Log("PASS");
}
public void OnInitializeFailed(InitializationFailureReason error){
Debug.LogError("Initialize Fail");
}
public void OnPurchaseFailed(Product i, PurchaseFailureReason p){
Debug.LogError("Purchase Error");
}
// 초기화 여부 체크,
public bool IsInitialized(){
return m_constroller != null && m_provider != null;
}
// 버튼1 보여주는곳.
void ShowItem1Price(){
if(!button.activeSelf) return;
button1.transform.Find("Text").getComponent<Text>().text = item1Price;
}
// 버튼 2 가격 보여주는곳.
void ShowItem2Price(){
if(!button.activeSelf) return;
button2.transform.Find("Text").getComponent<Text>().text = item1Price;
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e){
Debug.Log("Purchase ProcessingResult Call");
switch(e.purchasedProduct.definition.id){
case "item-produc-1" :
int coin = PlayerPrefabs.GetInt("COIN",0);
coin += 100;
PlayerPrefabs.SetInt("COIN",coin);
case "item-produc-2" :
int coin = PlayerPrefabs.GetInt("COIN",0);
coin += 500;
PlayerPrefabs.SetInt("COIN",coin);
}
return PurchaseProcessingResult.Complete;
}
코드를 작성하고 런했을때, 에디터에서 다음과 같이 가격이 뜬다면 정상작동된것이다.
"USD 0.01 "
참고 : Unity Forum
forum.unity.com/threads/solved-purchasing-works-but-cant-get-localprice-from-ios-and-android.485178/
Git Hub 계정 생성, Educaion 계정 만들기. (0) | 2021.04.02 |
---|---|
스플레쉬 구현 (0) | 2020.12.22 |
Validating receipts Not found GooglePlayTangle.Data() , AppleTangle.Data(); (0) | 2020.12.18 |
Unity 슬로우모션 , FixedUpdate Controll via script (0) | 2020.12.14 |
Unity IAP Product Local price (0) | 2020.12.02 |
Unity IAP (In App purchasing ) Initialize Error , Product Fail Initialize (0) | 2020.11.27 |