@dolami-inc/react-native-expo-unity 0.4.3 → 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,43 +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
|
-
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)
|
|
61
|
+
val player = UnityPlayerForActivityOrService(activity, this)
|
|
82
62
|
unityPlayer = player
|
|
83
|
-
containerView = frameLayout
|
|
84
|
-
|
|
85
|
-
// Start Unity rendering
|
|
86
|
-
player.resume()
|
|
87
|
-
player.windowFocusChanged(true)
|
|
88
63
|
|
|
89
64
|
NativeCallProxy.registerListener(this)
|
|
90
65
|
|
|
91
|
-
Log.i(TAG, "Unity initialized
|
|
66
|
+
Log.i(TAG, "Unity initialized")
|
|
92
67
|
} catch (e: Exception) {
|
|
93
68
|
Log.e(TAG, "Failed to initialize Unity", e)
|
|
94
69
|
}
|
|
@@ -101,6 +76,17 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
|
|
|
101
76
|
}
|
|
102
77
|
}
|
|
103
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
|
+
|
|
104
90
|
fun sendMessage(gameObject: String, methodName: String, message: String) {
|
|
105
91
|
if (!isInitialized) return
|
|
106
92
|
UnityPlayer.UnitySendMessage(gameObject, methodName, message)
|
|
@@ -141,13 +127,11 @@ class UnityBridge private constructor() : IUnityPlayerLifecycleEvents, NativeCal
|
|
|
141
127
|
override fun onUnityPlayerUnloaded() {
|
|
142
128
|
Log.i(TAG, "onUnityPlayerUnloaded")
|
|
143
129
|
unityPlayer = null
|
|
144
|
-
containerView = null
|
|
145
130
|
}
|
|
146
131
|
|
|
147
132
|
override fun onUnityPlayerQuitted() {
|
|
148
133
|
Log.i(TAG, "onUnityPlayerQuitted")
|
|
149
134
|
unityPlayer = null
|
|
150
|
-
containerView = null
|
|
151
135
|
}
|
|
152
136
|
|
|
153
137
|
// NativeCallProxy.MessageListener (Unity -> RN)
|