Home > OS >  Unity Event Handler Clear all Functions
Unity Event Handler Clear all Functions

Time:12-23

I am using rewarded ads of admob and using Action<bool> parameter to return true or false but the given reward increases 1 every time.

public void showRewarded(Action<bool> giveReward)
{
    if (rewardedAd.IsLoaded())
    {
        rewardedAd.Show();
        rewardedAd.OnUserEarnedReward  = delegate(object sender, Reward reward)
        {
            giveReward(true);
        };
    }
    else
    {
        giveReward(false);
    }

    requestRewarded();
}

I call this method in different places :

        public void watchAdUpgradeFactory()
    {
        AdsController.Instance.showRewarded(delegate(bool b)
        {
            if (b)
            {
                SoundManager.PlaySomeSoundOnce?.Invoke(SoundType.ButtonClick);
                playerStorageSO.ConcretePlayer.GetResource(ResourceType.Coin, 0);
                concreteFactory.Upgrade();
                ConfigureButton();
                checkdAdButton();
            }
        });
    }
        public void watchAdUpgradeTower()
    {
        AdsController.Instance.showRewarded(delegate(bool b)
        {
            if (b)
            {
               
                upgradeTower();
                ConfigureButton();
                checkdAdButton();
            }
        });
    }

This works as intended for the first time but the giveReward(true) stacks up after every time and player gets 1,2,3,4,5... rewards after consequent claims of reward. How can I fix this or is there a better method of achieving the same goal?

CodePudding user response:

I'm assuming, based on your post and your comments below it, that you want to clear out the "OnUserEarnedReward" event each time before you subscribe to it again.

You can't clear out an event outside of its owning class. Events are simply made this way: Outside of their owning class, events only let you "subscribe" and "unsubscribe" to them, that's it.

Theoretically speaking, you can forcefully clear out an event through reflection, like in this example. However, that's probably not a very safe solution, and I don't think it's the best way to go about this.

Your best bet in this case would be manually "unsubscribing" the last reward from the event. You can't do this if you subscribe to the event with an anonymous function / lambda expression, however. Even when two anonymous functions look identical, they're still considered different functions.

You could keep a variable holding that delegate, though, and use that variable to subscribe and then unsubscribe from the event.

CodePudding user response:

I have added a variable of Action to my ad manager script to keep track of the current reward request and added it to the event handler once at the initialization state. Here is the code snippets if anyone comes accross a similar issue.

    private Action<bool> currReward;
    private void initializeAds(){
        ///initialize ads
        rewardedAd.OnUserEarnedReward  = delegate(object sender, Reward reward)
        {
            if (currReward != null)
            {
                currReward(true);
                currReward = null;
            }
       };
    }
    public void showRewarded(Action<bool> rew)
    {
        currReward = rew;
        rewardedAd.Show();
        requestRewarded();
    }
  • Related