|
| 1 | +using HmsPlugin; |
| 2 | +using HuaweiMobileServices.IAP; |
| 3 | +using HuaweiMobileServices.Id; |
| 4 | +using HuaweiMobileServices.Utils; |
| 5 | +using System.Collections.Generic; |
| 6 | +using UnityEngine; |
| 7 | +using UnityEngine.SceneManagement; |
| 8 | +using UnityEngine.UI; |
| 9 | + |
| 10 | +/* This demo scene has been created to show how to use IAP and Account kit in multiple scenes. |
| 11 | +* Here are the steps to use this demo. |
| 12 | +* 1. Complete the quick start steps. https://evilminddevs.gitbook.io/hms-unity-plugin/getting-started/quick-start |
| 13 | +* 2. Enable Account kit - IAP from kit settings. https://evilminddevs.gitbook.io/hms-unity-plugin/getting-started/quick-start/connect-your-game-with-the-hms-kit-managers |
| 14 | +* 3. Make sure you enabled IAP and Account kit services in the AppGallery console. https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/config-agc-0000001050033072#section382112213818 |
| 15 | +* 4. Add These products to your project. (with same productID) https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/config-product-0000001050033076 |
| 16 | +* a. Product ID: coin100 - Type: consumable |
| 17 | +* b. Product ID: coin1000 - Type: consumable |
| 18 | +* c. Product ID: removeAds - Type: non-consumable |
| 19 | +* d. Product ID: premium - Type: subscription (1 week) |
| 20 | +* 5. Add these products to IAP tab. Unity > Huawei > Kit Settings > IAP. Then click create constant classes |
| 21 | +* |
| 22 | +* Have a question? You can ask by creating an issue in our project on Github or via our discord channel. |
| 23 | +*/ |
| 24 | + |
| 25 | + |
| 26 | +public class MainMenuManager : MonoBehaviour |
| 27 | +{ |
| 28 | + string TAG = "[MainMenuManager]:"; |
| 29 | + |
| 30 | + Button playBtn, storeBtn, signinBtn; |
| 31 | + Text userTxt, coinTxt; |
| 32 | + void Start() |
| 33 | + { |
| 34 | + Screen.orientation = ScreenOrientation.Landscape; |
| 35 | + |
| 36 | + playBtn = GameObject.Find("Play").GetComponent<Button>(); |
| 37 | + storeBtn = GameObject.Find("Store").GetComponent<Button>(); |
| 38 | + signinBtn = GameObject.Find("Login").GetComponent<Button>(); |
| 39 | + userTxt = GameObject.Find("userText").GetComponent<Text>(); |
| 40 | + coinTxt = GameObject.Find("coinText").GetComponent<Text>(); |
| 41 | + |
| 42 | + coinTxt.text = "Coin: " + PlayerPrefs.GetInt("Coin", 0); |
| 43 | + |
| 44 | +//Note: Enable Store button when InitializeIAPSuccess |
| 45 | + storeBtn.enabled = false; |
| 46 | + |
| 47 | + playBtn.onClick.AddListener(delegate { ClickListener(CLICKENUM.ELSE); }); |
| 48 | + signinBtn.onClick.AddListener(delegate { ClickListener(CLICKENUM.SIGNIN); }); |
| 49 | + storeBtn.onClick.AddListener(delegate { ClickListener(CLICKENUM.STORE); }); |
| 50 | + |
| 51 | + HMSAccountKitManager.Instance.OnSignInSuccess = SignInSuccess; |
| 52 | + HMSAccountKitManager.Instance.OnSignInFailed = SignInFailed; |
| 53 | +//If the user is already logged in to the AppGallery store, it is possible to log in without disturbing the user with silent login. |
| 54 | +//But if not, the 2002 error can be return. |
| 55 | +//Also check for IsSignedIn. This will prevent your user from logging in again and again when they return to the main menu. |
| 56 | + if (HMSAccountKitManager.Instance.IsSignedIn) |
| 57 | + SignInSuccess(HMSAccountKitManager.Instance.HuaweiId); |
| 58 | + else |
| 59 | + HMSAccountKitManager.Instance.SilentSignIn(); |
| 60 | + |
| 61 | + //When user back to mainMenu, this can work for subscriptions & non_consumable products. |
| 62 | + if (HMSIAPManager.Instance.isIapAvailable()) |
| 63 | + { |
| 64 | + if (HMSIAPManager.Instance.isUserOwnThisProduct("premium")) |
| 65 | + { |
| 66 | + //unlock premium features |
| 67 | + AndroidToast.MakeText("You are premium now.").Show(); |
| 68 | + Debug.Log($"{TAG}OnObtainOwnedPurchasesSuccess. You have premium."); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private void SignInFailed(HMSException exception) |
| 74 | + { |
| 75 | + Debug.LogError($"{TAG}User SignInFailed. HMSException:{exception}"); |
| 76 | + if(exception.ErrorCode == 2002) |
| 77 | + HMSAccountKitManager.Instance.SignIn(); |
| 78 | + } |
| 79 | + |
| 80 | + private void SignInSuccess(AuthAccount authAccount) |
| 81 | + { |
| 82 | + Debug.Log($"{TAG}User SignInSuccess DisplayName{authAccount.DisplayName}"); |
| 83 | + signinBtn.onClick.RemoveAllListeners(); |
| 84 | + signinBtn.onClick.AddListener(delegate { ClickListener(CLICKENUM.SIGNOUT); }); |
| 85 | + signinBtn.gameObject.GetComponentInChildren<Text>().text = "Logout"; |
| 86 | + userTxt.text = "Welcome back\n"+ authAccount.DisplayName; |
| 87 | + |
| 88 | +//Init IAP after OnSignInSuccess |
| 89 | + InitIAP(); |
| 90 | + } |
| 91 | + private void InitIAP() |
| 92 | + { |
| 93 | + HMSIAPManager.Instance.OnInitializeIAPSuccess = OnInitializeIAPSuccess; |
| 94 | + HMSIAPManager.Instance.OnObtainOwnedPurchasesSuccess = OnObtainOwnedPurchasesSuccess; |
| 95 | + |
| 96 | +//I don't need productInfos such as price, name, etc. in this scene. If you need productsInfos, you can remove comment from following code. |
| 97 | + //HMSIAPManager.Instance.OnObtainProductInfoSuccess = OnObtainProductInfoSuccess; |
| 98 | + |
| 99 | +/* |
| 100 | +* Important: To use this demo please uncheck Init on start box in IAP tab. (Unity>Huawei>Kit Settings>IAP) The line below does the same. |
| 101 | +*/ |
| 102 | + HMSIAPManager.Instance.InitializeIAP(); |
| 103 | + } |
| 104 | + |
| 105 | + private void OnObtainProductInfoSuccess(IList<ProductInfoResult> list) |
| 106 | + { |
| 107 | + Debug.Log($"{TAG}OnObtainProductInfoSuccess:{list.Count}"); |
| 108 | + } |
| 109 | + |
| 110 | + private void OnInitializeIAPSuccess() |
| 111 | + { |
| 112 | +//Enable Store button after Init success |
| 113 | + storeBtn.enabled = true; |
| 114 | + Debug.Log($"{TAG}OnInitializeIAPSuccess"); |
| 115 | + } |
| 116 | + |
| 117 | + private void OnObtainOwnedPurchasesSuccess(OwnedPurchasesResult result) |
| 118 | + { |
| 119 | + Debug.Log($"{TAG}OnObtainOwnedPurchasesSuccess:{result.ItemList.Count}"); |
| 120 | + foreach(InAppPurchaseData product in result.InAppPurchaseDataList) |
| 121 | + { |
| 122 | + //ConsumptionState: |
| 123 | + // 0: not consumed |
| 124 | + // 1: consumed |
| 125 | + //Kind: |
| 126 | + // 0: consumable |
| 127 | + // 1: non - consumable |
| 128 | + // 2: subscription |
| 129 | + // https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/inapppurchasedata-0000001050137635#section1031573764815 |
| 130 | + if (product.ConsumptionState == 0 && product.Kind == 0) |
| 131 | + { |
| 132 | +// not consumed means that there was a problem in the presentation of the product after the purchase process and the product could not be consumed. |
| 133 | +// This part is mandatory for consumable products. |
| 134 | + ConsumeProduct(product); |
| 135 | + } |
| 136 | + if(product.ProductId == "removeAds") |
| 137 | + { |
| 138 | + //removeAds |
| 139 | + AndroidToast.MakeText("You have removeAds.").Show(); |
| 140 | + Debug.Log($"{TAG}OnObtainOwnedPurchasesSuccess. You have removeAds."); |
| 141 | + } |
| 142 | + else if(product.ProductId == "premium") |
| 143 | + { |
| 144 | + //unlock premium features |
| 145 | + AndroidToast.MakeText("You are premium now.").Show(); |
| 146 | + Debug.Log($"{TAG}OnObtainOwnedPurchasesSuccess. You have premium."); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private void ConsumeProduct(InAppPurchaseData product) |
| 152 | + { |
| 153 | + Debug.Log($"{TAG} ConsumeProduct product:{product.ProductId}"); |
| 154 | +//Product not consumed so it did not given to your user. Make sure they receive the product. |
| 155 | + if (product.ProductId == "coin100") |
| 156 | + { |
| 157 | + int coin = PlayerPrefs.GetInt("Coin", 0) + 100; |
| 158 | + coinTxt.text = "Coin: " + coin; |
| 159 | + PlayerPrefs.SetInt("Coin", coin); |
| 160 | + } |
| 161 | +//Then consume non consumed product. |
| 162 | + HMSIAPManager.Instance.ConsumePurchase(product); |
| 163 | + } |
| 164 | + |
| 165 | + private void ClickListener(CLICKENUM enumValue) |
| 166 | + { |
| 167 | + if (enumValue == CLICKENUM.SIGNIN) |
| 168 | + { |
| 169 | + HMSAccountKitManager.Instance.SignIn(); |
| 170 | + } |
| 171 | + else if (enumValue == CLICKENUM.SIGNOUT) |
| 172 | + { |
| 173 | + HMSAccountKitManager.Instance.SignOut(); |
| 174 | + userTxt.text = "User not Logged in"; |
| 175 | + signinBtn.gameObject.GetComponentInChildren<Text>().text = "Login"; |
| 176 | + signinBtn.onClick.RemoveAllListeners(); |
| 177 | + signinBtn.onClick.AddListener(delegate { ClickListener(CLICKENUM.SIGNIN); }); |
| 178 | + |
| 179 | +//Disable store function after signout. |
| 180 | + storeBtn.enabled = false; |
| 181 | + } |
| 182 | + else if (enumValue == CLICKENUM.STORE) |
| 183 | + { |
| 184 | + SceneManager.LoadScene("Scene1_Store"); |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + AndroidToast.MakeText("This button has no function as this is a demo scene.").Show(); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + enum CLICKENUM |
| 193 | + { |
| 194 | + SIGNIN, |
| 195 | + SIGNOUT, |
| 196 | + ELSE, |
| 197 | + STORE |
| 198 | + } |
| 199 | +} |
0 commit comments