-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* A bool input has been added to the PurchaseProduct method to prevent it from being consumed optionally. Thus, those who wish can make their own consumption. * isUserOwnThisProduct method was added to see if the user has a specific product. (You need to call after initSuccess) * isIapAvailable method added. * GetAllOwnedPurchasesasList method added. This method will return owned products. (You need to call after initSuccess) * GetProductsList method added. This method will return information on products as a list. (You need to call after initSuccess) **_Reminder:_** You can still use the GetProductInfo method for specific products. * OnObtainOwnedPurchasesSuccess & OnObtainOwnedPurchaseRecordSuccess callbacks fixed. _**Reminder:**_ When testing non-consumable products with a sandbox, if you consume the product, you will not see that you have the product. If you don't consume, you can also see ownedpurchases. Just consume the product before you test repurchase. This is only for sandbox testing, in a normal process, you should consume after purchase. * Second IAP demo scene has been created to show how to use IAP and Account kit in multiple scenes.
- Loading branch information
1 parent
7695426
commit 09d7d4f
Showing
10 changed files
with
8,757 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
199 changes: 199 additions & 0 deletions
199
Assets/Huawei/Demos/IAP/Second Demo-Menu_Store/MainMenuManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
using HmsPlugin; | ||
using HuaweiMobileServices.IAP; | ||
using HuaweiMobileServices.Id; | ||
using HuaweiMobileServices.Utils; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
using UnityEngine.UI; | ||
|
||
/* This demo scene has been created to show how to use IAP and Account kit in multiple scenes. | ||
* Here are the steps to use this demo. | ||
* 1. Complete the quick start steps. https://evilminddevs.gitbook.io/hms-unity-plugin/getting-started/quick-start | ||
* 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 | ||
* 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 | ||
* 4. Add These products to your project. (with same productID) https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/config-product-0000001050033076 | ||
* a. Product ID: coin100 - Type: consumable | ||
* b. Product ID: coin1000 - Type: consumable | ||
* c. Product ID: removeAds - Type: non-consumable | ||
* d. Product ID: premium - Type: subscription (1 week) | ||
* 5. Add these products to IAP tab. Unity > Huawei > Kit Settings > IAP. Then click create constant classes | ||
* | ||
* Have a question? You can ask by creating an issue in our project on Github or via our discord channel. | ||
*/ | ||
|
||
|
||
public class MainMenuManager : MonoBehaviour | ||
{ | ||
string TAG = "[MainMenuManager]:"; | ||
|
||
Button playBtn, storeBtn, signinBtn; | ||
Text userTxt, coinTxt; | ||
void Start() | ||
{ | ||
Screen.orientation = ScreenOrientation.Landscape; | ||
|
||
playBtn = GameObject.Find("Play").GetComponent<Button>(); | ||
storeBtn = GameObject.Find("Store").GetComponent<Button>(); | ||
signinBtn = GameObject.Find("Login").GetComponent<Button>(); | ||
userTxt = GameObject.Find("userText").GetComponent<Text>(); | ||
coinTxt = GameObject.Find("coinText").GetComponent<Text>(); | ||
|
||
coinTxt.text = "Coin: " + PlayerPrefs.GetInt("Coin", 0); | ||
|
||
//Note: Enable Store button when InitializeIAPSuccess | ||
storeBtn.enabled = false; | ||
|
||
playBtn.onClick.AddListener(delegate { ClickListener(CLICKENUM.ELSE); }); | ||
signinBtn.onClick.AddListener(delegate { ClickListener(CLICKENUM.SIGNIN); }); | ||
storeBtn.onClick.AddListener(delegate { ClickListener(CLICKENUM.STORE); }); | ||
|
||
HMSAccountKitManager.Instance.OnSignInSuccess = SignInSuccess; | ||
HMSAccountKitManager.Instance.OnSignInFailed = SignInFailed; | ||
//If the user is already logged in to the AppGallery store, it is possible to log in without disturbing the user with silent login. | ||
//But if not, the 2002 error can be return. | ||
//Also check for IsSignedIn. This will prevent your user from logging in again and again when they return to the main menu. | ||
if (HMSAccountKitManager.Instance.IsSignedIn) | ||
SignInSuccess(HMSAccountKitManager.Instance.HuaweiId); | ||
else | ||
HMSAccountKitManager.Instance.SilentSignIn(); | ||
|
||
//When user back to mainMenu, this can work for subscriptions & non_consumable products. | ||
if (HMSIAPManager.Instance.isIapAvailable()) | ||
{ | ||
if (HMSIAPManager.Instance.isUserOwnThisProduct("premium")) | ||
{ | ||
//unlock premium features | ||
AndroidToast.MakeText("You are premium now.").Show(); | ||
Debug.Log($"{TAG}OnObtainOwnedPurchasesSuccess. You have premium."); | ||
} | ||
} | ||
} | ||
|
||
private void SignInFailed(HMSException exception) | ||
{ | ||
Debug.LogError($"{TAG}User SignInFailed. HMSException:{exception}"); | ||
if(exception.ErrorCode == 2002) | ||
HMSAccountKitManager.Instance.SignIn(); | ||
} | ||
|
||
private void SignInSuccess(AuthAccount authAccount) | ||
{ | ||
Debug.Log($"{TAG}User SignInSuccess DisplayName{authAccount.DisplayName}"); | ||
signinBtn.onClick.RemoveAllListeners(); | ||
signinBtn.onClick.AddListener(delegate { ClickListener(CLICKENUM.SIGNOUT); }); | ||
signinBtn.gameObject.GetComponentInChildren<Text>().text = "Logout"; | ||
userTxt.text = "Welcome back\n"+ authAccount.DisplayName; | ||
|
||
//Init IAP after OnSignInSuccess | ||
InitIAP(); | ||
} | ||
private void InitIAP() | ||
{ | ||
HMSIAPManager.Instance.OnInitializeIAPSuccess = OnInitializeIAPSuccess; | ||
HMSIAPManager.Instance.OnObtainOwnedPurchasesSuccess = OnObtainOwnedPurchasesSuccess; | ||
|
||
//I don't need productInfos such as price, name, etc. in this scene. If you need productsInfos, you can remove comment from following code. | ||
//HMSIAPManager.Instance.OnObtainProductInfoSuccess = OnObtainProductInfoSuccess; | ||
|
||
/* | ||
* 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. | ||
*/ | ||
HMSIAPManager.Instance.InitializeIAP(); | ||
} | ||
|
||
private void OnObtainProductInfoSuccess(IList<ProductInfoResult> list) | ||
{ | ||
Debug.Log($"{TAG}OnObtainProductInfoSuccess:{list.Count}"); | ||
} | ||
|
||
private void OnInitializeIAPSuccess() | ||
{ | ||
//Enable Store button after Init success | ||
storeBtn.enabled = true; | ||
Debug.Log($"{TAG}OnInitializeIAPSuccess"); | ||
} | ||
|
||
private void OnObtainOwnedPurchasesSuccess(OwnedPurchasesResult result) | ||
{ | ||
Debug.Log($"{TAG}OnObtainOwnedPurchasesSuccess:{result.ItemList.Count}"); | ||
foreach(InAppPurchaseData product in result.InAppPurchaseDataList) | ||
{ | ||
//ConsumptionState: | ||
// 0: not consumed | ||
// 1: consumed | ||
//Kind: | ||
// 0: consumable | ||
// 1: non - consumable | ||
// 2: subscription | ||
// https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/inapppurchasedata-0000001050137635#section1031573764815 | ||
if (product.ConsumptionState == 0 && product.Kind == 0) | ||
{ | ||
// 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. | ||
// This part is mandatory for consumable products. | ||
ConsumeProduct(product); | ||
} | ||
if(product.ProductId == "removeAds") | ||
{ | ||
//removeAds | ||
AndroidToast.MakeText("You have removeAds.").Show(); | ||
Debug.Log($"{TAG}OnObtainOwnedPurchasesSuccess. You have removeAds."); | ||
} | ||
else if(product.ProductId == "premium") | ||
{ | ||
//unlock premium features | ||
AndroidToast.MakeText("You are premium now.").Show(); | ||
Debug.Log($"{TAG}OnObtainOwnedPurchasesSuccess. You have premium."); | ||
} | ||
} | ||
} | ||
|
||
private void ConsumeProduct(InAppPurchaseData product) | ||
{ | ||
Debug.Log($"{TAG} ConsumeProduct product:{product.ProductId}"); | ||
//Product not consumed so it did not given to your user. Make sure they receive the product. | ||
if (product.ProductId == "coin100") | ||
{ | ||
int coin = PlayerPrefs.GetInt("Coin", 0) + 100; | ||
coinTxt.text = "Coin: " + coin; | ||
PlayerPrefs.SetInt("Coin", coin); | ||
} | ||
//Then consume non consumed product. | ||
HMSIAPManager.Instance.ConsumePurchase(product); | ||
} | ||
|
||
private void ClickListener(CLICKENUM enumValue) | ||
{ | ||
if (enumValue == CLICKENUM.SIGNIN) | ||
{ | ||
HMSAccountKitManager.Instance.SignIn(); | ||
} | ||
else if (enumValue == CLICKENUM.SIGNOUT) | ||
{ | ||
HMSAccountKitManager.Instance.SignOut(); | ||
userTxt.text = "User not Logged in"; | ||
signinBtn.gameObject.GetComponentInChildren<Text>().text = "Login"; | ||
signinBtn.onClick.RemoveAllListeners(); | ||
signinBtn.onClick.AddListener(delegate { ClickListener(CLICKENUM.SIGNIN); }); | ||
|
||
//Disable store function after signout. | ||
storeBtn.enabled = false; | ||
} | ||
else if (enumValue == CLICKENUM.STORE) | ||
{ | ||
SceneManager.LoadScene("Scene1_Store"); | ||
} | ||
else | ||
{ | ||
AndroidToast.MakeText("This button has no function as this is a demo scene.").Show(); | ||
} | ||
} | ||
|
||
enum CLICKENUM | ||
{ | ||
SIGNIN, | ||
SIGNOUT, | ||
ELSE, | ||
STORE | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Huawei/Demos/IAP/Second Demo-Menu_Store/MainMenuManager.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.