|
6 | 6 | import android.view.ViewGroup; |
7 | 7 | import android.view.WindowManager; |
8 | 8 |
|
9 | | -import com.unity3d.player.UnityPlayer; |
10 | | -import com.unity3d.player.IUnityPlayerLifecycleEvents; |
11 | | - |
12 | 9 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
13 | 10 |
|
| 11 | +import java.lang.reflect.InvocationTargetException; |
| 12 | + |
14 | 13 | public class ReactNativeUnity { |
15 | | - private static UnityPlayer unityPlayer; |
16 | | - public static boolean _isUnityReady; |
17 | | - public static boolean _isUnityPaused; |
18 | | - public static boolean _fullScreen; |
19 | | - |
20 | | - public static UnityPlayer getPlayer() { |
21 | | - if (!_isUnityReady) { |
22 | | - return null; |
| 14 | + private static UPlayer unityPlayer; |
| 15 | + public static boolean _isUnityReady; |
| 16 | + public static boolean _isUnityPaused; |
| 17 | + public static boolean _fullScreen; |
| 18 | + |
| 19 | + public static UPlayer getPlayer() { |
| 20 | + if (!_isUnityReady) { |
| 21 | + return null; |
| 22 | + } |
| 23 | + return unityPlayer; |
23 | 24 | } |
24 | | - return unityPlayer; |
25 | | - } |
26 | | - |
27 | | - public static boolean isUnityReady() { |
28 | | - return _isUnityReady; |
29 | | - } |
30 | 25 |
|
31 | | - public static boolean isUnityPaused() { |
32 | | - return _isUnityPaused; |
33 | | - } |
| 26 | + public static boolean isUnityReady() { |
| 27 | + return _isUnityReady; |
| 28 | + } |
34 | 29 |
|
35 | | - public static void createPlayer(final Activity activity, final UnityPlayerCallback callback) { |
36 | | - if (unityPlayer != null) { |
37 | | - callback.onReady(); |
38 | | - return; |
| 30 | + public static boolean isUnityPaused() { |
| 31 | + return _isUnityPaused; |
39 | 32 | } |
40 | | - if (activity != null) { |
41 | | - activity.runOnUiThread(new Runnable() { |
42 | | - @Override |
43 | | - public void run() { |
44 | | - activity.getWindow().setFormat(PixelFormat.RGBA_8888); |
45 | | - int flag = activity.getWindow().getAttributes().flags; |
46 | | - boolean fullScreen = false; |
47 | | - if ((flag & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) { |
48 | | - fullScreen = true; |
49 | | - } |
50 | | - |
51 | | - unityPlayer = new UnityPlayer(activity, new IUnityPlayerLifecycleEvents() { |
52 | | - @Override |
53 | | - public void onUnityPlayerUnloaded() { |
54 | | - callback.onUnload(); |
55 | | - } |
56 | | - |
57 | | - @Override |
58 | | - public void onUnityPlayerQuitted() { |
59 | | - callback.onQuit(); |
60 | | - } |
61 | | - }); |
62 | | - |
63 | | - try { |
64 | | - // wait a moment. fix unity cannot start when startup. |
65 | | - Thread.sleep(1000); |
66 | | - } catch (Exception e) { |
67 | | - } |
68 | | - |
69 | | - // start unity |
70 | | - addUnityViewToBackground(); |
71 | | - unityPlayer.windowFocusChanged(true); |
72 | | - unityPlayer.requestFocus(); |
73 | | - unityPlayer.resume(); |
74 | | - |
75 | | - // restore window layout |
76 | | - if (!fullScreen) { |
77 | | - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); |
78 | | - activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
79 | | - } |
80 | | - _isUnityReady = true; |
81 | | - callback.onReady(); |
| 33 | + |
| 34 | + public static void createPlayer(final Activity activity, final UnityPlayerCallback callback) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { |
| 35 | + if (unityPlayer != null) { |
| 36 | + callback.onReady(); |
| 37 | + |
| 38 | + return; |
82 | 39 | } |
83 | | - }); |
84 | | - } |
85 | | - } |
86 | 40 |
|
87 | | - public static void pause() { |
88 | | - if (unityPlayer != null) { |
89 | | - unityPlayer.pause(); |
90 | | - _isUnityPaused = true; |
| 41 | + if (activity != null) { |
| 42 | + activity.runOnUiThread(new Runnable() { |
| 43 | + @Override |
| 44 | + public void run() { |
| 45 | + activity.getWindow().setFormat(PixelFormat.RGBA_8888); |
| 46 | + int flag = activity.getWindow().getAttributes().flags; |
| 47 | + boolean fullScreen = false; |
| 48 | + if ((flag & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) { |
| 49 | + fullScreen = true; |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + unityPlayer = new UPlayer(activity, callback); |
| 54 | + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException e) {} |
| 55 | + |
| 56 | + try { |
| 57 | + // wait a moment. fix unity cannot start when startup. |
| 58 | + Thread.sleep(1000); |
| 59 | + } catch (Exception e) {} |
| 60 | + |
| 61 | + // start unity |
| 62 | + try { |
| 63 | + addUnityViewToBackground(); |
| 64 | + } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {} |
| 65 | + |
| 66 | + unityPlayer.windowFocusChanged(true); |
| 67 | + |
| 68 | + try { |
| 69 | + unityPlayer.requestFocusPlayer(); |
| 70 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {} |
| 71 | + |
| 72 | + unityPlayer.resume(); |
| 73 | + |
| 74 | + if (!fullScreen) { |
| 75 | + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); |
| 76 | + activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 77 | + } |
| 78 | + |
| 79 | + _isUnityReady = true; |
| 80 | + |
| 81 | + try { |
| 82 | + callback.onReady(); |
| 83 | + } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {} |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
91 | 87 | } |
92 | | - } |
93 | 88 |
|
94 | | - public static void resume() { |
95 | | - if (unityPlayer != null) { |
96 | | - unityPlayer.resume(); |
97 | | - _isUnityPaused = false; |
| 89 | + public static void pause() { |
| 90 | + if (unityPlayer != null) { |
| 91 | + unityPlayer.pause(); |
| 92 | + _isUnityPaused = true; |
| 93 | + } |
98 | 94 | } |
99 | | - } |
100 | 95 |
|
101 | | - public static void unload() { |
102 | | - if (unityPlayer != null) { |
103 | | - unityPlayer.unload(); |
104 | | - _isUnityPaused = false; |
| 96 | + public static void resume() { |
| 97 | + if (unityPlayer != null) { |
| 98 | + unityPlayer.resume(); |
| 99 | + _isUnityPaused = false; |
| 100 | + } |
105 | 101 | } |
106 | | - } |
107 | 102 |
|
108 | | - public static void addUnityViewToBackground() { |
109 | | - if (unityPlayer == null) { |
110 | | - return; |
111 | | - } |
112 | | - if (unityPlayer.getParent() != null) { |
113 | | - // NOTE: If we're being detached as part of the transition, make sure |
114 | | - // to explicitly finish the transition first, as it might still keep |
115 | | - // the view's parent around despite calling `removeView()` here. This |
116 | | - // prevents a crash on an `addContentView()` later on. |
117 | | - // Otherwise, if there's no transition, it's a no-op. |
118 | | - // See https://stackoverflow.com/a/58247331 |
119 | | - ((ViewGroup) unityPlayer.getParent()).endViewTransition(unityPlayer); |
120 | | - ((ViewGroup) unityPlayer.getParent()).removeView(unityPlayer); |
121 | | - } |
122 | | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
123 | | - unityPlayer.setZ(-1f); |
| 103 | + public static void unload() { |
| 104 | + if (unityPlayer != null) { |
| 105 | + unityPlayer.unload(); |
| 106 | + _isUnityPaused = false; |
| 107 | + } |
124 | 108 | } |
125 | | - final Activity activity = ((Activity) unityPlayer.getContext()); |
126 | | - ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(1, 1); |
127 | | - activity.addContentView(unityPlayer, layoutParams); |
128 | | - } |
129 | | - |
130 | | - public static void addUnityViewToGroup(ViewGroup group) { |
131 | | - if (unityPlayer == null) { |
132 | | - return; |
| 109 | + |
| 110 | + public static void addUnityViewToBackground() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { |
| 111 | + if (unityPlayer == null) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + if (unityPlayer.getParentPlayer() != null) { |
| 116 | + // NOTE: If we're being detached as part of the transition, make sure |
| 117 | + // to explicitly finish the transition first, as it might still keep |
| 118 | + // the view's parent around despite calling `removeView()` here. This |
| 119 | + // prevents a crash on an `addContentView()` later on. |
| 120 | + // Otherwise, if there's no transition, it's a no-op. |
| 121 | + // See https://stackoverflow.com/a/58247331 |
| 122 | + ((ViewGroup) unityPlayer.getParentPlayer()).endViewTransition(unityPlayer.requestFrame()); |
| 123 | + ((ViewGroup) unityPlayer.getParentPlayer()).removeView(unityPlayer.requestFrame()); |
| 124 | + } |
| 125 | + |
| 126 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 127 | + unityPlayer.setZ(-1f); |
| 128 | + } |
| 129 | + |
| 130 | + final Activity activity = ((Activity) unityPlayer.getContextPlayer()); |
| 131 | + ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(1, 1); |
| 132 | + activity.addContentView(unityPlayer.requestFrame(), layoutParams); |
133 | 133 | } |
134 | | - if (unityPlayer.getParent() != null) { |
135 | | - ((ViewGroup) unityPlayer.getParent()).removeView(unityPlayer); |
| 134 | + |
| 135 | + public static void addUnityViewToGroup(ViewGroup group) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 136 | + if (unityPlayer == null) { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + if (unityPlayer.getParentPlayer() != null) { |
| 141 | + ((ViewGroup) unityPlayer.getParentPlayer()).removeView(unityPlayer.requestFrame()); |
| 142 | + } |
| 143 | + |
| 144 | + ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT); |
| 145 | + group.addView(unityPlayer.requestFrame(), 0, layoutParams); |
| 146 | + unityPlayer.windowFocusChanged(true); |
| 147 | + unityPlayer.requestFocusPlayer(); |
| 148 | + unityPlayer.resume(); |
136 | 149 | } |
137 | | - ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT); |
138 | | - group.addView(unityPlayer, 0, layoutParams); |
139 | | - unityPlayer.windowFocusChanged(true); |
140 | | - unityPlayer.requestFocus(); |
141 | | - unityPlayer.resume(); |
142 | | - } |
143 | 150 |
|
144 | | - public interface UnityPlayerCallback { |
145 | | - void onReady(); |
| 151 | + public interface UnityPlayerCallback { |
| 152 | + void onReady() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException; |
146 | 153 |
|
147 | | - void onUnload(); |
| 154 | + void onUnload(); |
148 | 155 |
|
149 | | - void onQuit(); |
150 | | - } |
| 156 | + void onQuit(); |
| 157 | + } |
151 | 158 | } |
0 commit comments