Skip to content

Commit

Permalink
Added Unity Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
LTPhantom committed Dec 11, 2023
1 parent 2e9234a commit e29f0b4
Show file tree
Hide file tree
Showing 32 changed files with 547 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<application android:name="${applicationName}" android:label="google_mobile_ads_example" android:icon="@mipmap/ic_launcher">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-7319269804560504~8998416027"/>
android:value="ca-app-pub-3212738706492790~5949485661"/>
<activity android:name="io.flutter.plugins.googlemobileadsexample.MainActivity"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class _AnchoredAdaptiveExampleState extends State<AnchoredAdaptiveExample> {

_anchoredAdaptiveAd = BannerAd(
adUnitId: Platform.isAndroid
? 'ca-app-pub-3940256099942544/6300978111'
? 'ca-app-pub-7319269804560504/3526292527'
: 'ca-app-pub-3940256099942544/2934735716',
size: size,
request: AdRequest(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class _InlineAdaptiveExampleState extends State<InlineAdaptiveExample> {
_adWidth.truncate());

_inlineAdaptiveAd = AdManagerBannerAd(
adUnitId: 'ca-app-pub-3940256099942544/9214589741',
adUnitId: 'ca-app-pub-7319269804560504/2213210851',
sizes: [size],
request: AdManagerAdRequest(),
listener: AdManagerBannerAdListener(
Expand Down
9 changes: 6 additions & 3 deletions packages/google_mobile_ads/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'dart:io' show Platform;

import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:unity/unity_adapter.dart';
import 'dart:developer';

import 'anchored_adaptive_example.dart';
Expand Down Expand Up @@ -74,6 +75,8 @@ class _MyAppState extends State<MyApp> {
super.initState();
MobileAds.instance.updateRequestConfiguration(
RequestConfiguration(testDeviceIds: [testDevice]));
UnityAdapter.setGDPRConsent(false);
UnityAdapter.setCCPAConsent(false);
_createInterstitialAd();
_createRewardedAd();
_createRewardedInterstitialAd();
Expand All @@ -82,7 +85,7 @@ class _MyAppState extends State<MyApp> {
void _createInterstitialAd() {
InterstitialAd.load(
adUnitId: Platform.isAndroid
? 'ca-app-pub-7319269804560504/4137476472'
? 'ca-app-pub-3212738706492790/2245868040'
: 'ca-app-pub-3940256099942544/4411468910',
request: request,
adLoadCallback: InterstitialAdLoadCallback(
Expand Down Expand Up @@ -129,7 +132,7 @@ class _MyAppState extends State<MyApp> {
void _createRewardedAd() {
RewardedAd.load(
adUnitId: Platform.isAndroid
? 'ca-app-pub-3940256099942544/5224354917'
? 'ca-app-pub-7319269804560504/9900129189'
: 'ca-app-pub-3940256099942544/1712485313',
request: request,
rewardedAdLoadCallback: RewardedAdLoadCallback(
Expand Down Expand Up @@ -180,7 +183,7 @@ class _MyAppState extends State<MyApp> {
void _createRewardedInterstitialAd() {
RewardedInterstitialAd.load(
adUnitId: Platform.isAndroid
? 'ca-app-pub-3940256099942544/5354046379'
? 'ca-app-pub-7319269804560504/4158649975'
: 'ca-app-pub-3940256099942544/6978759866',
request: request,
rewardedInterstitialAdLoadCallback: RewardedInterstitialAdLoadCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class _NativeTemplateExampleExampleState extends State<NativeTemplateExample> {
super.didChangeDependencies();
// Create the ad objects and load ads.
_nativeAd = NativeAd(
adUnitId: '/6499/example/native',
adUnitId: 'ca-app-pub-7319269804560504/8587047512',
request: AdRequest(),
listener: NativeAdListener(
onAdLoaded: (Ad ad) {
Expand Down
6 changes: 4 additions & 2 deletions packages/google_mobile_ads/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ dependencies:
sdk: flutter
google_mobile_ads:
path: ../
meta_audience_network:
path: ../mediation/meta_audience_network
# meta_audience_network:
# path: ../mediation/meta_audience_network
unity:
path: ../mediation/unity
webview_flutter_android: ^3.7.0
webview_flutter: ^4.0.5

Expand Down
108 changes: 1 addition & 107 deletions packages/google_mobile_ads/lib/src/ad_containers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -825,111 +825,6 @@ class _FluidAdWidgetState extends State<FluidAdWidget> {
}
}

class AutoSizingAdWidget extends StatefulWidget {
const AutoSizingAdWidget({Key? key, required this.ad}) : super(key: key);

final AdWithView ad;

@override
_AutoSizingAdWidgetState createState() => _AutoSizingAdWidgetState();
}

class _AutoSizingAdWidgetState extends State<AutoSizingAdWidget> {
bool _adIdAlreadyMounted = false;
bool _adLoadNotCalled = false;
double _width = 0;
double _height = 0;

@override
void initState() {
super.initState();
final int? adId = instanceManager.adIdFor(widget.ad);
if (adId != null) {
if (instanceManager.isWidgetAdIdMounted(adId)) {
_adIdAlreadyMounted = true;
}
instanceManager.mountWidgetAdId(adId);
} else {
_adLoadNotCalled = true;
}
}

@override
void dispose() {
super.dispose();
final int? adId = instanceManager.adIdFor(widget.ad);
if (adId != null) {
instanceManager.unmountWidgetAdId(adId);
}
}

@override
Widget build(BuildContext context) {
if (_adIdAlreadyMounted) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('This AdWidget is already in the Widget tree'),
ErrorHint(
'If you placed this AdWidget in a list, make sure you create a new instance '
'in the builder function with a unique ad object.'),
ErrorHint(
'Make sure you are not using the same ad object in more than one AdWidget.'),
]);
}
if (_adLoadNotCalled) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
'AdWidget requires Ad.load to be called before AdWidget is inserted into the tree'),
ErrorHint(
'Parameter ad is not loaded. Call Ad.load before AdWidget is inserted into the tree.'),
]);
}

Widget platformView;
double height;
double width;
Map<String, Object?> creationParams = {
'adId': instanceManager.adIdFor(widget.ad),
'type': 'AutoSizingAdWidget',
};
if (defaultTargetPlatform == TargetPlatform.android) {
platformView = PlatformViewLink(
viewType: '${instanceManager.channel.name}/ad_widget',
surfaceFactory:
(BuildContext context, PlatformViewController controller) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
},
onCreatePlatformView: (PlatformViewCreationParams params) {
return PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: '${instanceManager.channel.name}/ad_widget',
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: StandardMessageCodec(),
)
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
..create();
},
);
width = _width / MediaQuery.of(context).devicePixelRatio;
height = _height / MediaQuery.of(context).devicePixelRatio;
} else {
platformView = UiKitView(
viewType: '${instanceManager.channel.name}/ad_widget',
creationParams: creationParams,
creationParamsCodec: StandardMessageCodec(),
);
width = _width;
height = _height;
}

return Container(height: max(1, height), width: max(1, width), child: platformView);
}
}

/// A banner ad.
///
/// This ad can either be overlaid on top of all flutter widgets as a static
Expand Down Expand Up @@ -1660,5 +1555,4 @@ class VideoOptions {
customControlsRequested == other.customControlsRequested &&
startMuted == other.startMuted;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1386,4 +1386,4 @@ class _BiMap<K extends Object, V extends Object> extends MapBase<K, V> {
inverse._map.remove(value);
return _map.remove(key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ dependencies:
flutter:
sdk: flutter
plugin_platform_interface: ^2.0.2
google_mobile_ads:
path: ../../

dev_dependencies:
flutter_test:
Expand Down
29 changes: 29 additions & 0 deletions packages/google_mobile_ads/mediation/unity/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
build/
33 changes: 33 additions & 0 deletions packages/google_mobile_ads/mediation/unity/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "c8101b56a9fa5500247d1de7deb0434a97a78c93"
channel: "master"

project_type: plugin

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: c8101b56a9fa5500247d1de7deb0434a97a78c93
base_revision: c8101b56a9fa5500247d1de7deb0434a97a78c93
- platform: android
create_revision: c8101b56a9fa5500247d1de7deb0434a97a78c93
base_revision: c8101b56a9fa5500247d1de7deb0434a97a78c93
- platform: ios
create_revision: c8101b56a9fa5500247d1de7deb0434a97a78c93
base_revision: c8101b56a9fa5500247d1de7deb0434a97a78c93

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
3 changes: 3 additions & 0 deletions packages/google_mobile_ads/mediation/unity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions packages/google_mobile_ads/mediation/unity/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
15 changes: 15 additions & 0 deletions packages/google_mobile_ads/mediation/unity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# unity

A new Flutter plugin project.

## Getting Started

This project is a starting point for a Flutter
[plug-in package](https://flutter.dev/developing-packages/),
a specialized package that includes platform-specific implementation code for
Android and/or iOS.

For help getting started with Flutter development, view the
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
9 changes: 9 additions & 0 deletions packages/google_mobile_ads/mediation/unity/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.cxx
Loading

0 comments on commit e29f0b4

Please sign in to comment.