ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 07/21 Unity IAP
    게임 플랫폼 응용프로그래밍 2021. 7. 21. 15:24

    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를 내부 테스트로 출시

     

     

     

     

     

     

     

     

     

     

    여기 ID를 제품 ID에 입력

     

     

     

     

    녹스에서 실행

     

    adb 로그

    adb connect 127.0.0.1:62001

    adb logcat -s Unity

     

    결제하면 확인 메일이 온다

     

    '게임 플랫폼 응용프로그래밍' 카테고리의 다른 글

    07/22 Unity ads 붙이기  (0) 2021.07.22
    06/30 파이어베이스(Firebase) 사용 연습  (0) 2021.06.30
    06/25 웹뷰  (0) 2021.06.25
    06/24 네이버로그인  (0) 2021.06.24
    06/24 GPGS 4 리더보드  (0) 2021.06.24
Designed by Tistory.