Unity 3.4.2 adapter updates
Unity 3.4.2 adapter updates
MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnity.m
adapters/Unity/UnityAdapter/GADMAdapterUnity.m
/// Unity Ads Banner wrapper
GADMAdapterUnityBannerAd *_bannerAd;
-
- /// YES if the adapter is loading.
- BOOL _isLoading;
}
@end
}
- (void)stopBeingDelegate {
- [[GADMAdapterUnitySingleton sharedInstance] stopTrackingDelegate:self];
+ if (_bannerAd != nil) {
[_bannerAd stopBeingDelegate];
}
+ [UnityAds removeDelegate:self];
+}
#pragma mark Interstitial Methods
[strongConnector adapter:self didFailAd:error];
return;
}
- _isLoading = YES;
- [[GADMAdapterUnitySingleton sharedInstance] requestInterstitialAdWithDelegate:self];
+
+ [[GADMAdapterUnitySingleton sharedInstance] initializeWithGameID:_gameID];
+ [UnityAds addDelegate:self];
+ [UnityAds load:_placementID];
}
- (void)presentInterstitialFromRootViewController:(UIViewController *)rootViewController {
- // We will send adapterWillPresentInterstitial callback before presenting unity ad because the ad
- // has already loaded.
- [_networkConnector adapterWillPresentInterstitial:self];
- [[GADMAdapterUnitySingleton sharedInstance]
- presentInterstitialAdForViewController:rootViewController
- delegate:self];
+ [UnityAds show:rootViewController placementId:_placementID];
}
#pragma mark Banner Methods
#pragma mark - Unity Delegate Methods
-- (void)unityAdsPlacementStateChanged:(NSString *)placementId
+- (void)unityAdsPlacementStateChanged:(NSString *)placementID
oldState:(UnityAdsPlacementState)oldState
newState:(UnityAdsPlacementState)newState {
- // This callback is not forwarded to the adapter by the GADMAdapterUnitySingleton and the adapter
- // should use the unityAdsReady: and unityAdsDidError: callbacks to forward Unity Ads SDK state to
- // Google Mobile Ads SDK.
+ id<GADMAdNetworkConnector> strongNetworkConnector = _networkConnector;
+ if (strongNetworkConnector && [placementID isEqualToString:_placementID]) {
+ if (newState == kUnityAdsPlacementStateNoFill){
+ NSError *errorWithDescription = GADUnityErrorWithDescription(@"NO_FILL");
+ [strongNetworkConnector adapter:self didFailAd:errorWithDescription];
+ }
+ }
}
- (void)unityAdsDidFinish:(NSString *)placementID withFinishState:(UnityAdsFinishState)state {
id<GADMAdNetworkConnector> strongNetworkConnector = _networkConnector;
- if (strongNetworkConnector) {
+ if (strongNetworkConnector && [placementID isEqualToString:_placementID]) {
[strongNetworkConnector adapterWillDismissInterstitial:self];
[strongNetworkConnector adapterDidDismissInterstitial:self];
+ [UnityAds removeDelegate:self];
}
}
- (void)unityAdsReady:(NSString *)placementID {
id<GADMAdNetworkConnector> strongNetworkConnector = _networkConnector;
- if (!_isLoading) {
- return;
- }
-
- if (strongNetworkConnector) {
+ if (strongNetworkConnector && [placementID isEqualToString:_placementID]) {
[strongNetworkConnector adapterDidReceiveInterstitial:self];
}
- _isLoading = NO;
}
- (void)unityAdsDidClick:(NSString *)placementID {
// The Unity Ads SDK doesn't provide an event for leaving the application, so the adapter assumes
// that a click event indicates the user is leaving the application for a browser or deeplink, and
// notifies the Google Mobile Ads SDK accordingly.
- if (strongNetworkConnector) {
+ if (strongNetworkConnector && [placementID isEqualToString:_placementID]) {
[strongNetworkConnector adapterDidGetAdClick:self];
[strongNetworkConnector adapterWillLeaveApplication:self];
}
- (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message {
id<GADMAdNetworkConnector> strongNetworkConnector = _networkConnector;
- if (!_isLoading) {
- // Unity Ads show error will only happen after the ad has been loaded. So, we will send
- // dismiss/close callbacks.
- if (error == kUnityAdsErrorShowError) {
if (strongNetworkConnector) {
+ NSError *errorWithDescription = GADUnityErrorWithDescription(message);
+ [strongNetworkConnector adapter:self didFailAd:errorWithDescription];
+ if (error == kUnityAdsErrorShowError) {
[strongNetworkConnector adapterWillDismissInterstitial:self];
[strongNetworkConnector adapterDidDismissInterstitial:self];
}
+ [UnityAds removeDelegate:self];
}
return;
}
- NSError *errorWithDescription = GADUnityErrorWithDescription(message);
- if (strongNetworkConnector) {
- [strongNetworkConnector adapter:self didFailAd:errorWithDescription];
- }
- _isLoading = NO;
+- (void)unityAdsDidStart:(nonnull NSString *)placementID {
+ id<GADMAdNetworkConnector> strongNetworkConnector = _networkConnector;
+ if (strongNetworkConnector && [placementID isEqualToString:_placementID]) {
+ [strongNetworkConnector adapterWillPresentInterstitial:self];
}
-
-- (void)unityAdsDidStart:(nonnull NSString *)placementId {
- // nothing to do
}
@end

MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnityRewardedAd.m
adapters/Unity/UnityAdapter/GADMAdapterUnityRewardedAd.m
_placementID = [_adConfiguration.credentials.settings objectForKey:kGADMAdapterUnityPlacementID];
NSLog(@"Requesting unity rewarded ad with placement: %@", _placementID);
- GADMAdapterUnityRewardedAd __weak *weakSelf = self;
-
if (!_gameID || !_placementID) {
if (_adLoadCompletionHandler) {
NSError *error = GADUnityErrorWithDescription(@"Game ID and Placement ID cannot be nil.");
return;
}
- if ([UnityAds isReady:_placementID]) {
- if (_adLoadCompletionHandler) {
- _adEventDelegate = _adLoadCompletionHandler(self, nil);
- _adLoadCompletionHandler = nil;
- }
- _isLoading = NO;
- } else {
- _isLoading = YES;
- }
-
- [[GADMAdapterUnitySingleton sharedInstance] requestRewardedAdWithDelegate:weakSelf];
+ [[GADMAdapterUnitySingleton sharedInstance] initializeWithGameID:_gameID];
+ [UnityAds addDelegate:self];
+ [UnityAds load:_placementID];
}
- (void)presentFromViewController:(nonnull UIViewController *)viewController {
- if (_isLoading) {
- NSError *presentError = GADUnityErrorWithDescription(@"Ad has not finished loading.");
- [_adEventDelegate didFailToPresentWithError:presentError];
- } else {
- [[GADMAdapterUnitySingleton sharedInstance] presentRewardedAdForViewController:viewController
- delegate:self];
- }
+ [UnityAds show:viewController placementId:_placementID];
}
#pragma mark GADMAdapterUnityDataProvider Methods
#pragma mark - Unity Delegate Methods
- (void)unityAdsDidError:(UnityAdsError)error withMessage:(nonnull NSString *)message {
- if (!_isLoading) {
- if (error == kUnityAdsErrorShowError) {
+ id<GADMediationRewardedAdEventDelegate> strongDelegate = _adEventDelegate;
+ if (strongDelegate && error == kUnityAdsErrorShowError) {
NSError *presentError = GADUnityErrorWithDescription(message);
[_adEventDelegate didFailToPresentWithError:presentError];
}
- return;
- }
if (_adLoadCompletionHandler) {
NSError *errorWithDescription = GADUnityErrorWithDescription(message);
_adLoadCompletionHandler(nil, errorWithDescription);
_adLoadCompletionHandler = nil;
}
- _isLoading = NO;
}
-- (void)unityAdsDidFinish:(nonnull NSString *)placementId
+- (void)unityAdsDidFinish:(nonnull NSString *)placementID
withFinishState:(UnityAdsFinishState)state {
id<GADMediationRewardedAdEventDelegate> strongDelegate = _adEventDelegate;
+ if (strongDelegate && [placementID isEqualToString:_placementID]) {
if (state == kUnityAdsFinishStateCompleted) {
[strongDelegate didEndVideo];
// Unity Ads doesn't provide a way to set the reward on their front-end. Default to a reward
}
[strongDelegate willDismissFullScreenView];
[strongDelegate didDismissFullScreenView];
+ [UnityAds removeDelegate:self];
+ }
}
-- (void)unityAdsDidStart:(nonnull NSString *)placementId {
+- (void)unityAdsDidStart:(nonnull NSString *)placementID {
id<GADMediationRewardedAdEventDelegate> strongDelegate = _adEventDelegate;
+ if (strongDelegate && [placementID isEqualToString:_placementID]) {
[strongDelegate willPresentFullScreenView];
[strongDelegate didStartVideo];
}
-
-- (void)unityAdsReady:(nonnull NSString *)placementId {
- if (!_isLoading) {
- return;
}
- if (_adLoadCompletionHandler) {
+- (void)unityAdsReady:(nonnull NSString *)placementID {
+ if (_adLoadCompletionHandler && [placementID isEqualToString:_placementID]) {
_adEventDelegate = _adLoadCompletionHandler(self, nil);
_adLoadCompletionHandler = nil;
}
- _isLoading = NO;
}
-- (void)unityAdsDidClick:(nonnull NSString *)placementId {
+- (void)unityAdsDidClick:(nonnull NSString *)placementID {
// The Unity Ads SDK doesn't provide an event for leaving the application, so the adapter assumes
// that a click event indicates the user is leaving the application for a browser or deeplink, and
// notifies the Google Mobile Ads SDK accordingly.
id<GADMediationRewardedAdEventDelegate> strongDelegate = _adEventDelegate;
+ if (strongDelegate && [placementID isEqualToString:_placementID]) {
[strongDelegate reportClick];
}
+}
-- (void)unityAdsPlacementStateChanged:(nonnull NSString *)placementId
+- (void)unityAdsPlacementStateChanged:(nonnull NSString *)placementID
oldState:(UnityAdsPlacementState)oldState
newState:(UnityAdsPlacementState)newState {
// This callback is not forwarded to the adapter by the GADMAdapterUnitySingleton and the adapter
// should use the unityAdsReady: and unityAdsDidError: callbacks to forward Unity Ads SDK state to
// Google Mobile Ads SDK.
+ //id<GADMediationRewardedAdEventDelegate> strongDelegate = _adEventDelegate;
+ if (_adLoadCompletionHandler && [placementID isEqualToString:_placementID] && newState == kUnityAdsPlacementStateNoFill) {
+ NSError *errorWithDescription = GADUnityErrorWithDescription(@"NO_FILL");
+ _adLoadCompletionHandler(nil, errorWithDescription);
+ _adLoadCompletionHandler = nil;
+ }
}
@end

MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h
- (void)initializeWithGameID:(NSString *)gameID;
-/// Requests a reward-based video ad with |adapterDelegate|.
-- (void)requestRewardedAdWithDelegate:
- (id<GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate>)adapterDelegate;
-
-/// Presents a reward-based video ad for |viewController| with |adapterDelegate|.
-- (void)presentRewardedAdForViewController:(UIViewController *)viewController
- delegate:
- (id<GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate>)
- adapterDelegate;
-
-/// Configures an interstitial ad with provided |gameID| and |adapterDelegate|.
-- (void)requestInterstitialAdWithDelegate:
- (id<GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate>)adapterDelegate;
-
-/// Presents an interstitial ad for |viewController| with |adapterDelegate|.
-- (void)presentInterstitialAdForViewController:(UIViewController *)viewController
- delegate:(id<GADMAdapterUnityDataProvider,
- UnityAdsExtendedDelegate>)adapterDelegate;
-
-/// Tells the adapter to remove itself as a |adapterDelegate|.
-- (void)stopTrackingDelegate:
- (id<GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate>)adapterDelegate;
-
@end

MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m
#import "GADMAdapterUnityConstants.h"
#import "GADMAdapterUnityUtils.h"
-@interface GADMAdapterUnitySingleton () <UnityAdsExtendedDelegate> {
+@interface GADMAdapterUnitySingleton () {
/// Array to hold all adapter delegates.
NSMapTable *_adapterDelegates;
[mediationMetaData commit];
// Initializing Unity Ads with |gameID|.
- [UnityAds initialize:gameID delegate:self testMode:false enablePerPlacementLoad:true];
-}
-
-- (void)addAdapterDelegate:
- (id<GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate>)adapterDelegate {
- @synchronized(_adapterDelegates) {
- [_adapterDelegates setObject:adapterDelegate forKey:[adapterDelegate getPlacementID]];
- }
-}
-
-#pragma mark - Rewardbased video ad methods
-
-- (void)requestRewardedAdWithDelegate:
- (id<GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate>)adapterDelegate {
- NSString *gameID = [adapterDelegate getGameID];
- NSString *placementID = [adapterDelegate getPlacementID];
-
- @synchronized(_adapterDelegates) {
- if ([_adapterDelegates objectForKey:placementID]) {
- NSString *message = @"An ad is already loading for placement ID %@";
- [adapterDelegate unityAdsDidError:kUnityAdsErrorInternalError
- withMessage:[NSString stringWithFormat:message, placementID]];
- return;
- }
- }
-
- [self addAdapterDelegate:adapterDelegate];
-
- if (![UnityAds isInitialized]) {
- [self initializeWithGameID:gameID];
- }
-
- [UnityAds load:placementID];
- if ([UnityAds isReady:placementID]) {
- [adapterDelegate unityAdsReady:placementID];
- }
-}
-
-- (void)presentRewardedAdForViewController:(UIViewController *)viewController
- delegate:
- (id<GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate>)
- adapterDelegate {
- _currentShowingUnityDelegate = adapterDelegate;
-
- NSString *placementID = [adapterDelegate getPlacementID];
- if ([UnityAds isReady:placementID]) {
- UADSMediationMetaData *mediationMetaData = [[UADSMediationMetaData alloc] init];
- [mediationMetaData setOrdinal:impressionOrdinal++];
- [mediationMetaData commit];
- [UnityAds show:viewController placementId:placementID];
- } else {
- UADSMediationMetaData *mediationMetaData = [[UADSMediationMetaData alloc] init];
- [mediationMetaData setMissedImpressionOrdinal:missedImpressionOrdinal++];
- [mediationMetaData commit];
- }
-}
-
-#pragma mark - Interstitial ad methods
-
-- (void)requestInterstitialAdWithDelegate:
- (id<GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate>)adapterDelegate {
- NSString *gameID = [adapterDelegate getGameID];
- NSString *placementID = [adapterDelegate getPlacementID];
-
- @synchronized(_adapterDelegates) {
- if ([_adapterDelegates objectForKey:placementID]) {
- NSString *message = @"An ad is already loading for placement ID %@";
- [adapterDelegate unityAdsDidError:kUnityAdsErrorInternalError
- withMessage:[NSString stringWithFormat:message, placementID]];
- return;
- }
- }
-
- [self addAdapterDelegate:adapterDelegate];
-
- if (![UnityAds isInitialized]) {
- [self initializeWithGameID:gameID];
- }
-
- [UnityAds load:placementID];
- if ([UnityAds isReady:placementID]) {
- [adapterDelegate unityAdsReady:placementID];
- }
-}
-
-- (void)presentInterstitialAdForViewController:(UIViewController *)viewController
- delegate:(id<GADMAdapterUnityDataProvider,
- UnityAdsExtendedDelegate>)adapterDelegate {
- _currentShowingUnityDelegate = adapterDelegate;
-
- NSString *placementID = [adapterDelegate getPlacementID];
- if ([UnityAds isReady:placementID]) {
- UADSMediationMetaData *mediationMetaData = [[UADSMediationMetaData alloc] init];
- [mediationMetaData setOrdinal:impressionOrdinal++];
- [mediationMetaData commit];
- [UnityAds show:viewController placementId:placementID];
- } else {
- UADSMediationMetaData *mediationMetaData = [[UADSMediationMetaData alloc] init];
- [mediationMetaData setMissedImpressionOrdinal:missedImpressionOrdinal++];
- [mediationMetaData commit];
- }
-}
-
-#pragma mark - Unity Delegate Methods
-
-- (void)unityAdsPlacementStateChanged:(NSString *)placementId
- oldState:(UnityAdsPlacementState)oldState
- newState:(UnityAdsPlacementState)newState {
- // This callback is not forwarded to the adapter by the GADMAdapterUnitySingleton and the adapter
- // should use the unityAdsReady: and unityAdsDidError: callbacks to forward Unity Ads SDK state to
- // Google Mobile Ads SDK.
-}
-
-- (void)unityAdsDidFinish:(NSString *)placementID withFinishState:(UnityAdsFinishState)state {
- @synchronized(_adapterDelegates) {
- GADMAdapterUnityMapTableRemoveObjectForKey(_adapterDelegates, placementID);
- }
- [_currentShowingUnityDelegate unityAdsDidFinish:placementID withFinishState:state];
-}
-
-- (void)unityAdsDidStart:(NSString *)placementID {
- [_currentShowingUnityDelegate unityAdsDidStart:placementID];
-}
-
-- (void)unityAdsReady:(NSString *)placementID {
- id<GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate> adapterDelegate;
- @synchronized(_adapterDelegates) {
- adapterDelegate = [_adapterDelegates objectForKey:placementID];
- }
-
- if (adapterDelegate) {
- [adapterDelegate unityAdsReady:placementID];
- }
-}
-
-- (void)unityAdsDidClick:(NSString *)placementID {
- [_currentShowingUnityDelegate unityAdsDidClick:placementID];
-}
-
-- (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message {
- if (error == kUnityAdsErrorShowError) {
- [_currentShowingUnityDelegate unityAdsDidError:error withMessage:message];
- return;
- }
-
- NSArray *delegates;
- @synchronized(_adapterDelegates) {
- delegates = _adapterDelegates.objectEnumerator.allObjects;
- }
-
- for (id<UnityAdsExtendedDelegate, UnityAdsExtendedDelegate> delegate in delegates) {
- [delegate unityAdsDidError:error withMessage:message];
- }
-
- @synchronized(_adapterDelegates) {
- [_adapterDelegates removeAllObjects];
- }
-}
-
-- (void)stopTrackingDelegate:
- (id<GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate>)adapterDelegate {
- GADMAdapterUnityMapTableRemoveObjectForKey(_adapterDelegates, [adapterDelegate getPlacementID]);
+ [UnityAds initialize:gameID testMode:false enablePerPlacementLoad:true];
}
@end

Unity 3.4.2 adapter updates

Unity 3.4.2 adapter updates