sydi_unity_ads 1.0.1 sydi_unity_ads: ^1.0.1 copied to clipboard
SYDI Unity Ads plugin.
example/lib/main.dart
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:sydi_unity_ads/sydi_unity_ads.dart';
void main() {
runApp(const UnityMediationAdsApp());
}
class UnityMediationAdsApp extends StatelessWidget {
const UnityMediationAdsApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Unity Mediation Ads Example',
home: Scaffold(
appBar: AppBar(
title: const Text('Unity Mediation 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> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
UnityAdsBase.initialize(
gameId: AdManager.gameId,
onComplete: () => print('Initialization Complete ...'),
onFailed: (error, message) =>
print('Initialization Failed: $error $message'),
);
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await SydiUnityAds.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return SizedBox(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
RewardedAdButton(
placementId: AdManager.rewardedVideoAdPlacementId,
title: 'Show Rewarded Video',
),
InterstitialAdButton(
placementId: AdManager.interstitialVideoAdPlacementId,
title: 'Show Interstitial Video',
),
],
),
],
),
);
}
}
class RewardedAdButton extends StatefulWidget {
const RewardedAdButton(
{Key? key, required this.placementId, required this.title})
: super(key: key);
final String placementId;
final String title;
@override
_RewardedAdButtonState createState() => _RewardedAdButtonState();
}
class _RewardedAdButtonState extends State<RewardedAdButton> {
bool _loaded = false;
@override
void initState() {
super.initState();
UnityRewardedAds.load(
placementId: widget.placementId,
onComplete: (placementId) {
print('RewardedAd Load Complete $placementId');
setState(() {
_loaded = true;
});
},
onFailed: (placementId, error, message) =>
print('RewardedAd Load Failed $placementId: $error $message'),
);
}
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: _loaded
? () {
UnityRewardedAds.show(
placementId: widget.placementId,
onRewardedShowed: (placementId) =>
print('Rewarded Ad $placementId was shown'),
onRewardedFailedShow: (placementId, error, message) =>
print('Rewarded Ad $placementId failed: $error $message'),
onRewardedClosed: (placementId) =>
print('Rewarded Ad $placementId closed'),
onRewardedClicked: (placementId) => print('Rewarded Ad $placementId clicked'),
onUserRewarded: (placementId) =>
print('Rewarded Ad $placementId user Rewarded'),
);
}
: null,
child: Text(widget.title),
);
}
}
class InterstitialAdButton extends StatefulWidget {
const InterstitialAdButton(
{Key? key, required this.placementId, required this.title})
: super(key: key);
final String placementId;
final String title;
@override
_InterstitialAdButtonState createState() => _InterstitialAdButtonState();
}
class _InterstitialAdButtonState extends State<InterstitialAdButton> {
bool _loaded = false;
@override
void initState() {
super.initState();
UnityInterstitialAds.load(
placementId: widget.placementId,
onComplete: (placementId) {
print('InterstitialAd Load Complete $placementId');
setState(() {
_loaded = true;
});
},
onFailed: (placementId, error, message) =>
print('InterstitialAd Load Failed $placementId: $error $message'),
);
}
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: _loaded
? () {
UnityInterstitialAds.show(
placementId: widget.placementId,
onInterstitialShowed: (placementId) =>
print('Interstitial Ad $placementId was shown'),
onInterstitialFailedShow: (placementId, error, message) =>
print('Interstitial Ad $placementId failed: $error $message'),
onInterstitialClosed: (placementId) =>
print('Interstitial Ad $placementId closed'),
onInterstitialClicked: (placementId) =>
print('Interstitial Ad $placementId clicked'),
);
}
: null,
child: Text(widget.title),
);
}
}
class AdManager {
static String get gameId {
if (defaultTargetPlatform == TargetPlatform.android) {
return '4562192';
}
if (defaultTargetPlatform == TargetPlatform.iOS) {
return 'your_ios_game_id';
}
return '';
}
static String get interstitialVideoAdPlacementId {
return 'Interstitial_Android';
}
static String get rewardedVideoAdPlacementId {
return 'Rewarded_Android';
}
}