@dolami-inc/react-native-expo-unity 0.4.1 → 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,23 +52,43 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
44
52
  get() = unityPlayer != null
45
53
 
46
54
  /**
47
- * Returns the UnityPlayer view for embedding.
48
- * Unity 6+ made UnityPlayer abstract; use getView() to get the renderable view.
55
+ * Returns the FrameLayout container that holds Unity's rendering surface.
49
56
  */
50
57
  val unityPlayerView: View?
51
- get() = unityPlayer?.view
58
+ get() = containerView
52
59
 
53
60
  fun initialize(activity: Activity) {
54
61
  if (isInitialized) return
55
62
 
56
63
  val runInit = Runnable {
57
64
  try {
58
- 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)
59
82
  unityPlayer = player
83
+ containerView = frameLayout
84
+
85
+ // Start Unity rendering
86
+ player.resume()
87
+ player.windowFocusChanged(true)
60
88
 
61
89
  NativeCallProxy.registerListener(this)
62
90
 
63
- Log.i(TAG, "Unity initialized")
91
+ Log.i(TAG, "Unity initialized (GameActivity mode)")
64
92
  } catch (e: Exception) {
65
93
  Log.e(TAG, "Failed to initialize Unity", e)
66
94
  }
@@ -113,11 +141,13 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
113
141
  override fun onUnityPlayerUnloaded() {
114
142
  Log.i(TAG, "onUnityPlayerUnloaded")
115
143
  unityPlayer = null
144
+ containerView = null
116
145
  }
117
146
 
118
147
  override fun onUnityPlayerQuitted() {
119
148
  Log.i(TAG, "onUnityPlayerQuitted")
120
149
  unityPlayer = null
150
+ containerView = null
121
151
  }
122
152
 
123
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.1",
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",