상세 컨텐츠

본문 제목

Unity IAP Purcahse code

개발생활/Unity Engine

by 한국인맛집 2020. 12. 18. 12:15

본문

반응형

 

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/

 

[Solved] Purchasing works but can't get localprice from iOS and Android

I tried to do as this thread (https://forum.unity3d.com/threads/unity-iap-get-item-price.391448/) but it didn't work no matter how many times I tried....

forum.unity.com

 

반응형

관련글 더보기