Skip to content

Commit 1ed9056

Browse files
committed
fix ios and android
1 parent 95f3561 commit 1ed9056

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

android/src/main/java/com/azesmwayreactnativeunity/ReactNativeUnityViewManager.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@
2727
@ReactModule(name = ReactNativeUnityViewManager.NAME)
2828
public class ReactNativeUnityViewManager extends ReactNativeUnityViewManagerSpec<ReactNativeUnityView> implements LifecycleEventListener, View.OnAttachStateChangeListener {
2929
ReactApplicationContext reactContext;
30-
static ThemedReactContext themedContext;
30+
ReactApplicationContext context;
3131
static ReactNativeUnityView view;
3232
public static final String NAME = "RNUnityView";
3333

34+
public ReactNativeUnityViewManager(ReactApplicationContext context) {
35+
super();
36+
this.context = context;
37+
context.addLifecycleEventListener(this);
38+
}
39+
3440
@NonNull
3541
@Override
3642
public String getName() {
@@ -40,8 +46,7 @@ public String getName() {
4046
@NonNull
4147
@Override
4248
public ReactNativeUnityView createViewInstance(@NonNull ThemedReactContext context) {
43-
themedContext = context;
44-
view = new ReactNativeUnityView(context);
49+
view = new ReactNativeUnityView(this.context);
4550
view.addOnAttachStateChangeListener(this);
4651

4752
if (getPlayer() != null) {
@@ -226,7 +231,7 @@ public void setFullScreen(ReactNativeUnityView view, boolean fullScreen) {
226231
public void postMessage(ReactNativeUnityView view, String gameObject, String methodName, String message) {
227232
if (isUnityReady()) {
228233
assert getPlayer() != null;
229-
getPlayer().UnitySendMessage(gameObject, methodName, message);
234+
UnityPlayer.UnitySendMessage(gameObject, methodName, message);
230235
}
231236
}
232237
}

ios/RNUnityView.mm

+34-36
Original file line numberDiff line numberDiff line change
@@ -37,44 +37,42 @@ - (bool)unityIsInitialized {
3737
}
3838

3939
- (void)initUnityModule {
40-
dispatch_async(dispatch_get_main_queue(), ^{
41-
@try {
42-
if([self unityIsInitialized]) {
43-
return;
44-
}
45-
46-
[self setUfw: UnityFrameworkLoad()];
47-
[[self ufw] registerFrameworkListener: self];
48-
49-
unsigned count = (int) [[[NSProcessInfo processInfo] arguments] count];
50-
char **array = (char **)malloc((count + 1) * sizeof(char*));
51-
52-
for (unsigned i = 0; i < count; i++)
53-
{
54-
array[i] = strdup([[[[NSProcessInfo processInfo] arguments] objectAtIndex:i] UTF8String]);
55-
}
56-
array[count] = NULL;
57-
58-
[[self ufw] runEmbeddedWithArgc: gArgc argv: array appLaunchOpts: appLaunchOpts];
59-
[[self ufw] appController].quitHandler = ^(){ NSLog(@"AppController.quitHandler called"); };
60-
[self.ufw.appController.rootView removeFromSuperview];
61-
62-
if (@available(iOS 13.0, *)) {
63-
[[[[self ufw] appController] window] setWindowScene: nil];
64-
} else {
65-
[[[[self ufw] appController] window] setScreen: nil];
66-
}
67-
68-
[[[[self ufw] appController] window] addSubview: self.ufw.appController.rootView];
69-
[[[[self ufw] appController] window] makeKeyAndVisible];
70-
[[[[[[self ufw] appController] window] rootViewController] view] setNeedsLayout];
71-
72-
[NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
40+
@try {
41+
if([self unityIsInitialized]) {
42+
return;
7343
}
74-
@catch (NSException *e) {
75-
NSLog(@"%@",e);
44+
45+
[self setUfw: UnityFrameworkLoad()];
46+
[[self ufw] registerFrameworkListener: self];
47+
48+
unsigned count = (int) [[[NSProcessInfo processInfo] arguments] count];
49+
char **array = (char **)malloc((count + 1) * sizeof(char*));
50+
51+
for (unsigned i = 0; i < count; i++)
52+
{
53+
array[i] = strdup([[[[NSProcessInfo processInfo] arguments] objectAtIndex:i] UTF8String]);
7654
}
77-
});
55+
array[count] = NULL;
56+
57+
[[self ufw] runEmbeddedWithArgc: gArgc argv: array appLaunchOpts: appLaunchOpts];
58+
[[self ufw] appController].quitHandler = ^(){ NSLog(@"AppController.quitHandler called"); };
59+
[self.ufw.appController.rootView removeFromSuperview];
60+
61+
if (@available(iOS 13.0, *)) {
62+
[[[[self ufw] appController] window] setWindowScene: nil];
63+
} else {
64+
[[[[self ufw] appController] window] setScreen: nil];
65+
}
66+
67+
[[[[self ufw] appController] window] addSubview: self.ufw.appController.rootView];
68+
[[[[self ufw] appController] window] makeKeyAndVisible];
69+
[[[[[[self ufw] appController] window] rootViewController] view] setNeedsLayout];
70+
71+
[NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
72+
}
73+
@catch (NSException *e) {
74+
NSLog(@"%@",e);
75+
}
7876
}
7977

8078
- (void)layoutSubviews {

0 commit comments

Comments
 (0)