게임 플랫폼 응용프로그래밍
07/21 Unity IAP
박준희
2021. 7. 21. 15:24
728x90
gpgs 로그인이 구현된 것을 기반으로 진행

App.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;
using UnityEngine.SceneManagement;
public class App : MonoBehaviour
{
public Text txtResult;
public Text versionText;
public Text txtid;
public Text txtUserName;
public Text txtState;
public Image thumb;
void Start()
{
versionText.text = Application.version;
Debug.Log("================================> Init GPGS");
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
.Build();
PlayGamesPlatform.InitializeInstance(config);
// recommended for debugging:
PlayGamesPlatform.DebugLogEnabled = true;
// Activate the Google Play Games platform
PlayGamesPlatform.Activate();
Debug.Log("================================> Authenticate");
// authenticate user:
PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptAlways, (result) =>
{
// handle results
Debug.Log("================================>" + result);
Debug.Log("================================>" + Social.localUser);
Debug.Log("================================>" + Social.localUser.authenticated);
this.txtResult.text = result.ToString();
this.txtid.text = Social.localUser.id;
this.txtUserName.text = Social.localUser.userName;
this.txtState.text = Social.localUser.state.ToString();
StartCoroutine(this.WaitForLoadThumb(() =>
{
Debug.Log(Social.localUser.image);
this.thumb.sprite = Sprite.Create(Social.localUser.image, new Rect(0, 0, Social.localUser.image.width, Social.localUser.image.height), Vector2.zero);
this.thumb.SetNativeSize();
}));
});
}
private IEnumerator WaitForLoadThumb(System.Action callback)
{
while (true)
{
if (Social.localUser.image != null)
{
break;
}
yield return null;
}
callback();
}
}
IAP


프로젝트 세팅



다 허용

대상 나이 체크 창이 뜸











로그인 후 옮겨갈 씬을 생성

버튼은 IAP 버튼으로 생성








IAPMain 게임오브젝트 생성후
IAPMain 스크립트 작성
IAPMain .cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Purchasing;
public class IAPMain : MonoBehaviour
{
public IAPButton btnCoin500;
public IAPButton btnRemoveAds;
// Start is called before the first frame update
void Start()
{
this.btnCoin500.onPurchaseComplete.AddListener(new UnityAction<Product>((product) =>
{
Debug.LogFormat("[구매 성공] 코인 500을 획득 했습니다. : ", product.transactionID);
}));
this.btnCoin500.onPurchaseFailed.AddListener(new UnityAction<Product, PurchaseFailureReason>((product, reason) =>
{
Debug.LogFormat("[구매 실패] : {0}, {1}", product.transactionID, reason.ToString());
}));
this.btnRemoveAds.onPurchaseComplete.AddListener(new UnityAction<Product>((product) =>
{
Debug.LogFormat("[구매 성공] 광고를 제거했습니다. : ", product.transactionID);
}));
this.btnRemoveAds.onPurchaseFailed.AddListener(new UnityAction<Product, PurchaseFailureReason>((product, reason) =>
{
Debug.LogFormat("[구매 실패] : {0}, {1}", product.transactionID, reason.ToString());
}));
}
// Update is called once per frame
void Update()
{
}
}
버튼 어사인

App.cs 에 IAP씬으로 이동하는 스크립트 추가
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;
using UnityEngine.SceneManagement;
public class App : MonoBehaviour
{
public Text txtResult;
public Text versionText;
public Text txtid;
public Text txtUserName;
public Text txtState;
public Image thumb;
void Start()
{
versionText.text = Application.version;
Debug.Log("================================> Init GPGS");
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
.Build();
PlayGamesPlatform.InitializeInstance(config);
// recommended for debugging:
PlayGamesPlatform.DebugLogEnabled = true;
// Activate the Google Play Games platform
PlayGamesPlatform.Activate();
Debug.Log("================================> Authenticate");
// authenticate user:
PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptAlways, (result) =>
{
// handle results
Debug.Log("================================>" + result);
Debug.Log("================================>" + Social.localUser);
Debug.Log("================================>" + Social.localUser.authenticated);
this.txtResult.text = result.ToString();
this.txtid.text = Social.localUser.id;
this.txtUserName.text = Social.localUser.userName;
this.txtState.text = Social.localUser.state.ToString();
StartCoroutine(this.WaitForLoadThumb(() =>
{
Debug.Log(Social.localUser.image);
this.thumb.sprite = Sprite.Create(Social.localUser.image, new Rect(0, 0, Social.localUser.image.width, Social.localUser.image.height), Vector2.zero);
this.thumb.SetNativeSize();
}));
SceneManager.LoadScene("IAP");
});
}
private IEnumerator WaitForLoadThumb(System.Action callback)
{
while (true)
{
if (Social.localUser.image != null)
{
break;
}
yield return null;
}
callback();
}
}
버전을 올려서 빌드

apk를 내부 테스트로 출시







녹스에서 실행

adb 로그
adb connect 127.0.0.1:62001
adb logcat -s Unity



728x90