Skip to content

Commit 2ba080f

Browse files
author
Martin Straub
committed
added startUnity and stopUnity methods to be able to pause unity and stop it from consuming cpu cycles and battery life
1 parent cfa37d5 commit 2ba080f

File tree

2 files changed

+72
-35
lines changed

2 files changed

+72
-35
lines changed

README.md

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ First add the following variable to your `AppDelegate`
254254
var currentUnityController: UnityAppController!
255255
```
256256
Now we need to initialize and proxy through the calls to the `UnityAppController`.
257+
257258
All said and done you will be left with the following:
258259

259260
```swift
@@ -262,39 +263,73 @@ All said and done you will be left with the following:
262263
//
263264
// Created by Adam Venturella on 10/28/15
264265
//
266+
// Updated by Martin Straub on 15/03/2017.
267+
// Added some stuff to pause unity in order to stop consuming cpu cylces and battery life, when not being displayed.
268+
// Indeed, unity will still sit in memory all the time, but that seems to be a more complex thing to solve.
269+
// Just use `startUnity` and `stopUnity` for running/pausing unity (see also ViewController example below).
270+
//
265271

266272
import UIKit
267273

268274
class AppDelegate: UIResponder, UIApplicationDelegate {
269-
270-
var window: UIWindow?
271-
var currentUnityController: UnityAppController!
272-
273-
275+
var currentUnityController: UnityAppController?
276+
var application: UIApplication?
277+
var isUnityRunning = false
278+
274279
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
280+
self.application = application
275281
currentUnityController = UnityAppController()
276-
currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)
282+
currentUnityController!.application(application, didFinishLaunchingWithOptions: launchOptions)
283+
284+
// first call to startUnity will do some init stuff, so just call it here and directly stop it again
285+
startUnity()
286+
stopUnity()
287+
277288
return true
278289
}
279-
290+
280291
func applicationWillResignActive(_ application: UIApplication) {
281-
currentUnityController.applicationWillResignActive(application)
292+
if isUnityRunning {
293+
currentUnityController?.applicationWillResignActive(application)
294+
}
282295
}
283-
296+
284297
func applicationDidEnterBackground(_ application: UIApplication) {
285-
currentUnityController.applicationDidEnterBackground(application)
298+
if isUnityRunning {
299+
currentUnityController?.applicationDidEnterBackground(application)
300+
}
286301
}
287-
302+
288303
func applicationWillEnterForeground(_ application: UIApplication) {
289-
currentUnityController.applicationWillEnterForeground(application)
304+
if isUnityRunning {
305+
currentUnityController?.applicationWillEnterForeground(application)
306+
}
290307
}
291-
308+
292309
func applicationDidBecomeActive(_ application: UIApplication) {
293-
currentUnityController.applicationDidBecomeActive(application)
310+
if isUnityRunning {
311+
currentUnityController?.applicationDidBecomeActive(application)
312+
}
294313
}
295-
314+
296315
func applicationWillTerminate(_ application: UIApplication) {
297-
currentUnityController.applicationWillTerminate(application)
316+
if isUnityRunning {
317+
currentUnityController?.applicationWillTerminate(application)
318+
}
319+
}
320+
321+
func startUnity() {
322+
if !isUnityRunning {
323+
isUnityRunning = true
324+
currentUnityController!.applicationDidBecomeActive(application!)
325+
}
326+
}
327+
328+
func stopUnity() {
329+
if isUnityRunning {
330+
currentUnityController!.applicationWillResignActive(application!)
331+
isUnityRunning = false
332+
}
298333
}
299334
}
300335

@@ -344,34 +379,35 @@ file for me attached to a storyboard. Here is how I hooked up my little demo:
344379
// ViewController.swift
345380
//
346381
// Created by Adam Venturella on 10/28/15.
382+
// Updated by Martin Straub on 15/03/2017.
347383
//
348384

349385
import UIKit
350386

351387
class ViewController: UIViewController {
352-
353-
@IBAction func onLoadUnity(sender: AnyObject) {
354-
loadUnity()
355-
}
356-
357-
@IBAction func onCallUnity(sender: AnyObject) {
358-
UnitySendMessage("EventBus", "Trigger", "Hello World")
359-
}
360-
361-
func loadUnity(){
362-
363-
let unityView = UnityGetGLView()
364-
365-
self.view.addSubview(unityView)
366-
unityView.translatesAutoresizingMaskIntoConstraints = false
367-
388+
var unityView: UIView?
389+
390+
@IBAction func startUnity(sender: AnyObject) {
391+
let appDelegate = UIApplication.shared.delegate as! AppDelegate
392+
appDelegate.startUnity()
393+
394+
unityView = UnityGetGLView()!
395+
396+
self.view!.addSubview(unityView!)
397+
unityView!.translatesAutoresizingMaskIntoConstraints = false
398+
368399
// look, non-full screen unity content!
369400
let views = ["view": unityView]
370-
let w = NSLayoutConstraint.constraintsWithVisualFormat("|[view]-20-|", options: [], metrics: nil, views: views)
371-
let h = NSLayoutConstraint.constraintsWithVisualFormat("V:|-75-[view]-50-|", options: [], metrics: nil, views: views)
372-
401+
let w = NSLayoutConstraint.constraints(withVisualFormat: "|-20-[view]-20-|", options: [], metrics: nil, views: views)
402+
let h = NSLayoutConstraint.constraints(withVisualFormat: "V:|-75-[view]-50-|", options: [], metrics: nil, views: views)
373403
view.addConstraints(w + h)
374404
}
405+
406+
@IBAction func stopUnity(sender: AnyObject) {
407+
let appDelegate = UIApplication.shared.delegate as! AppDelegate
408+
appDelegate.stopUnity()
409+
unityView!.removeFromSuperview()
410+
}
375411
}
376412

377413
```

Unity.xcconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ SWIFT_OBJC_BRIDGING_HEADER = UnityBridge.h;
2121
OTHER_CFLAGS = -DINIT_SCRIPTING_BACKEND=1;
2222
CLANG_CXX_LANGUAGE_STANDARD = c++11;
2323
CLANG_CXX_LIBRARY = libc++;
24+
CLANG_ENABLE_MODULES = NO;
2425
CLANG_WARN_BOOL_CONVERSION = NO;
2526
CLANG_WARN_CONSTANT_CONVERSION = NO;
2627
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES;

0 commit comments

Comments
 (0)