Flutter广告集成插件sydi_unity_ads的使用
Flutter广告集成插件sydi_unity_ads的使用
SYDI Unity Ads 插件
SYDI Unity Ads 插件是一个用于在 Flutter 应用中集成广告功能的工具。它支持 Rewarded Ads(激励视频广告)和 Interstitial Ads(插屏广告)。本文将详细介绍如何使用该插件。
开始使用
项目初始化
本项目是一个 Flutter 插件包,包含 Android 和 iOS 平台的具体实现代码。要开始使用该插件,请确保你已经配置好 Flutter 环境,并且熟悉 Flutter 的基本开发流程。
如果你是第一次使用 Flutter,可以参考官方文档:
示例代码详解
以下是一个完整的示例代码,展示了如何在 Flutter 中使用 sydi_unity_ads 插件来集成广告功能。
示例代码
// 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](/user/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](/user/override)
_UnityAdsExampleState createState() => _UnityAdsExampleState();
}
class _UnityAdsExampleState extends State<UnityAdsExample> {
String _platformVersion = 'Unknown';
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
UnityAdsBase.initialize(
gameId: AdManager.gameId,
onComplete: () => print('Initialization Complete ...'),
onFailed: (error, message) =>
print('Initialization Failed: $error $message'),
);
}
// 初始化平台状态
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion =
await SydiUnityAds.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
[@override](/user/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](/user/override)
_RewardedAdButtonState createState() => _RewardedAdButtonState();
}
class _RewardedAdButtonState extends State<RewardedAdButton> {
bool _loaded = false;
[@override](/user/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](/user/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](/user/override)
_InterstitialAdButtonState createState() => _InterstitialAdButtonState();
}
class _InterstitialAdButtonState extends State<InterstitialAdButton> {
bool _loaded = false;
[@override](/user/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](/user/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'; // 替换为你的 Android 游戏 ID
}
if (defaultTargetPlatform == TargetPlatform.iOS) {
return 'your_ios_game_id'; // 替换为你的 iOS 游戏 ID
}
return '';
}
static String get interstitialVideoAdPlacementId {
return 'Interstitial_Android'; // 替换为你的插屏广告放置 ID
}
static String get rewardedVideoAdPlacementId {
return 'Rewarded_Android'; // 替换为你的激励视频广告放置 ID
}
}
更多关于Flutter广告集成插件sydi_unity_ads的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter广告集成插件sydi_unity_ads的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
SydiUnityAds 是一个用于在 Flutter 应用中集成 Unity Ads 广告的插件。Unity Ads 是一个流行的广告平台,特别适用于游戏应用。通过 SydiUnityAds 插件,开发者可以轻松地在 Flutter 应用中展示 Unity Ads 广告。
以下是使用 SydiUnityAds 插件的基本步骤:
1. 添加依赖
首先,在 pubspec.yaml 文件中添加 sydi_unity_ads 插件的依赖:
dependencies:
flutter:
sdk: flutter
sydi_unity_ads: ^版本号
请将 ^版本号 替换为最新的插件版本号。
2. 初始化 Unity Ads
在应用启动时,需要初始化 Unity Ads。通常可以在 main.dart 文件中的 main 函数中进行初始化。
import 'package:flutter/material.dart';
import 'package:sydi_unity_ads/sydi_unity_ads.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 Unity Ads
await SydiUnityAds.initialize(
gameId: 'YOUR_GAME_ID', // 替换为你的 Unity Ads Game ID
testMode: true, // 测试模式
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Unity Ads Example',
home: MyHomePage(),
);
}
}
3. 展示广告
在需要展示广告的地方调用相应的方法。SydiUnityAds 支持展示奖励视频广告和插页式广告。
展示奖励视频广告
import 'package:flutter/material.dart';
import 'package:sydi_unity_ads/sydi_unity_ads.dart';
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Unity Ads Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
// 展示奖励视频广告
bool isReady = await SydiUnityAds.isReady(placementId: 'rewardedVideo');
if (isReady) {
await SydiUnityAds.showRewardedAd(
placementId: 'rewardedVideo',
onRewarded: () {
// 用户完成观看广告并应获得奖励
print('User rewarded!');
},
);
} else {
print('Ad not ready');
}
},
child: Text('Show Rewarded Ad'),
),
),
);
}
}
展示插页式广告
import 'package:flutter/material.dart';
import 'package:sydi_unity_ads/sydi_unity_ads.dart';
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Unity Ads Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
// 展示插页式广告
bool isReady = await SydiUnityAds.isReady(placementId: 'interstitial');
if (isReady) {
await SydiUnityAds.showInterstitialAd(placementId: 'interstitial');
} else {
print('Ad not ready');
}
},
child: Text('Show Interstitial Ad'),
),
),
);
}
}
4. 处理广告事件
你可以通过回调函数来处理广告的各个事件,例如广告加载成功、广告展示完成、广告关闭等。
SydiUnityAds.setAdListener(
onAdLoaded: (placementId) {
print('Ad loaded: $placementId');
},
onAdFailedToLoad: (placementId, error) {
print('Ad failed to load: $placementId, error: $error');
},
onAdClosed: (placementId) {
print('Ad closed: $placementId');
},
onAdOpened: (placementId) {
print('Ad opened: $placementId');
},
);
5. 测试模式
在开发阶段,建议启用测试模式。测试模式下,Unity Ads 会展示测试广告,而不是真实的广告。
await SydiUnityAds.initialize(
gameId: 'YOUR_GAME_ID',
testMode: true, // 启用测试模式
);
6. 发布应用
在发布应用之前,请确保将 testMode 设置为 false,以便展示真实的广告。
await SydiUnityAds.initialize(
gameId: 'YOUR_GAME_ID',
testMode: false, // 禁用测试模式
);

