@dolami-inc/react-native-expo-unity 0.4.4 → 0.4.5
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.
|
@@ -18,9 +18,6 @@ class ExpoUnityView(context: Context, appContext: AppContext) : ExpoView(context
|
|
|
18
18
|
var autoUnloadOnUnmount: Boolean = true
|
|
19
19
|
|
|
20
20
|
init {
|
|
21
|
-
// post {} dispatches to main thread. setupUnity -> initialize runs
|
|
22
|
-
// synchronously when already on main thread, so mountUnityView sees the
|
|
23
|
-
// fully-initialized player without a race condition.
|
|
24
21
|
post { setupUnity() }
|
|
25
22
|
}
|
|
26
23
|
|
|
@@ -61,6 +58,18 @@ class ExpoUnityView(context: Context, appContext: AppContext) : ExpoView(context
|
|
|
61
58
|
FrameLayout.LayoutParams.MATCH_PARENT
|
|
62
59
|
)
|
|
63
60
|
)
|
|
61
|
+
|
|
62
|
+
Log.i(TAG, "Unity view mounted")
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
override fun onAttachedToWindow() {
|
|
66
|
+
super.onAttachedToWindow()
|
|
67
|
+
// Start rendering after the view is in the window hierarchy,
|
|
68
|
+
// so Unity's surface is properly connected to the display.
|
|
69
|
+
val bridge = UnityBridge.getInstance()
|
|
70
|
+
if (bridge.isInitialized) {
|
|
71
|
+
bridge.startRendering()
|
|
72
|
+
}
|
|
64
73
|
}
|
|
65
74
|
|
|
66
75
|
override fun onDetachedFromWindow() {
|
|
@@ -4,20 +4,18 @@ 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
|
|
8
7
|
import android.view.View
|
|
9
|
-
import android.widget.FrameLayout
|
|
10
8
|
import com.expounity.bridge.NativeCallProxy
|
|
11
9
|
import com.unity3d.player.IUnityPlayerLifecycleEvents
|
|
12
10
|
import com.unity3d.player.UnityPlayer
|
|
13
|
-
import com.unity3d.player.
|
|
11
|
+
import com.unity3d.player.UnityPlayerForActivityOrService
|
|
14
12
|
|
|
15
13
|
/**
|
|
16
14
|
* Singleton managing the UnityPlayer lifecycle.
|
|
17
15
|
* Android equivalent of ios/UnityBridge.mm.
|
|
18
16
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
17
|
+
* Uses UnityPlayerForActivityOrService which manages its own rendering
|
|
18
|
+
* surface internally — suitable for UaaL (Unity as a Library) embedding.
|
|
21
19
|
*/
|
|
22
20
|
class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCallProxy.MessageListener {
|
|
23
21
|
|
|
@@ -40,9 +38,6 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
|
|
|
40
38
|
var unityPlayer: UnityPlayer? = null
|
|
41
39
|
private set
|
|
42
40
|
|
|
43
|
-
/** The FrameLayout container that Unity renders into. */
|
|
44
|
-
private var containerView: FrameLayout? = null
|
|
45
|
-
|
|
46
41
|
var onMessage: ((String) -> Unit)? = null
|
|
47
42
|
|
|
48
43
|
/** Tracked here (not on the Module) so it survives module recreation. */
|
|
@@ -52,48 +47,23 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
|
|
|
52
47
|
get() = unityPlayer != null
|
|
53
48
|
|
|
54
49
|
/**
|
|
55
|
-
* Returns the
|
|
50
|
+
* Returns the UnityPlayer's FrameLayout for embedding.
|
|
51
|
+
* UnityPlayerForActivityOrService creates its own rendering surface internally.
|
|
56
52
|
*/
|
|
57
53
|
val unityPlayerView: View?
|
|
58
|
-
get() =
|
|
54
|
+
get() = unityPlayer?.frameLayout
|
|
59
55
|
|
|
60
56
|
fun initialize(activity: Activity) {
|
|
61
57
|
if (isInitialized) return
|
|
62
58
|
|
|
63
59
|
val runInit = Runnable {
|
|
64
60
|
try {
|
|
65
|
-
|
|
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)
|
|
61
|
+
val player = UnityPlayerForActivityOrService(activity, this)
|
|
87
62
|
unityPlayer = player
|
|
88
|
-
containerView = frameLayout
|
|
89
|
-
|
|
90
|
-
// Start Unity rendering
|
|
91
|
-
player.resume()
|
|
92
|
-
player.windowFocusChanged(true)
|
|
93
63
|
|
|
94
64
|
NativeCallProxy.registerListener(this)
|
|
95
65
|
|
|
96
|
-
Log.i(TAG, "Unity initialized
|
|
66
|
+
Log.i(TAG, "Unity initialized")
|
|
97
67
|
} catch (e: Exception) {
|
|
98
68
|
Log.e(TAG, "Failed to initialize Unity", e)
|
|
99
69
|
}
|
|
@@ -106,6 +76,17 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
|
|
|
106
76
|
}
|
|
107
77
|
}
|
|
108
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Must be called after the Unity view is attached to the window hierarchy.
|
|
81
|
+
* Triggers Unity's rendering pipeline.
|
|
82
|
+
*/
|
|
83
|
+
fun startRendering() {
|
|
84
|
+
val player = unityPlayer ?: return
|
|
85
|
+
player.resume()
|
|
86
|
+
player.windowFocusChanged(true)
|
|
87
|
+
Log.i(TAG, "Rendering started")
|
|
88
|
+
}
|
|
89
|
+
|
|
109
90
|
fun sendMessage(gameObject: String, methodName: String, message: String) {
|
|
110
91
|
if (!isInitialized) return
|
|
111
92
|
UnityPlayer.UnitySendMessage(gameObject, methodName, message)
|
|
@@ -146,13 +127,11 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
|
|
|
146
127
|
override fun onUnityPlayerUnloaded() {
|
|
147
128
|
Log.i(TAG, "onUnityPlayerUnloaded")
|
|
148
129
|
unityPlayer = null
|
|
149
|
-
containerView = null
|
|
150
130
|
}
|
|
151
131
|
|
|
152
132
|
override fun onUnityPlayerQuitted() {
|
|
153
133
|
Log.i(TAG, "onUnityPlayerQuitted")
|
|
154
134
|
unityPlayer = null
|
|
155
|
-
containerView = null
|
|
156
135
|
}
|
|
157
136
|
|
|
158
137
|
// NativeCallProxy.MessageListener (Unity -> RN)
|