仅在特定场景中显示 adMob 横幅; Swift
Show adMob banners only in specific scenes; Swift
在我的游戏中,我有 4 个不同的场景。
现在我想投放一些 AdMob 横幅广告,但只在这 4 个场景中的 2 个场景中。
我是这样试过的。
class GameViewController: UIViewController, GADBannerViewDelegate {
var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if var scene = StartScene(fileNamed: "StartScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
if var scene = MainMenuScene(fileNamed: "MainMenuScene") {
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
self.view.addSubview(bannerView)
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
bannerView.rootViewController = self
bannerView.delegate = self
bannerView.adUnitID = "ca-app-pub-2567181742111069/9977523039"
bannerView.load(request)
bannerView.center = CGPoint(x: 329, y: 350)
}
view.ignoresSiblingOrder = true
view.showsPhysics = false
view.showsFPS = false
view.showsNodeCount = false
}
}
问题是,广告仍然显示在每个场景中。
有人知道如何解决这个问题吗?
广告显示在每个场景中,因为横幅视图附加到 GameViewController(充当超级视图),它包含子场景。
在viewDidLoad()
中创建并加载横幅广告,将其隐藏在您不希望它出现的场景中,然后在您想要的场景中显示:
class GameViewController: UIViewController, GADBannerViewDelegate {
var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
self.view.addSubview(bannerView)
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
bannerView.rootViewController = self
bannerView.delegate = self
bannerView.adUnitID = "ca-app-pub-2567181742111069/9977523039"
bannerView.load(request)
bannerView.center = CGPoint(x: 329, y: 350)
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if var scene = StartScene(fileNamed: "StartScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
self.bannerView.isHidden = true
view.presentScene(scene)
}
if var scene = MainMenuScene(fileNamed: "MainMenuScene") {
self.bannerView.isHidden = false
}
view.ignoresSiblingOrder = true
view.showsPhysics = false
view.showsFPS = false
view.showsNodeCount = false
}
}
您还可以使用 addSubView
和 removeFromSuperview
函数从 superview(GameViewController) 及其 window
add/remove
在我的游戏中,我有 4 个不同的场景。 现在我想投放一些 AdMob 横幅广告,但只在这 4 个场景中的 2 个场景中。
我是这样试过的。
class GameViewController: UIViewController, GADBannerViewDelegate {
var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if var scene = StartScene(fileNamed: "StartScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
if var scene = MainMenuScene(fileNamed: "MainMenuScene") {
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
self.view.addSubview(bannerView)
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
bannerView.rootViewController = self
bannerView.delegate = self
bannerView.adUnitID = "ca-app-pub-2567181742111069/9977523039"
bannerView.load(request)
bannerView.center = CGPoint(x: 329, y: 350)
}
view.ignoresSiblingOrder = true
view.showsPhysics = false
view.showsFPS = false
view.showsNodeCount = false
}
}
问题是,广告仍然显示在每个场景中。
有人知道如何解决这个问题吗?
广告显示在每个场景中,因为横幅视图附加到 GameViewController(充当超级视图),它包含子场景。
在viewDidLoad()
中创建并加载横幅广告,将其隐藏在您不希望它出现的场景中,然后在您想要的场景中显示:
class GameViewController: UIViewController, GADBannerViewDelegate {
var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
self.view.addSubview(bannerView)
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
bannerView.rootViewController = self
bannerView.delegate = self
bannerView.adUnitID = "ca-app-pub-2567181742111069/9977523039"
bannerView.load(request)
bannerView.center = CGPoint(x: 329, y: 350)
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if var scene = StartScene(fileNamed: "StartScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
self.bannerView.isHidden = true
view.presentScene(scene)
}
if var scene = MainMenuScene(fileNamed: "MainMenuScene") {
self.bannerView.isHidden = false
}
view.ignoresSiblingOrder = true
view.showsPhysics = false
view.showsFPS = false
view.showsNodeCount = false
}
}
您还可以使用 addSubView
和 removeFromSuperview
函数从 superview(GameViewController) 及其 window