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

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,43 @@ 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
+ // Create the container and surface that Unity will render into
66
+ val frameLayout = FrameLayout(activity)
67
+ frameLayout.layoutParams = FrameLayout.LayoutParams(
68
+ FrameLayout.LayoutParams.MATCH_PARENT,
69
+ FrameLayout.LayoutParams.MATCH_PARENT
70
+ )
71
+
72
+ val surfaceView = SurfaceView(activity)
73
+ frameLayout.addView(
74
+ surfaceView,
75
+ FrameLayout.LayoutParams(
76
+ FrameLayout.LayoutParams.MATCH_PARENT,
77
+ FrameLayout.LayoutParams.MATCH_PARENT
78
+ )
79
+ )
80
+
81
+ val player = UnityPlayerForGameActivity(activity, frameLayout, surfaceView, this)
60
82
  unityPlayer = player
83
+ containerView = frameLayout
61
84
 
62
- // Start Unity rendering — without these calls the player
63
- // stays in a paused/unfocused state and shows a blank screen.
85
+ // Start Unity rendering
64
86
  player.resume()
65
87
  player.windowFocusChanged(true)
66
88
 
67
89
  NativeCallProxy.registerListener(this)
68
90
 
69
- Log.i(TAG, "Unity initialized")
91
+ Log.i(TAG, "Unity initialized (GameActivity mode)")
70
92
  } catch (e: Exception) {
71
93
  Log.e(TAG, "Failed to initialize Unity", e)
72
94
  }
@@ -119,11 +141,13 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
119
141
  override fun onUnityPlayerUnloaded() {
120
142
  Log.i(TAG, "onUnityPlayerUnloaded")
121
143
  unityPlayer = null
144
+ containerView = null
122
145
  }
123
146
 
124
147
  override fun onUnityPlayerQuitted() {
125
148
  Log.i(TAG, "onUnityPlayerQuitted")
126
149
  unityPlayer = null
150
+ containerView = null
127
151
  }
128
152
 
129
153
  // 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.3",
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",