Lớp công cụ
AdService
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Tóm Tắt
Phương Pháp
CreateAdRewardFromDevProductId(devProductId: number):AdReward |
GetAdAvailabilityNowAsync(adFormat: Enum.AdFormat):GetAdAvailabilityNowResult |
GetCampaignEligibilityAsync(campaignId: string,player: Player?):GetCampaignEligibilityResult |
RegisterAdOpportunityAsync(instance: Instance,placementId: number?):() |
RegisterDisclosureButton(disclosureButton: GuiButton,adIntegrationPlacementId: string):() |
ShowRewardedVideoAdAsync(player: Player,reward: AdReward,placementId: number?):Enum.ShowAdResult |
ShowVideoAd():() |
UnregisterAdOpportunity(instance: Instance):() |
Sự Kiện
VideoAdClosed(adShown: boolean):RBXScriptSignal |
Tài Liệu Tham Khảo API
Phương Pháp
CreateAdRewardFromDevProductId
GetAdAvailabilityNowAsync
Tham Số
Lợi Nhuận
GetAdAvailabilityNowResult
GetCampaignEligibilityAsync
Lợi Nhuận
GetCampaignEligibilityResult
Mẫu mã
Ví dụ Tích hợp Quảng cáo
local AdService = game:GetService("AdService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local YOUR_CAMPAIGN_ID = "123" -- Nhập ID chiến dịch tích hợp quảng cáo của bạn từ trình quản lý quảng cáo ở đây.
local AD_INTEGRATION_IDS = {
-- Nhập ID vị trí tích hợp quảng cáo của bạn cho mỗi tài sản bạn đã đăng ký là một phần của
-- chiến dịch tích hợp quảng cáo của bạn.
BrandedBuilding = "abc-123",
BrandedScreenGui = "xyz-789",
}
-- Xác minh rằng LocalPlayer đủ điều kiện để xem chiến dịch của bạn
local success, result = pcall(function()
return AdService:GetCampaignEligibilityAsync(YOUR_CAMPAIGN_ID)
end)
if success and result.IsEligible then
-- Tất cả tài sản tích hợp quảng cáo nên được ẩn theo mặc định. Khi bạn biết rằng
-- người chơi đủ điều kiện, thì bạn có thể bắt đầu hiển thị các tài sản tích hợp quảng cáo.
local brandedBuildingModel = ReplicatedStorage.adIntegration.building
local brandedBuildingModelDisclosureBillboardGui = brandedBuildingModel.disclosureBillboardGui
-- Đừng quên đăng ký các nút thông báo quảng cáo của bạn để người chơi biết ai đang trả tiền cho quảng cáo!
AdService:RegisterDisclosureButton(
brandedBuildingModelDisclosureBillboardGui.adDisclosureButton,
AD_INTEGRATION_IDS.BrandedBuilding
)
brandedBuildingModelDisclosureBillboardGui.Parent = Players.LocalPlayer.PlayerGui
brandedBuildingModel.Parent = game.Workspace
local brandedScreenGui = ReplicatedStorage.adIntegration.screenGui
AdService:RegisterDisclosureButton(brandedScreenGui.adDisclosureButton, AD_INTEGRATION_IDS.BrandedScreenGui)
brandedScreenGui.Parent = Players.LocalPlayer.PlayerGui
endRegisterAdOpportunityAsync
Lợi Nhuận
()
Mẫu mã
Theo dõi cơ hội quảng cáo
local AdService = game:GetService("AdService")
local playerGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
-- Tạo ScreenGui và nút bấm
local screenGui = Instance.new("ScreenGui", playerGui)
local adButton = Instance.new("TextButton", screenGui)
-- Đăng ký nút bấm với AdService
local success, err = pcall(function()
-- Tham số thứ hai là một tham số ID Đặt chỗ (Số) tùy chọn được tạo trong Creator Hub.
-- Nếu bạn chọn không cung cấp một ID Đặt chỗ, cơ hội quảng cáo sẽ được theo dõi dưới vị trí mặc định.
AdService:RegisterAdOpportunityAsync(adButton, 1234567891234567)
end)
if not success then
warn("Đăng ký cơ hội quảng cáo thất bại:", err)
endRegisterDisclosureButton
ShowRewardedVideoAdAsync
AdService:ShowRewardedVideoAdAsync(
Lợi Nhuận
Mẫu mã
Hiển thị quảng cáo video có thưởng
-- Trong một script trên client:
local AdService = game:GetService("AdService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local rewardedVideoButton: GuiButton =
Players.LocalPlayer.PlayerGui:WaitForChild("myAdGui"):WaitForChild("watchRewardedVideoButton")
local watchRewardedVideoAdRemoteEvent: RemoteEvent = ReplicatedStorage:WaitForChild("watchRewardedVideoAd")
-- Client chưa có quảng cáo sẵn; ẩn nút
rewardedVideoButton.Visible = false
local success, result = pcall(function()
return AdService:GetAdAvailabilityNowAsync(Enum.AdFormat.RewardedVideo)
end)
if success and result.AdAvailabilityResult == Enum.AdAvailabilityResult.IsAvailable then
-- Hiển thị nút nếu có quảng cáo sẵn
rewardedVideoButton.Visible = true
end
rewardedVideoButton.Activated:Connect(function()
watchRewardedVideoAdRemoteEvent:FireServer()
-- Kết quả IsAvailable chỉ tốt cho một lần phát quảng cáo. Để phát quảng cáo khác, bạn cần gọi
-- GetAdAvailabilityNowAsync và kiểm tra lại tính khả dụng.
rewardedVideoButton.Visible = false
end)
-- Trong một script trên server:
local AdService = game:GetService("AdService")
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local watchRewardedVideoAdRemoteEvent: RemoteEvent = ReplicatedStorage:WaitForChild("watchRewardedVideoAd")
local REWARDED_VIDEO_PRODUCT_ID = 123 -- Nhập ID sản phẩm phát triển của bạn tại đây
local function grantReward(player: Player)
print(`tặng quà cho {player.Name}!`)
end
MarketplaceService.ProcessReceipt = function(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
if receiptInfo.ProductId == REWARDED_VIDEO_PRODUCT_ID then
if receiptInfo.ProductPurchaseChannel == Enum.ProductPurchaseChannel.AdReward then
print(`người chơi {receiptInfo.PlayerId} đã nhận phần thưởng video có thưởng của tôi bằng cách xem một quảng cáo`)
else
print(`người chơi {receiptInfo.PlayerId} đã mua sản phẩm video có thưởng của tôi bằng Robux`)
end
-- Tặng phần thưởng cho người chơi
grantReward(player)
else
-- Xử lý các sản phẩm khác
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
watchRewardedVideoAdRemoteEvent.OnServerEvent:Connect(function(player)
local reward = AdService:CreateAdRewardFromDevProductId(REWARDED_VIDEO_PRODUCT_ID)
AdService:ShowRewardedVideoAdAsync(player, reward)
end)ShowVideoAd
Sự Kiện
VideoAdClosed