Skip to content

Commit f3491a9

Browse files
Wesley Bosf111fei
authored andcommitted
Add iOS and Android Unity pause/resume functions
1 parent 793da68 commit f3491a9

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

android/src/main/java/com/reactnative/unity/view/UnityViewManager.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class UnityViewManager extends SimpleViewManager<UnityView> implements Li
1919
private static final String REACT_CLASS = "UnityView";
2020

2121
public static final int COMMAND_POST_MESSAGE = 1;
22+
public static final int COMMAND_PAUSE = 2;
23+
public static final int COMMAND_RESUME = 3;
2224

2325
private ReactApplicationContext context;
2426

@@ -36,7 +38,9 @@ public String getName() {
3638
@Override
3739
public @Nullable Map<String, Integer> getCommandsMap() {
3840
return MapBuilder.of(
39-
"postMessage", COMMAND_POST_MESSAGE
41+
"postMessage", COMMAND_POST_MESSAGE,
42+
"pause", COMMAND_PAUSE,
43+
"resume", COMMAND_RESUME
4044
);
4145
}
4246

@@ -49,6 +53,12 @@ public void receiveCommand(UnityView root, int commandId, @Nullable ReadableArra
4953
String message = args.getString(2);
5054
UnityUtils.postMessage(gameObject, methodName, message);
5155
break;
56+
case COMMAND_PAUSE:
57+
UnityUtils.getPlayer().pause();
58+
break;
59+
case COMMAND_RESUME:
60+
UnityUtils.getPlayer().resume();
61+
break;
5262
}
5363
}
5464

ios/RNUnityViewManager.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,14 @@ - (void)setBridge:(RCTBridge *)bridge {
8484
UnityPostMessage(gameObject, methodName, message);
8585
}
8686

87+
RCT_EXPORT_METHOD(pause:(nonnull NSNumber *)reactTag)
88+
{
89+
UnityPauseCommand();
90+
}
91+
92+
RCT_EXPORT_METHOD(resume:(nonnull NSNumber *)reactTag)
93+
{
94+
UnityResumeCommand();
95+
}
96+
8797
@end

ios/UnityUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ void InitUnity(void);
1313

1414
void UnityPostMessage(NSString* gameObject, NSString* methodName, NSString* message);
1515

16+
void UnityPauseCommand();
17+
18+
void UnityResumeCommand();
19+
1620
#ifdef __cplusplus
1721
} // extern "C"
1822
#endif

ios/UnityUtils.mm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@
6262
UnitySendMessage([gameObject UTF8String], [methodName UTF8String], [message UTF8String]);
6363
}
6464

65+
extern "C" void UnityPauseCommand()
66+
{
67+
dispatch_async(dispatch_get_main_queue(), ^{
68+
UnityPause(1);
69+
});
70+
}
71+
72+
extern "C" void UnityResumeCommand()
73+
{
74+
dispatch_async(dispatch_get_main_queue(), ^{
75+
UnityPause(0);
76+
});
77+
}
78+
6579
@implementation UnityUtils
6680

6781
static NSHashTable* mUnityEventListeners = [NSHashTable weakObjectsHashTable];

src/index.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ export default class UnityView extends React.Component<UnityViewProps> {
3838
);
3939
};
4040

41+
/**
42+
* Pause the unity player
43+
*/
44+
public pause() {
45+
UIManager.dispatchViewManagerCommand(
46+
this.getViewHandle(),
47+
UIManager.UnityView.Commands.pause,
48+
[]
49+
);
50+
};
51+
52+
/**
53+
* Resume the unity player
54+
*/
55+
public resume() {
56+
UIManager.dispatchViewManagerCommand(
57+
this.getViewHandle(),
58+
UIManager.UnityView.Commands.resume,
59+
[]
60+
);
61+
};
62+
4163
/**
4264
* Send Message to UnityMessageManager.
4365
* @param message The message will post.

0 commit comments

Comments
 (0)