[英]Getting the UIViewController from an SKScene
From what I understand, the UIViewController manages interactions with the interface, such as transitions, while a View is just an object, a vessel for context.据我了解,UIViewController 管理与界面的交互,例如转换,而视图只是 object,是上下文的容器。 Therefore, I am confused on how to get the UIViewController of an SKScene
因此,我对如何获得 SKScene 的 UIViewController 感到困惑
I am trying to implement Google Mobile Ads into my project:我正在尝试在我的项目中实施Google 移动广告:
interstitial.present(fromRootViewController: /* UIViewController */)
However, one of the arguments requires an UIViewController object.但是,arguments 之一需要 UIViewController object。 I have looked at some other different questions, but most were a bit outdated and in Objective-c.
我看过其他一些不同的问题,但大多数都有些过时并且在 Objective-c 中。 Does anyone have any suggestions on the best way to get the UIViewController of an SKScene
有没有人对获得 SKScene 的 UIViewController 的最佳方法有任何建议
This is the method where I am initializing the interstitial:这是我初始化插页式广告的方法:
override func didMove(to view: SKView) {
interstitial = GADInterstitial(adUnitID: "code")
let request = GADRequest()
interstitial.load(request)
if interstitial.isReady {
// interstitial.present(fromRootViewController: /* UIViewController */)
} else {
print("Ad wasn't ready")
}
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.backgroundColor = UIColor(red:0.17, green:0.24, blue:0.31, alpha:1.0)
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
}
Your view controller is: 您的视图控制器是:
self.view?.window?.rootViewController
Example: 例:
let isReady = GADRewardBasedVideoAd.sharedInstance().isReady
guard let controller = self.view?.window?.rootViewController as? GameViewController else {return}
if isReady {
print("ADMOB: started")
GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: controller)
}
Declare the VC on your skscene and set it to self on your View Controller. 在您的场景上声明VC,然后在View Controller上将其设置为self。 Something like this:
像这样:
SKScene 场景
var viewController: myViewController!
override func didMove(to view: SKView) {
viewController.myVariable = 10
}
View Controller 查看控制器
class myViewController: UIViewController, SKSceneDelegate {
var scene: GameScene!
var myVariable: Int
override func viewDidLoad() {
super.viewDidLoad()
skView.presentScene(scene)
scene.viewController = self
}
It says it that …skView.presentScene(scene) scene.viewController = self SKView can't work and neither will skView.它说…skView.presentScene(scene) scene.viewController = self SKView 不能工作,skView 也不能。 PresentScene can't be used on type SKView
PresentScene 不能用于 SKView 类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.