@[TOC]facebook小遊戲SDK接入介紹
備註
- 目前小遊戲只支持公司
- 需要驗證商務平臺
- 有內購的需要提交內購英文版流程
- 廣告支持IOS和安卓 不支持web
- 支付不支持IOS
SDK初始化
FBInstant.initializeAsync().then(function() {
// 初始化
});
設置加載進度
FBInstant.setLoadingProgress(progress);
//progress進度值
開始遊戲
FBInstant.startGameAsync().then(function() {
//只能在初始化後調用
});
獲取登錄簽名
FBInstant.player.getSignedPlayerInfoAsync().then(function (result) {
// 這裏result能獲取到包含facebook_id的簽名
});
獲取用戶運行的平臺
FBInstant.getPlatform() //這個能得到IOS...三個平臺
設置排行榜
FBInstant.getLeaderboardAsync('allscore').then(function(leaderboard){
return leaderboard.setScoreAsync(1000);
}).then(function(){
console.log('Score saved')
}).catch(function(err){
console.log(err);
});
allscore //後臺設置的排行榜名稱
獲取排行榜
FBInstant.getLeaderboardAsync('allscore').then(function(leaderboard){
return leaderboard.getEntriesAsync(10, 0);
}).then(function(entries){
for (var i = 0; i < entries.length; i++) {
console.log(entries[i].getRank() + '. ' + entries[i].getPlayer().getName() + ': ' +entries[i].getScore());
}
}).catch(function(err){
console.log(err);
});
allscore //後臺設置的排行榜名稱
內購–判斷環境
FBInstant.payments.onReady(function () {
// 進入這裏證明當前環境支持支付
});
內購–獲取後臺配置的商品
FBInstant.payments.getCatalogAsync().then(function (menu) {
//menu 商品列表
});
內購–獲取沒有發放的清單
FBInstant.payments.getPurchasesAsync().then(function (purchases) {
console.log(purchases);
//處理未發放的訂單
}).catch(function(error){
console.log(error);
});
內購–購買併發放物品
FBInstant.payments.purchaseAsync({
productID: gid, //商品ID 後臺設置
developerPayload: null,
}).then(function (purchase) {
//發放物品
FBInstant.payments.consumePurchaseAsync(purchase.purchaseToken).then(function () {
//成功請求後臺發放物品
}).catch(function(error) {
console.log(error);
});
}).catch(function(error) {
console.log(error);
});
分享
FBInstant.context.chooseAsync().then(function() {
FBInstant.updateAsync({
action: "CUSTOM",
template: "top_score",
cta: "Race " + FBInstant.player.getName(),
image: 'BASE64',
text: {
default: "I Challenge You To A Race!"
},
data: {
myReplayData: "..."
},
strategy: "LAST",
notification: "NO_PUSH"
}).then(function() {});
});
激勵廣告審覈
激勵廣告–預加載
var videos=[];
var videoload=function ()
{
// 預加載3個
var preloadedRewardedVideo=null;
FBInstant.getRewardedVideoAsync(
'xxxxxxxxxx', // Your Ad Placement Id
).then(function(rewarded) {
preloadedRewardedVideo = rewarded;
return preloadedRewardedVideo.loadAsync();
}).then(function(){
videos.push(preloadedRewardedVideo);
}).catch(function(err){
console.log(err);
});
}
for(var i=1;i<=3;i++)
{
videoload();
}
激勵廣告–顯示
var isShow=false;
function show()
{
videoload();
if(isShow){
return ;
}
var preloadedRewardedVideo=videos.pop();
if(!preloadedRewardedVideo){
return ;
}
isShow=true;
preloadedRewardedVideo.showAsync().then(function() {
// 播放成功
isShow=false;
}).catch(function(e) {
isShow=false;
console.log(e);
});
}