|
1 | 1 | part of flutter_unity_widget; |
2 | 2 |
|
3 | | -typedef void UnityWidgetCreatedCallback(UnityWidgetController controller); |
| 3 | +typedef void UnityCreatedCallback(UnityWidgetController controller); |
| 4 | + |
| 5 | +final UnityViewFlutterPlatform _unityViewFlutterPlatform = |
| 6 | + UnityViewFlutterPlatform.instance; |
4 | 7 |
|
5 | 8 | class UnityWidgetController { |
6 | 9 | final _UnityWidgetState _unityWidgetState; |
7 | | - final MethodChannel channel; |
8 | 10 |
|
9 | | - UnityWidgetController._( |
10 | | - this.channel, |
11 | | - this._unityWidgetState, |
12 | | - ) { |
13 | | - channel.setMethodCallHandler(_handleMethod); |
| 11 | + /// The unityId for this controller |
| 12 | + final int unityId; |
| 13 | + |
| 14 | + UnityWidgetController._(this._unityWidgetState, {@required this.unityId}) |
| 15 | + : assert(_unityViewFlutterPlatform != null) { |
| 16 | + _connectStreams(unityId); |
14 | 17 | } |
15 | 18 |
|
16 | | - static UnityWidgetController init( |
17 | | - int id, _UnityWidgetState unityWidgetState) { |
18 | | - final MethodChannel channel = |
19 | | - MethodChannel('plugins.xraph.com/unity_view_$id'); |
| 19 | + /// Initialize [UnityWidgetController] with [id] |
| 20 | + /// Mainly for internal use when instantiating a [UnityWidgetController] passed |
| 21 | + /// in [UnityWidget.onUnityCreated] callback. |
| 22 | + static Future<UnityWidgetController> init( |
| 23 | + int id, _UnityWidgetState unityWidgetState) async { |
| 24 | + assert(id != null); |
| 25 | + await _unityViewFlutterPlatform.init(id); |
20 | 26 | return UnityWidgetController._( |
21 | | - channel, |
22 | 27 | unityWidgetState, |
| 28 | + unityId: id, |
23 | 29 | ); |
24 | 30 | } |
25 | 31 |
|
26 | | - Future<bool> isReady() async { |
27 | | - final bool isReady = await channel.invokeMethod('isReady'); |
28 | | - return isReady; |
| 32 | + @visibleForTesting |
| 33 | + MethodChannel get channel { |
| 34 | + if (_unityViewFlutterPlatform is MethodChannelUnityViewFlutter) { |
| 35 | + return (_unityViewFlutterPlatform as MethodChannelUnityViewFlutter) |
| 36 | + .channel(unityId); |
| 37 | + } |
| 38 | + return null; |
29 | 39 | } |
30 | 40 |
|
31 | | - Future<bool> isPaused() async { |
32 | | - final bool isReady = await channel.invokeMethod('isPaused'); |
33 | | - return isReady; |
34 | | - } |
| 41 | + void _connectStreams(int unityId) { |
| 42 | + if (_unityWidgetState.widget.onUnityMessage != null) { |
| 43 | + _unityViewFlutterPlatform.onUnityMessage(unityId: unityId).listen( |
| 44 | + (UnityMessageEvent e) => |
| 45 | + _unityWidgetState.widget.onUnityMessage(e.value)); |
| 46 | + } |
| 47 | + |
| 48 | + if (_unityWidgetState.widget.onUnitySceneLoaded != null) { |
| 49 | + _unityViewFlutterPlatform.onUnitySceneLoaded(unityId: unityId).listen( |
| 50 | + (SceneLoadedEvent e) => |
| 51 | + _unityWidgetState.widget.onUnitySceneLoaded(e.value)); |
| 52 | + } |
35 | 53 |
|
36 | | - Future<bool> isLoaded() async { |
37 | | - final bool isReady = await channel.invokeMethod('isLoaded'); |
38 | | - return isReady; |
| 54 | + if (_unityWidgetState.widget.onUnityUnloaded != null) { |
| 55 | + _unityViewFlutterPlatform |
| 56 | + .onUnityUnloaded(unityId: unityId) |
| 57 | + .listen((_) => _unityWidgetState.widget.onUnityUnloaded()); |
| 58 | + } |
39 | 59 | } |
40 | 60 |
|
41 | | - Future<bool> isInBackground() async { |
42 | | - final bool isReady = await channel.invokeMethod('isInBackground'); |
43 | | - return isReady; |
| 61 | + /// Checks to see if unity player is ready to be used |
| 62 | + /// Returns `true` if unity player is ready. |
| 63 | + Future<bool> isReady() { |
| 64 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 65 | + return _unityViewFlutterPlatform.isReady(unityId: unityId); |
| 66 | + } |
| 67 | + return null; |
44 | 68 | } |
45 | 69 |
|
46 | | - Future<bool> createUnity() async { |
47 | | - final bool isReady = await channel.invokeMethod('createUnity'); |
48 | | - return isReady; |
| 70 | + /// Get the current pause state of the unity player |
| 71 | + /// Returns `true` if unity player is paused. |
| 72 | + Future<bool> isPaused() { |
| 73 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 74 | + return _unityViewFlutterPlatform.isPaused(unityId: unityId); |
| 75 | + } |
| 76 | + return null; |
49 | 77 | } |
50 | 78 |
|
51 | | - postMessage(String gameObject, methodName, message) { |
52 | | - channel.invokeMethod('postMessage', <String, dynamic>{ |
53 | | - 'gameObject': gameObject, |
54 | | - 'methodName': methodName, |
55 | | - 'message': message, |
56 | | - }); |
| 79 | + /// Get the current load state of the unity player |
| 80 | + /// Returns `true` if unity player is loaded. |
| 81 | + Future<bool> isLoaded() { |
| 82 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 83 | + return _unityViewFlutterPlatform.isLoaded(unityId: unityId); |
| 84 | + } |
| 85 | + return null; |
57 | 86 | } |
58 | 87 |
|
59 | | - pause() async { |
60 | | - await channel.invokeMethod('pause'); |
| 88 | + /// Helper method to know if Unity has been put in background mode (WIP) unstable |
| 89 | + /// Returns `true` if unity player is in background. |
| 90 | + Future<bool> inBackground() { |
| 91 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 92 | + return _unityViewFlutterPlatform.inBackground(unityId: unityId); |
| 93 | + } |
| 94 | + return null; |
61 | 95 | } |
62 | 96 |
|
63 | | - resume() async { |
64 | | - await channel.invokeMethod('resume'); |
| 97 | + /// Creates a unity player if it's not already created. Please only call this if unity is not ready, |
| 98 | + /// or is in unloaded state. Use [isLoaded] to check. |
| 99 | + /// Returns `true` if unity player was created succesfully. |
| 100 | + Future<bool> create() { |
| 101 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 102 | + return _unityViewFlutterPlatform.createUnityPlayer(unityId: unityId); |
| 103 | + } |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + /// Post message to unity from flutter. This method takes in a string [message]. |
| 109 | + /// The [gameObject] must match the name of an actual unity game object in a scene at runtime, and the [methodName], |
| 110 | + /// must exist in a `MonoDevelop` `class` and also exposed as a method. [message] is an parameter taken by the method |
| 111 | + /// |
| 112 | + /// ```dart |
| 113 | + /// postMessage("GameManager", "openScene", "ThirdScene") |
| 114 | + /// ``` |
| 115 | + Future<void> postMessage(String gameObject, methodName, message) { |
| 116 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 117 | + return _unityViewFlutterPlatform.postMessage( |
| 118 | + unityId: unityId, |
| 119 | + gameObject: gameObject, |
| 120 | + methodName: methodName, |
| 121 | + message: message, |
| 122 | + ); |
| 123 | + } |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + /// Post message to unity from flutter. This method takes in a Json or map structure as the [message]. |
| 128 | + /// The [gameObject] must match the name of an actual unity game object in a scene at runtime, and the [methodName], |
| 129 | + /// must exist in a `MonoDevelop` `class` and also exposed as a method. [message] is an parameter taken by the method |
| 130 | + /// |
| 131 | + /// ```dart |
| 132 | + /// postJsonMessage("GameManager", "openScene", {"buildIndex": 3, "name": "ThirdScene"}) |
| 133 | + /// ``` |
| 134 | + Future<void> postJsonMessage( |
| 135 | + String gameObject, String methodName, Map<String, dynamic> message) { |
| 136 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 137 | + return _unityViewFlutterPlatform.postJsonMessage( |
| 138 | + unityId: unityId, |
| 139 | + gameObject: gameObject, |
| 140 | + methodName: methodName, |
| 141 | + message: message, |
| 142 | + ); |
| 143 | + } |
| 144 | + return null; |
65 | 145 | } |
66 | 146 |
|
67 | | - /// Opens unity in it's own activity. Android only. |
68 | | - openNative() async { |
69 | | - await channel.invokeMethod('openNative'); |
| 147 | + /// Pause the unity in-game player with this method |
| 148 | + Future<void> pause() { |
| 149 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 150 | + return _unityViewFlutterPlatform.pausePlayer(unityId: unityId); |
| 151 | + } |
| 152 | + return null; |
70 | 153 | } |
71 | 154 |
|
72 | | - unload() async { |
73 | | - await channel.invokeMethod('unload'); |
| 155 | + /// Resume the unity in-game player with this method idf it is in a paused state |
| 156 | + Future<void> resume() { |
| 157 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 158 | + return _unityViewFlutterPlatform.resumePlayer(unityId: unityId); |
| 159 | + } |
| 160 | + return null; |
74 | 161 | } |
75 | 162 |
|
76 | | - quitPlayer() async { |
77 | | - await channel.invokeMethod('quitPlayer'); |
| 163 | + /// Sometimes you want to open unity in it's own process and openInNativeProcess does just that. |
| 164 | + /// It works for Android and iOS is WIP |
| 165 | + Future<void> openInNativeProcess() { |
| 166 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 167 | + return _unityViewFlutterPlatform.openInNativeProcess(unityId: unityId); |
| 168 | + } |
| 169 | + return null; |
78 | 170 | } |
79 | 171 |
|
80 | | - silentQuitPlayer() async { |
81 | | - await channel.invokeMethod('silentQuitPlayer'); |
| 172 | + /// Sometimes you want to open unity in it's own process and openNative does just that. |
| 173 | + /// It works for Android and iOS is WIP |
| 174 | + @Deprecated('Prefer to use the openInNativeProcess() method') |
| 175 | + Future<void> openNative() { |
| 176 | + return openInNativeProcess(); |
82 | 177 | } |
83 | 178 |
|
84 | | - Future<void> _dispose() async { |
85 | | - await channel.invokeMethod('dispose'); |
| 179 | + /// Unloads unity player from th current process (Works on Android only for now) |
| 180 | + /// iOS is WIP. For more information please read [Unity Docs](https://docs.unity3d.com/2020.2/Documentation/Manual/UnityasaLibrary.html) |
| 181 | + Future<void> unload() { |
| 182 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 183 | + return _unityViewFlutterPlatform.unloadPlayer(unityId: unityId); |
| 184 | + } |
| 185 | + return null; |
86 | 186 | } |
87 | 187 |
|
88 | | - Future<dynamic> _handleMethod(MethodCall call) async { |
89 | | - switch (call.method) { |
90 | | - case "onUnityMessage": |
91 | | - if (_unityWidgetState.widget != null) { |
92 | | - _unityWidgetState.widget.onUnityMessage(this, call.arguments); |
93 | | - } |
94 | | - break; |
95 | | - case "onUnityUnloaded": |
96 | | - if (_unityWidgetState.widget != null) { |
97 | | - _unityWidgetState.widget.onUnityUnloaded(this); |
98 | | - } |
99 | | - break; |
100 | | - case "onUnitySceneLoaded": |
101 | | - if (_unityWidgetState.widget != null) { |
102 | | - _unityWidgetState.widget.onUnitySceneLoaded( |
103 | | - this, |
104 | | - name: call.arguments['name'], |
105 | | - buildIndex: call.arguments['buildIndex'], |
106 | | - isLoaded: call.arguments['isLoaded'], |
107 | | - isValid: call.arguments['isValid'], |
108 | | - ); |
109 | | - } |
110 | | - break; |
111 | | - default: |
112 | | - throw UnimplementedError("Unimplemented ${call.method} method"); |
| 188 | + /// quit method quits unity player. Note that this kills the current flutter process, thus quiting the app |
| 189 | + /// It optionally takes in [silent] which is a WIP to mitigate killing the flutter process |
| 190 | + Future<void> quit({bool silent}) { |
| 191 | + if (!_unityWidgetState.widget.enablePlaceholder) { |
| 192 | + return _unityViewFlutterPlatform.quitPlayer( |
| 193 | + unityId: unityId, silent: silent); |
113 | 194 | } |
| 195 | + return null; |
| 196 | + } |
| 197 | + |
| 198 | + /// quitPlayer method quits unity player. Note that this kills the current flutter process, thus quiting the app |
| 199 | + /// It optionally takes in [silent] which is a WIP to mitigate killing the flutter process |
| 200 | + @Deprecated('Prefer to use the quit() method') |
| 201 | + Future<void> quitPlayer({bool silent}) { |
| 202 | + return quit(silent: silent); |
| 203 | + } |
| 204 | + |
| 205 | + void dispose() { |
| 206 | + return _unityViewFlutterPlatform.dispose(unityId: unityId); |
114 | 207 | } |
115 | 208 | } |
116 | 209 |
|
117 | | -typedef onUnityMessageCallback = void Function( |
118 | | - UnityWidgetController controller, dynamic handler); |
| 210 | +typedef void UnityMessageCallback(dynamic handler); |
119 | 211 |
|
120 | | -typedef onUnitySceneChangeCallback = void Function( |
121 | | - UnityWidgetController controller, { |
122 | | - String name, |
123 | | - int buildIndex, |
124 | | - bool isLoaded, |
125 | | - bool isValid, |
126 | | -}); |
| 212 | +typedef void UnitySceneChangeCallback(SceneLoaded message); |
127 | 213 |
|
128 | | -typedef onUnityUnloadCallback = void Function(UnityWidgetController controller); |
| 214 | +typedef void UnityUnloadCallback(); |
0 commit comments