1,在fb小游戏开发者平台创建应用 小游戏 - Facebook 游戏
2,点击fb应用->控制面板,添加产品:小游戏,Audience Network
3,在fb商务管理平台给小游戏添加广告点
4,给小游戏接入instant-games-sdk,小游戏 SDK - Facebook 游戏,以下为laya为例:
- 把instant-games-sdk的头文件加入libs目录
- 在index.html引入sdk,可以放在<head>标签内或<body>标签内,放在加载游戏代码之前:
<script src="https://connect.facebook.net/en_US/fbinstant.7.0.js"></script>
- 在main.ts中初始化fbsdk,fb要求在执行其它游戏逻辑之前初始化:
initFb(){
try{
console.log( ">> init fb 111" );
// 初始化FB小游戏
FBInstant.initializeAsync()
.then(function () {
// 告诉FB资源已经加载完毕
FBInstant.setLoadingProgress(100);
// 启动FB小游戏
FBInstant.startGameAsync()
.then(function () {
console.info("Success Load Scene");
Main._selfMain.fnMain();
})
.catch(function (e) {
console.error("Start Game Async failed: ", e);
});
})
.catch(function (e) {
console.error("Fail to start, Error: ", e);
});
}catch (e) {
console.error("Fail to init fb, Error: ", e);
}
}
- 调用banner,插屏或视频广告例:
import IPlatform from "./IPlatform"; import LayaGameConfig from "../../LayaGameConfig"; import { Notifications } from "src/core/common/Notifications"; export default class FbPlatform extends IPlatform { // private interstitialAd: FBInstant.AdInstance; private static _selfobj:FbPlatform; public init():void { super.init(); FbPlatform._selfobj = this; let version=Number(Laya.LocalStorage.getItem("version")); if(version<LayaGameConfig.version){ //Laya.Loader.clearResByGroup(); } this._bannerAdID.push( "bannerid" ); this._interAdID.push( "插屏id" ); this._videoAdID.push( "视屏id" ); this._musicManageState = 0; FBInstant.onPause( () => { // FbPlatform._selfobj.pasueResumeAudio("pause"); console.log( ">> FBInstant.onPause" ); }); } public initLaterLoad(){ super.initLaterLoad(); this.loadVideo(); } private pasueResumeAudio(type: string) { if (type == "pause") { if (FbPlatform._selfobj._musicManageState == 0) { FbPlatform._selfobj._musicManageState = 1; puremvc.Facade.getInstance("AppFacade").sendNotification(Notifications.MusicManage, "pause"); } } else if (type == "resume") { if (FbPlatform._selfobj._musicManageState == 1) { FbPlatform._selfobj._musicManageState = 0; puremvc.Facade.getInstance("AppFacade").sendNotification(Notifications.MusicManage, "resume"); } } } public addNavAppMapCallBack(thisObj: any,callback:Function):void { } public initlogin(thisObj: any,callback:Function,args?:any):void { super.initlogin( thisObj, callback, args ); this.loginCallback( {"state":1,"plartform":"Fb"} ); } public exitGame(){ FBInstant.quit(); } public vibrateShort(){ if ( navigator && navigator.vibrate) { // 支持 // console.log("支持设备震动!"); } else { // 不支持 // console.log("不支持设备震动!"); return; }//*/ // 震5秒 // navigator.vibrate(5000); // 震5秒,停0.3秒,在震4秒 // navigator.vibrate([5000, 300, 4000]); navigator.vibrate([20, 10, 10]); } public share( callObj:any,callback:Function ){ super.share( callObj, callback ); console.log(">> load share img 111" ); let url = "res/common/img/share/1.png"; Laya.loader.load( url, Laya.Handler.create(this, function(): void { var t: Laya.Texture = Laya.loader.getRes( url ); console.log(">> load share img suc 222" ); // var newHtmlCanvas:Laya.HTMLCanvas = new Laya.HTMLCanvas(false); // let img_base64 = newHtmlCanvas.toBase64( url, 0.8 ); // console.log( ">> img_base64:", img_base64 ); var sp = new Laya.Sprite(); sp.graphics.drawTexture( t ); var htmlCanvas = sp.drawToCanvas(750, 600, 0, 0); var img_base64 = htmlCanvas.source.toDataURL("image/png"); console.log( ">> img_base64:", img_base64 ); FBInstant.shareAsync({ intent: 'INVITE', image: img_base64, text: 'Safeguard justice and peace!', // data: { myReplayData: '...' }, }).then(function() { // continue with the game. console.log( ">> share suc" ); if( callObj && callback ){ callback.call( callObj, 1 ); } }).catch( err => { console.log( ">> share err:", JSON.stringify( err ) ); if( callObj && callback ){ callback.call( callObj, -1 ); } }); })) } private async loadInterstitialAd(){ try{ let adid = this._interAdID[0]; FbPlatform._selfobj._interstitialAd = null; FbPlatform._selfobj._interstitialAd = await FBInstant.getInterstitialAdAsync( adid ); FbPlatform._selfobj._interstitialAd.loadAsync().then(function () { console.log('loadInterAdAsync resolved.'); }).catch(function(err) { console.error('InterAd failed to load: ' + err.message ); }); } catch( err ){ console.log( ">> showInterstitialAd err:", JSON.stringify( err ) ); } } public async showInterstitialAd( thisObj: any, callback: Function ){ console.log( ">> showInterstitialAd 111" ); if(!this._haveAd)return; if( this._interAdID.length == 0 ) return; let adid = this._interAdID[0]; // FBInstant.getInterstitialAdAsync( // adid // ).then(function(interstitial) { // FbPlatform._selfobj._interstitialAd = interstitial; // //interstitial.getPlacementID(); // 'my_placement_id' // return interstitial.loadAsync(); // }).then( function(){ // // Ad loaded // return FbPlatform._selfobj._interstitialAd.showAsync(); // }).catch( err => { // console.log( ">> showInterstitialAd err:", JSON.stringify( err ) ); // }); try{ FbPlatform._selfobj._interstitialAd = null; FbPlatform._selfobj._interstitialAd = await FBInstant.getInterstitialAdAsync( adid ); if( FbPlatform._selfobj._interstitialAd == null ) return; console.log( ">> showInterstitialAd loading ..." ); await FbPlatform._selfobj._interstitialAd.loadAsync(); // Laya.SoundManager.stopAll(); await FbPlatform._selfobj._interstitialAd.showAsync(); if( thisObj && callback ){ callback.call( thisObj ); } console.log( ">> showInterstitialAd showing" ); /* FbPlatform._selfobj._interstitialAd.loadAsync().then(function () { console.log('loadInterAdAsync resolved.'); FbPlatform._selfobj._interstitialAd.showAsync() .then(function() { // Perform post-ad success operation console.log('Interstitial ad finished successfully'); if( thisObj && callback ){ callback.call( thisObj ); } }) .catch(function(e) { console.error(e.message); if( thisObj && callback ){ callback.call( thisObj ); } }); console.log( ">> showInterstitialAd showing" ); }).catch(function(err) { console.error('InterAd failed to load: ' + err.message ); }); console.log( ">> showInterstitialAd load end" ); //*/ } catch( err ){ console.log( ">> showInterstitialAd err:", JSON.stringify( err ) ); } } public async showAdBanner(left:number,top:number,width:number,height:number,islaya:number,pos:number,thisobj:any,thisfun:Function){ // console.log( ">> fb no banner ad" ); // return; let adid = this._bannerAdID[0]; FBInstant.loadBannerAdAsync( adid // Replace with your Ad Placement ID. ).then(function () { console.log('loadBannerAdAsync resolved.'); }).catch(function(err) { console.error('Banner failed to load: ' + err.message); } ); } public closeAdBanner(){ FBInstant.hideBannerAdAsync().then(function () { console.log('hideBannerAdAsync resolved.'); }).catch(function (err) { console.error('hideBannerAdAsync rejected: ' + err.message); } ); } private async loadVideo(){ if( !FbPlatform._selfobj._videoRunLoad ){ try{ FbPlatform._selfobj._videoRunLoad = true; let adid = this._videoAdID[0]; FbPlatform._selfobj._videoAd = null; FbPlatform._selfobj._videoAd = await FBInstant.getRewardedVideoAsync( adid ); console.log( ">> loadVideo loading ..." ); await FbPlatform._selfobj._videoAd.loadAsync(); console.log( ">> loadVideo load end" ); FbPlatform._selfobj._videoRunLoad = false; FbPlatform._selfobj._isReadyVideo = true; } catch( e ){ FbPlatform._selfobj._videoRunLoad = false; // Throws ADS_TOO_MANY_INSTANCES // Throws CLIENT_UNSUPPORTED_OPERATION // Throws ADS_FREQUENT_LOAD // Throws ADS_NO_FILL // Throws INVALID_PARAM // Throws NETWORK_FAILURE console.log( ">> loadVideo err:", JSON.stringify( e ) ); } } } public checkReadyVideo( nochange?: boolean ):number{ if(!this._haveAd)return 0; if (nochange) {//同一页面不改变广告分享方式 if (this.shareCurrentType == 2) { return 2; } } this._videoRunShow=false; if(!this._isReadyVideo){ FbPlatform._selfobj.loadVideo(); return 0; } else{ return 1; } } public showAdVideo(thisObj:any,callback:Function,args:any):boolean{ if(!this._haveAd)return false; if(!this._isReadyVideo)return false; if(this._videoRunShow)return false; super.showAdVideo( thisObj ,callback, args ); this._videoRunShow=true; this._videoAd.showAsync() .then( function(){ // Ad watched console.log('Rewarded video watched successfully, to preload'); FbPlatform._selfobj._videoRunShow=false; FbPlatform._selfobj._isReadyVideo=false; FbPlatform._selfobj.videoAdCallback( 1 ); Laya.timer.once( 1000, FbPlatform._selfobj, FbPlatform._selfobj.loadVideo ); }) .catch(err => { // Throws ADS_NOT_LOADED // Throws INVALID_PARAM // Throws NETWORK_FAILURE // Throws INVALID_OPERATION // Throws RATE_LIMITED console.log( JSON.stringify( err ) ); FbPlatform._selfobj._videoRunShow=false; FbPlatform._selfobj._isReadyVideo=false; FbPlatform._selfobj.videoAdCallback( -1 ); }); return true; } public checkCanVideoRecord():boolean{ return false; } public startVideoRecord(duration:number,thisObj:any,callback:Function,args?:any):boolean{ return false; } public stopVideoRecord(){ } /**分享视频 */ public shareRecord( callObj:any,callback:Function ){ } public checkShortcut(thisObj: any, callback: Function) { FBInstant.canCreateShortcutAsync().then(function(canCreateShortcut) { if (canCreateShortcut) { callback.call(thisObj, 1 ); } else{ callback.call(thisObj, 0 ); } }); } public installShortcut(thisObj: any, callback: Function) { FBInstant.createShortcutAsync().then(function() { // Shortcut created callback.call(thisObj, 1 ); }) .catch(function() { // Shortcut not created callback.call(thisObj, 0 ); } ); } }
5,导出成web h5,直接压缩web目录为zip,进入fb开发者后台->小游戏->网页托管,点击上传版本上传zip包,上传成功后点击界面的推送到生产阶段或测试阶段,点击play可直接测试
小游戏->详情,内有游戏链接
6,一般要设置应用隐私权政策网址与服务条款网址:应用设置->基本