@@ -24,6 +24,7 @@ class FlutterUnityController {
2424
2525 /// The unityId for this controller
2626 int lastUnityId = 0 ;
27+ bool _testMode = false ;
2728
2829 /// used for cancel the subscription
2930 StreamSubscription ? _onUnityMessageSub,
@@ -39,34 +40,61 @@ class FlutterUnityController {
3940 return FlutterUnityPlatform .instance.stream;
4041 }
4142
43+ /// This method enables test mode where no api calls
44+ /// gets to the native side
45+ void enableTestMode () {
46+ _testMode = true ;
47+ }
48+
49+ /// This method disables test mode where no api calls
50+ /// gets to the native side
51+ void disableTestMode () {
52+ _testMode = false ;
53+ }
54+
4255 /// Checks to see if unity player is ready to be used
4356 /// Returns `true` if unity player is ready.
4457 Future <bool ?>? isReady () {
58+ if (_testMode) {
59+ return Future .value (true );
60+ }
4561 return FlutterUnityPlatform .instance.isReady ();
4662 }
4763
4864 /// Get the current pause state of the unity player
4965 /// Returns `true` if unity player is paused.
5066 Future <bool ?>? isPaused () {
67+ if (_testMode) {
68+ return Future .value (false );
69+ }
5170 return FlutterUnityPlatform .instance.isPaused ();
5271 }
5372
5473 /// Get the current load state of the unity player
5574 /// Returns `true` if unity player is loaded.
5675 Future <bool ?>? isLoaded () {
76+ if (_testMode) {
77+ return Future .value (true );
78+ }
5779 return FlutterUnityPlatform .instance.isLoaded ();
5880 }
5981
6082 /// Helper method to know if Unity has been put in background mode (WIP) unstable
6183 /// Returns `true` if unity player is in background.
6284 Future <bool ?>? inBackground () {
85+ if (_testMode) {
86+ return Future .value (false );
87+ }
6388 return FlutterUnityPlatform .instance.inBackground ();
6489 }
6590
6691 /// Creates a unity player if it's not already created. Please only call this if unity is not ready,
6792 /// or is in unloaded state. Use [isLoaded] to check.
6893 /// Returns `true` if unity player was created succesfully.
6994 Future <bool ?>? create () {
95+ if (_testMode) {
96+ return Future .value (true );
97+ }
7098 return FlutterUnityPlatform .instance.createUnityPlayer ();
7199 }
72100
@@ -78,6 +106,10 @@ class FlutterUnityController {
78106 /// postMessage("GameManager", "openScene", "ThirdScene")
79107 /// ```
80108 Future <void >? postMessage (String gameObject, methodName, message) {
109+ if (_testMode) {
110+ return Future .value (null );
111+ }
112+
81113 return FlutterUnityPlatform .instance.postMessage (
82114 gameObject: gameObject,
83115 methodName: methodName,
@@ -94,6 +126,10 @@ class FlutterUnityController {
94126 /// ```
95127 Future <void >? postJsonMessage (
96128 String gameObject, String methodName, Map <String , dynamic > message) {
129+ if (_testMode) {
130+ return Future .value (null );
131+ }
132+
97133 return FlutterUnityPlatform .instance.postJsonMessage (
98134 gameObject: gameObject,
99135 methodName: methodName,
@@ -103,28 +139,48 @@ class FlutterUnityController {
103139
104140 /// Pause the unity in-game player with this method
105141 Future <void >? pause () {
142+ if (_testMode) {
143+ return Future .value (null );
144+ }
145+
106146 return FlutterUnityPlatform .instance.pausePlayer ();
107147 }
108148
109149 /// Resume the unity in-game player with this method idf it is in a paused state
110150 Future <void >? resume () {
151+ if (_testMode) {
152+ return Future .value (null );
153+ }
154+
111155 return FlutterUnityPlatform .instance.resumePlayer ();
112156 }
113157
114158 /// Sometimes you want to open unity in it's own process and openInNativeProcess does just that.
115159 /// It works for Android and iOS is WIP
116160 Future <void >? openInNativeProcess () {
161+ if (_testMode) {
162+ return Future .value (null );
163+ }
164+
117165 return FlutterUnityPlatform .instance.openInNativeProcess ();
118166 }
119167
120168 /// Unloads unity player from th current process (Works on Android only for now)
121169 /// iOS is WIP. For more information please read [Unity Docs] (https://docs.unity3d.com/2020.2/Documentation/Manual/UnityasaLibrary.html)
122170 Future <void >? unload () {
171+ if (_testMode) {
172+ return Future .value (null );
173+ }
174+
123175 return FlutterUnityPlatform .instance.unloadPlayer ();
124176 }
125177
126178 /// Quits unity player. Note that this kills the current flutter process, thus quiting the app
127179 Future <void >? quit () {
180+ if (_testMode) {
181+ return Future .value (null );
182+ }
183+
128184 return FlutterUnityPlatform .instance.quitPlayer ();
129185 }
130186
@@ -157,6 +213,8 @@ class FlutterUnityController {
157213
158214 Future <void > dispose () async {
159215 _cancelSubscriptions ();
160- await FlutterUnityPlatform .instance.dispose ();
216+ if (! _testMode) {
217+ await FlutterUnityPlatform .instance.dispose ();
218+ }
161219 }
162220}
0 commit comments