My app will feature tokens to fund some in app functions. If someone clicks a menu button to run a paid function and has insufficient tokens, they should be prompted with a new token purchase menu to buy more tokens. After purchasing sufficient tokens, the requested function that prompted the token purchase should then run automatically.
I am having trouble figuring out the best way to make the paid function automatically run after the tokens are purchased. The best way I could think of is as follows.
In my main menus I am instantiating buttons to run paid functions essentially as follows:
GameObject menuOption = Instantiate(Resources.Load<GameObject>("Menu Text Toggle Option")) as GameObject;
//BUILD IN THE CLICK FUNCTION
menuOption.GetComponentInChildren<Button>().onClick.AddListener(delegate {
if (userTokens >= costOfActivity) {
userTokens -= costOfActivity;
Debug.Log("PURCHASED FUNCTION OCCURS HERE");
}
else {
Debug.Log("OPEN TOKEN PURCHASE MENU");
BuildAndOpenTokenPurchaseMenu();
IAPButton[] iapButtonList = tokenPurchaseMenu.GetComponentsInChildren<IAPButton>();
for (int i = 0; i < iapButtonList.Length; i++) {
//ADD PURCHASED FUNCTION TO RUN ON COMPLETION
iapButtonList[i].onPurchaseComplete.AddListener(delegate {
userTokens -= costOfActivity;
Debug.Log("PURCHASED FUNCTION OCCURS");
GameObject.Destroy(tokenPurchaseMenu);
});
}
}
});
So in other words, when you click the action button in the paid function menu, it runs the desired function if you have sufficient tokens. However, if you don't have enough tokens, it generates a token purchase menu (eg. from a prefab) and opens this.
The IAP buttons in this freshly generated token purchase menu would just have actions to buy tokens.But in this script I add the function that needs to complete after that purchase is done as a second listener based on whatever button triggered it to generate and open.
The only way this works though is if I destroy the menu and rebuild it each time. Because otherwise if the token menu survives and is just "hidden" when not needed, I would just be adding more and more listeners to it for cumulative extra functions if it's triggered multiple times.
Destroying the token purchase menu and then re-instantiating solves this, but it's wasteful. I think this method would work. But I wonder if there's a better way to do it.
Does this make sense and how would you approach it?
eg. Is there some way I could use the same approach but just remove the extra added listener at the end rather than nuking the whole token purchase menu each time and rebuilding it from scratch?
Thoughts? Thanks.
So if I'm not clear, my code for general function purchase menu is:
- Instantiate regular menu button in regular purchase function menu.
- Build into that menu button the function to generate and open a token purchase menu if there are inadequate funds.
- Have the token purchase menu generate from its own prefab with only basic token purchase functions built in to run on purchase completion.
- In step #2, also then add a second listener to the token purchase IAP menu buttons in #3 to run whatever function prompted the creation of the token purchase menu.
- Destroy the token purchase menu when done (as it now has extra listeners added to its IAP buttons that I don't want to stick around).