Add placementInUse to manange requested placementId
Add placementInUse to manange requested placementId
MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnity.m
adapters/Unity/UnityAdapter/GADMAdapterUnity.m
if (_bannerAd != nil) {
[_bannerAd stopBeingDelegate];
}
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse removeObject:[self getPlacementID]];
[UnityAds removeDelegate:self];
}
NSError *error = GADUnityErrorWithDescription(@"Game ID and Placement ID cannot be nil.");
[strongConnector adapter:self didFailAd:error];
return;
+ } else if ([[GADMAdapterUnitySingleton sharedInstance].placementsInUse containsObject:_placementID]) {
+ NSString *errorMsg = @"An ad is already loading for placement ID: ";
+ errorMsg = [errorMsg stringByAppendingString:_placementID];
+ NSError *error = GADUnityErrorWithDescription(errorMsg);
+ [strongConnector adapter:self didFailAd:error];
+ return;
}
UnitySingletonCompletion completeBlock = ^(UnityAdsError *error, NSString *message) {
NSError *errorWithDescription = GADUnityErrorWithDescription(message);
[strongConnector adapter:self didFailAd:errorWithDescription];
}
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse removeObject:[self getPlacementID]];
return;
}
+
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse addObject:[self getPlacementID]];
[UnityAds addDelegate:self];
[UnityAds load:[self getPlacementID]];
};
}
- (void)presentInterstitialFromRootViewController:(UIViewController *)rootViewController {
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse removeObject:_placementID];
[UnityAds show:rootViewController placementId:_placementID];
}
- (void)unityAdsPlacementStateChanged:(NSString *)placementID
oldState:(UnityAdsPlacementState)oldState
newState:(UnityAdsPlacementState)newState {
+ if([placementID isEqualToString:_placementID]) {
+ if (newState == kUnityAdsPlacementStateNoFill || newState == kUnityAdsPlacementStateDisabled) {
id<GADMAdNetworkConnector> strongNetworkConnector = _networkConnector;
- if (strongNetworkConnector && [placementID isEqualToString:_placementID]) {
- if (newState == kUnityAdsPlacementStateNoFill){
- NSError *errorWithDescription = GADUnityErrorWithDescription(@"NO_FILL");
- [strongNetworkConnector adapter:self didFailAd:errorWithDescription];
- } else if (newState == kUnityAdsPlacementStateDisabled) {
- NSError *errorWithDescription = GADUnityErrorWithDescription(@"PlACEMENT_DISABLED");
- [strongNetworkConnector adapter:self didFailAd:errorWithDescription];
- } else if (newState == kUnityAdsPlacementStateNotAvailable) {
- NSError *errorWithDescription = GADUnityErrorWithDescription(@"PlACEMENT_NOTAVAILABLE");
+ if (strongNetworkConnector) {
+ NSString *errorMsg = @"Failed to load: ";
+ errorMsg = [errorMsg stringByAppendingString:placementID];
+ NSError *errorWithDescription = GADUnityErrorWithDescription(errorMsg);
[strongNetworkConnector adapter:self didFailAd:errorWithDescription];
}
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse removeObject:placementID];
+ }
}
}
- (void)unityAdsDidFinish:(NSString *)placementID withFinishState:(UnityAdsFinishState)state {
+ if ([placementID isEqualToString:_placementID]) {
id<GADMAdNetworkConnector> strongNetworkConnector = _networkConnector;
- if (strongNetworkConnector && [placementID isEqualToString:_placementID]) {
+ if (strongNetworkConnector) {
if (state == kUnityAdsFinishStateCompleted) {
[strongNetworkConnector adapterDidDismissInterstitial:self];
} else if (state == kUnityAdsFinishStateError) {
} else if (state == kUnityAdsFinishStateSkipped) {
[strongNetworkConnector adapterDidDismissInterstitial:self];
}
+ }
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse removeObject:placementID];
[UnityAds removeDelegate:self];
}
}

MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnityRewardedAd.m
adapters/Unity/UnityAdapter/GADMAdapterUnityRewardedAd.m
_adLoadCompletionHandler = nil;
}
return;
+ } else if ([[GADMAdapterUnitySingleton sharedInstance].placementsInUse containsObject:_placementID]) {
+ if (_adLoadCompletionHandler) {
+ NSString *errorMsg = @"An ad is already loading for placement ID: ";
+ errorMsg = [errorMsg stringByAppendingString:_placementID];
+ NSError *error = GADUnityErrorWithDescription(errorMsg);
+ _adLoadCompletionHandler(nil, error);
+ _adLoadCompletionHandler = nil;
+ }
+ return;
}
if (![UnityAds isSupported]) {
UnitySingletonCompletion completeBlock = ^(UnityAdsError *error, NSString *message) {
GADMAdapterUnityRewardedAd *strongSelf = weakSelf;
if (!strongSelf) {
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse removeObject:[self getPlacementID]];
return;
}
if (error) {
NSError *errorWithDescription = GADUnityErrorWithDescription(message);
strongSelf->_adLoadCompletionHandler(nil, errorWithDescription);
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse removeObject:[self getPlacementID]];
return;
}
+
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse addObject:[self getPlacementID]];
[UnityAds addDelegate:strongSelf];
[UnityAds load:[strongSelf getPlacementID]];
};
}
- (void)presentFromViewController:(nonnull UIViewController *)viewController {
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse removeObject:_placementID];
[UnityAds show:viewController placementId:_placementID];
+ id<GADMediationRewardedAdEventDelegate> strongDelegate = _adEventDelegate;
+ if (strongDelegate) {
+ [strongDelegate willPresentFullScreenView];
+ }
}
#pragma mark GADMAdapterUnityDataProvider Methods
#pragma mark - Unity Delegate Methods
- (void)unityAdsDidError:(UnityAdsError)error withMessage:(nonnull NSString *)message {
+ //do nothing.
}
- (void)unityAdsDidFinish:(nonnull NSString *)placementID
withFinishState:(UnityAdsFinishState)state {
+ if ([placementID isEqualToString:_placementID]) {
id<GADMediationRewardedAdEventDelegate> strongDelegate = _adEventDelegate;
- if (strongDelegate && [placementID isEqualToString:_placementID]) {
+ if (strongDelegate) {
if (state == kUnityAdsFinishStateCompleted) {
[strongDelegate didEndVideo];
// Unity Ads doesn't provide a way to set the reward on their front-end. Default to a reward
rewardAmount:[NSDecimalNumber one]];
[strongDelegate didRewardUserWithReward:reward];
} else if (state == kUnityAdsFinishStateError) {
- [strongDelegate willPresentFullScreenView];
NSError *presentError = GADUnityErrorWithDescription(@"Finish State Error");
[strongDelegate didFailToPresentWithError:presentError];
}
[strongDelegate didDismissFullScreenView];
+ }
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse removeObject:placementID];
[UnityAds removeDelegate:self];
}
}
- (void)unityAdsDidStart:(nonnull NSString *)placementID {
id<GADMediationRewardedAdEventDelegate> strongDelegate = _adEventDelegate;
if (strongDelegate && [placementID isEqualToString:_placementID]) {
- [strongDelegate willPresentFullScreenView];
[strongDelegate didStartVideo];
}
}
- (void)unityAdsPlacementStateChanged:(nonnull NSString *)placementID
oldState:(UnityAdsPlacementState)oldState
newState:(UnityAdsPlacementState)newState {
- if (_adLoadCompletionHandler && [placementID isEqualToString:_placementID]) {
- if (newState == kUnityAdsPlacementStateNoFill) {
- NSError *errorWithDescription = GADUnityErrorWithDescription(@"NO_FILL");
- _adLoadCompletionHandler(nil, errorWithDescription);
- } else if (newState == kUnityAdsPlacementStateNotAvailable) {
- NSError *errorWithDescription = GADUnityErrorWithDescription(@"PlACEMENT_NOTAVAILABLE");
- _adLoadCompletionHandler(nil, errorWithDescription);
- } else if (newState == kUnityAdsPlacementStateDisabled) {
- NSError *errorWithDescription = GADUnityErrorWithDescription(@"PlACEMENT_DISABLED");
+ if ([placementID isEqualToString:_placementID]) {
+ if (newState == kUnityAdsPlacementStateNoFill || newState == kUnityAdsPlacementStateDisabled) {
+ if (_adLoadCompletionHandler) {
+ NSString *errorMsg = @"Failed to load: ";
+ errorMsg = [errorMsg stringByAppendingString:placementID];
+ NSError *errorWithDescription = GADUnityErrorWithDescription(errorMsg);
_adLoadCompletionHandler(nil, errorWithDescription);
}
+ [[GADMAdapterUnitySingleton sharedInstance].placementsInUse removeObject:placementID];
+ }
}
}

MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h
@@ -23,6 +23,7 @@ typedef void (^UnitySingletonCompletion)(UnityAdsError *error, NSString *message
@interface GADMAdapterUnitySingleton : NSObject
@property (nonatomic, strong) UnitySingletonCompletion completeBlock;
+@property NSMutableSet<NSString*>* placementsInUse;
/// Shared instance.
+ (instancetype)sharedInstance;

MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m
- (id)init {
self = [super init];
+ if (self) {
+ self.placementsInUse = [[NSMutableSet alloc] init];
+ }
return self;
}

Add placementInUse to manange requested placementId

Add placementInUse to manange requested placementId