-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.dart
169 lines (154 loc) · 4.81 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:unity_ads_plugin/unity_ads_plugin.dart';
void main() {
runApp(const UnityAdsExampleApp());
}
class UnityAdsExampleApp extends StatelessWidget {
const UnityAdsExampleApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Unity Ads Example',
home: Scaffold(
appBar: AppBar(
title: const Text('Unity Ads Example'),
),
body: const SafeArea(
child: _UnityAdsExample(),
),
),
);
}
}
class _UnityAdsExample extends StatefulWidget {
const _UnityAdsExample({Key? key}) : super(key: key);
@override
_UnityAdsExampleState createState() => _UnityAdsExampleState();
}
class _UnityAdsExampleState extends State<_UnityAdsExample> {
bool _showBanner = false;
Map<String, bool> placements = {
AdManager.interstitialVideoAdPlacementId: false,
AdManager.rewardedVideoAdPlacementId: false,
};
@override
void initState() {
super.initState();
UnityAds.init(
gameId: AdManager.gameId,
testMode: true,
onComplete: () {
print('Initialization Complete');
_loadAds();
},
onFailed: (error, message) => print('Initialization Failed: $error $message'),
);
}
@override
Widget build(BuildContext context) {
return SizedBox(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
setState(() {
_showBanner = !_showBanner;
});
},
child: Text(_showBanner ? 'Hide Banner' : 'Show Banner'),
),
ElevatedButton(
onPressed: placements[AdManager.rewardedVideoAdPlacementId] == true
? () => _showAd(AdManager.rewardedVideoAdPlacementId)
: null,
child: const Text('Show Rewarded Video'),
),
ElevatedButton(
onPressed: placements[AdManager.interstitialVideoAdPlacementId] == true
? () => _showAd(AdManager.interstitialVideoAdPlacementId)
: null,
child: const Text('Show Interstitial Video'),
),
],
),
if (_showBanner)
UnityBannerAd(
placementId: AdManager.bannerAdPlacementId,
onLoad: (placementId) => print('Banner loaded: $placementId'),
onClick: (placementId) => print('Banner clicked: $placementId'),
onShown: (placementId) => print('Banner shown: $placementId'),
onFailed: (placementId, error, message) =>
print('Banner Ad $placementId failed: $error $message'),
),
],
),
);
}
void _loadAds() {
for (var placementId in placements.keys) {
_loadAd(placementId);
}
}
void _loadAd(String placementId) {
UnityAds.load(
placementId: placementId,
onComplete: (placementId) {
print('Load Complete $placementId');
setState(() {
placements[placementId] = true;
});
},
onFailed: (placementId, error, message) => print('Load Failed $placementId: $error $message'),
);
}
void _showAd(String placementId) {
setState(() {
placements[placementId] = false;
});
UnityAds.showVideoAd(
placementId: placementId,
onComplete: (placementId) {
print('Video Ad $placementId completed');
_loadAd(placementId);
},
onFailed: (placementId, error, message) {
print('Video Ad $placementId failed: $error $message');
_loadAd(placementId);
},
onStart: (placementId) => print('Video Ad $placementId started'),
onClick: (placementId) => print('Video Ad $placementId click'),
onSkipped: (placementId) {
print('Video Ad $placementId skipped');
_loadAd(placementId);
},
);
}
}
class AdManager {
static String get gameId {
if (defaultTargetPlatform == TargetPlatform.android) {
return 'your_android_game_id';
}
if (defaultTargetPlatform == TargetPlatform.iOS) {
return 'your_ios_game_id';
}
return '';
}
static String get bannerAdPlacementId {
return 'your_banner_ad_placement_id';
}
static String get interstitialVideoAdPlacementId {
return 'your_interstitial_video_ad_placement_id';
}
static String get rewardedVideoAdPlacementId {
return 'your_rewarded_video_ad_placement_id';
}
}