-
Notifications
You must be signed in to change notification settings - Fork 75
/
UPlayer.java
115 lines (89 loc) · 3.65 KB
/
UPlayer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.azesmwayreactnativeunity;
import android.app.Activity;
import android.content.res.Configuration;
import android.widget.FrameLayout;
import com.unity3d.player.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class UPlayer {
private static UnityPlayer unityPlayer;
public UPlayer(final Activity activity, final ReactNativeUnity.UnityPlayerCallback callback) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
super();
Class<?> _player = null;
try {
_player = Class.forName("com.unity3d.player.UnityPlayerForActivityOrService");
} catch (ClassNotFoundException e) {
_player = Class.forName("com.unity3d.player.UnityPlayer");
}
Constructor<?> constructor = _player.getConstructors()[1];
unityPlayer = (UnityPlayer) constructor.newInstance(activity, new IUnityPlayerLifecycleEvents() {
@Override
public void onUnityPlayerUnloaded() {
callback.onUnload();
}
@Override
public void onUnityPlayerQuitted() {
callback.onQuit();
}
});
}
public static void UnitySendMessage(String gameObject, String methodName, String message) {
UnityPlayer.UnitySendMessage(gameObject, methodName, message);
}
public void pause() {
unityPlayer.pause();
}
public void windowFocusChanged(boolean b) {
unityPlayer.windowFocusChanged(b);
}
public void resume() {
unityPlayer.resume();
}
public void unload() {
unityPlayer.unload();
}
public Object getParentPlayer() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
try {
Method getFrameLayout = unityPlayer.getClass().getMethod("getFrameLayout");
FrameLayout frame = (FrameLayout) this.requestFrame();
return frame.getParent();
} catch (NoSuchMethodException e) {
Method getParent = unityPlayer.getClass().getMethod("getParent");
return getParent.invoke(unityPlayer);
}
}
public void configurationChanged(Configuration newConfig) {
unityPlayer.configurationChanged(newConfig);
}
public void destroy() {
unityPlayer.destroy();
}
public void requestFocusPlayer() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
try {
Method getFrameLayout = unityPlayer.getClass().getMethod("getFrameLayout");
FrameLayout frame = (FrameLayout) this.requestFrame();
frame.requestFocus();
} catch (NoSuchMethodException e) {
Method requestFocus = unityPlayer.getClass().getMethod("requestFocus");
requestFocus.invoke(unityPlayer);
}
}
public FrameLayout requestFrame() throws NoSuchMethodException {
try {
Method getFrameLayout = unityPlayer.getClass().getMethod("getFrameLayout");
return (FrameLayout) getFrameLayout.invoke(unityPlayer);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
return unityPlayer;
}
}
public void setZ(float v) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
try {
Method setZ = unityPlayer.getClass().getMethod("setZ");
setZ.invoke(unityPlayer, v);
} catch (NoSuchMethodException e) {}
}
public Object getContextPlayer() {
return unityPlayer.getContext();
}
}