@dolami-inc/react-native-expo-unity 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,15 +4,20 @@ import android.app.Activity
4
4
  import android.os.Handler
5
5
  import android.os.Looper
6
6
  import android.util.Log
7
+ import android.view.SurfaceView
7
8
  import android.view.View
9
+ import android.widget.FrameLayout
8
10
  import com.expounity.bridge.NativeCallProxy
9
11
  import com.unity3d.player.IUnityPlayerLifecycleEvents
10
12
  import com.unity3d.player.UnityPlayer
11
- import com.unity3d.player.UnityPlayerForActivityOrService
13
+ import com.unity3d.player.UnityPlayerForGameActivity
12
14
 
13
15
  /**
14
16
  * Singleton managing the UnityPlayer lifecycle.
15
17
  * Android equivalent of ios/UnityBridge.mm.
18
+ *
19
+ * Unity 6+ uses GameActivity mode. UnityPlayerForGameActivity requires
20
+ * a FrameLayout container and SurfaceView that it renders into.
16
21
  */
17
22
  class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCallProxy.MessageListener {
18
23
 
@@ -35,6 +40,9 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
35
40
  var unityPlayer: UnityPlayer? = null
36
41
  private set
37
42
 
43
+ /** The FrameLayout container that Unity renders into. */
44
+ private var containerView: FrameLayout? = null
45
+
38
46
  var onMessage: ((String) -> Unit)? = null
39
47
 
40
48
  /** Tracked here (not on the Module) so it survives module recreation. */
@@ -44,29 +52,48 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
44
52
  get() = unityPlayer != null
45
53
 
46
54
  /**
47
- * Returns the UnityPlayer's FrameLayout container for embedding.
48
- * Unity 6+ made UnityPlayer abstract; getFrameLayout() returns the full
49
- * container with the rendering surface inside it.
55
+ * Returns the FrameLayout container that holds Unity's rendering surface.
50
56
  */
51
57
  val unityPlayerView: View?
52
- get() = unityPlayer?.frameLayout
58
+ get() = containerView
53
59
 
54
60
  fun initialize(activity: Activity) {
55
61
  if (isInitialized) return
56
62
 
57
63
  val runInit = Runnable {
58
64
  try {
59
- val player = UnityPlayerForActivityOrService(activity, this)
65
+ // Load native libraries required by Unity's GameActivity mode.
66
+ // Unity's own launcher does this via android.app.lib_name metadata,
67
+ // but since we bypass GameActivity lifecycle we load manually.
68
+ System.loadLibrary("game")
69
+
70
+ // Create the container and surface that Unity will render into
71
+ val frameLayout = FrameLayout(activity)
72
+ frameLayout.layoutParams = FrameLayout.LayoutParams(
73
+ FrameLayout.LayoutParams.MATCH_PARENT,
74
+ FrameLayout.LayoutParams.MATCH_PARENT
75
+ )
76
+
77
+ val surfaceView = SurfaceView(activity)
78
+ frameLayout.addView(
79
+ surfaceView,
80
+ FrameLayout.LayoutParams(
81
+ FrameLayout.LayoutParams.MATCH_PARENT,
82
+ FrameLayout.LayoutParams.MATCH_PARENT
83
+ )
84
+ )
85
+
86
+ val player = UnityPlayerForGameActivity(activity, frameLayout, surfaceView, this)
60
87
  unityPlayer = player
88
+ containerView = frameLayout
61
89
 
62
- // Start Unity rendering — without these calls the player
63
- // stays in a paused/unfocused state and shows a blank screen.
90
+ // Start Unity rendering
64
91
  player.resume()
65
92
  player.windowFocusChanged(true)
66
93
 
67
94
  NativeCallProxy.registerListener(this)
68
95
 
69
- Log.i(TAG, "Unity initialized")
96
+ Log.i(TAG, "Unity initialized (GameActivity mode)")
70
97
  } catch (e: Exception) {
71
98
  Log.e(TAG, "Failed to initialize Unity", e)
72
99
  }
@@ -119,11 +146,13 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
119
146
  override fun onUnityPlayerUnloaded() {
120
147
  Log.i(TAG, "onUnityPlayerUnloaded")
121
148
  unityPlayer = null
149
+ containerView = null
122
150
  }
123
151
 
124
152
  override fun onUnityPlayerQuitted() {
125
153
  Log.i(TAG, "onUnityPlayerQuitted")
126
154
  unityPlayer = null
155
+ containerView = null
127
156
  }
128
157
 
129
158
  // NativeCallProxy.MessageListener (Unity -> RN)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dolami-inc/react-native-expo-unity",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "Unity as a Library (UaaL) bridge for React Native / Expo",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",