-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from teogor/feature/unity-engine-composable
Upgrade Unity Integration with Modern Composables and Documentation
- Loading branch information
Showing
10 changed files
with
261 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Integrating Unity Engine in your Jetpack Compose UI | ||
|
||
This document covers two key composable functions, `UnityEngineView` and `UnityEngineScaffold`, that | ||
enable you to display the Unity Engine instance within your Jetpack Compose application. | ||
|
||
## `UnityEngineView` - Displaying the Unity Engine | ||
|
||
This function provides a basic way to integrate the Unity Engine into your UI: | ||
|
||
**Parameters** | ||
|
||
* `modifier`: Modifier to apply to the Unity Engine view for styling and positioning. | ||
* `onUnityEngineCreated`: An optional callback invoked when the Unity Engine is created and ready. | ||
|
||
**Example Usage** | ||
|
||
```kotlin | ||
Box(modifier = Modifier.fillMaxSize()) { | ||
UnityEngineView( | ||
modifier = Modifier.size(300.dp), | ||
onUnityEngineCreated = { /* Optional callback */ } | ||
) | ||
} | ||
``` | ||
|
||
This will place a Unity Engine view with a size of 300dp within a `Box` that fills the entire | ||
screen. You can customize the size and position using the `modifier` parameter. | ||
|
||
## `UnityEngineScaffold` - Layout with Unity Engine and Content | ||
|
||
This function offers a more structured approach for integrating the Unity Engine alongside other UI | ||
elements: | ||
|
||
**Parameters** | ||
|
||
* `modifier`: Modifier for the overall layout. | ||
* `unityEngineModifier`: Modifier specific to the Unity Engine view. | ||
* `onUnityEngineCreated`: Optional callback for Unity Engine creation. | ||
* `content`: The composable content to display alongside the Unity Engine. | ||
|
||
**Example Usage** | ||
|
||
```kotlin | ||
Column(modifier = Modifier.fillMaxSize()) { | ||
UnityEngineScaffold( | ||
unityEngineModifier = Modifier.size(200.dp), | ||
content = { | ||
Text("Additional UI elements here") | ||
Button(onClick = {}) { | ||
Text("Interact with the Unity Engine") | ||
} | ||
} | ||
) | ||
} | ||
``` | ||
|
||
This example creates a column layout with the Unity Engine occupying the top portion (200dp) and | ||
additional UI elements (text and button) below. | ||
|
||
### Key Points | ||
|
||
* Both functions handle the lifecycle management of the Unity Engine instance. | ||
* Use `onUnityEngineCreated` to perform actions when the Unity Engine is ready, such as loading | ||
specific content or sending messages. | ||
* Consider using `UnityEngineScaffold` when you need to integrate the Unity Engine with other UI | ||
elements in a structured layout. | ||
* Remember to import the necessary dependencies and configure your Unity project for proper | ||
integration with Jetpack Compose. | ||
|
||
This documentation provides a basic overview of these functions. For further details, refer to the | ||
source code and explore the available documentation within your project. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
drifter-compose/src/main/kotlin/dev/teogor/drifter/compose/UnityEngineView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright 2023 teogor (Teodor Grigor) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.teogor.drifter.compose | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.BoxScope | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import androidx.lifecycle.Lifecycle | ||
import dev.teogor.ceres.core.common.utils.OnLifecycleEvent | ||
import dev.teogor.drifter.compose.components.UnityPlayerView | ||
|
||
@Deprecated( | ||
"Use UnityEngineView directly instead. This function will be removed in a future version.", | ||
ReplaceWith("UnityEngineView(modifier = modifier, onUnityEngineCreated = onCreated)"), | ||
) | ||
@Composable | ||
fun UvpComposable( | ||
modifier: Modifier = Modifier, | ||
onCreated: () -> Unit, | ||
) { | ||
UnityEngineView(modifier, onCreated) | ||
} | ||
|
||
/** | ||
* Composable function that displays the Unity Engine instance within your Compose UI. | ||
* | ||
* @param modifier Modifier to apply to the Unity Engine view. | ||
* @param onUnityEngineCreated Callback invoked when the Unity Engine is created and ready. | ||
*/ | ||
@Composable | ||
fun UnityEngineView( | ||
modifier: Modifier = Modifier, | ||
onUnityEngineCreated: () -> Unit, | ||
) { | ||
var unityEngineView by remember { | ||
mutableStateOf<UnityPlayerView?>(null) | ||
} | ||
|
||
OnLifecycleEvent { _, event -> | ||
when (event) { | ||
Lifecycle.Event.ON_START -> { | ||
unityEngineView?.onStart() | ||
} | ||
|
||
Lifecycle.Event.ON_RESUME -> { | ||
unityEngineView?.onResume() | ||
} | ||
|
||
Lifecycle.Event.ON_PAUSE -> { | ||
unityEngineView?.onPause() | ||
} | ||
|
||
Lifecycle.Event.ON_STOP -> { | ||
unityEngineView?.onStop() | ||
} | ||
|
||
Lifecycle.Event.ON_DESTROY -> { | ||
unityEngineView?.onDestroy() | ||
} | ||
|
||
else -> {} | ||
} | ||
} | ||
|
||
AndroidView( | ||
modifier = modifier, | ||
factory = { context -> | ||
UnityPlayerView(context).apply { | ||
loadPlayer(onUnityEngineCreated = onUnityEngineCreated) | ||
unityEngineView = this | ||
} | ||
}, | ||
) | ||
} | ||
|
||
/** | ||
* Composable function for creating a layout with a Unity Engine view and additional content. | ||
* | ||
* @param modifier Modifier to apply to the overall layout. | ||
* @param unityEngineModifier Modifier to apply specifically to the Unity Engine view. | ||
* @param onUnityEngineCreated Optional callback invoked when the Unity Engine is created | ||
* and ready. | ||
* @param content The composable content to display alongside the Unity Engine. | ||
*/ | ||
@Composable | ||
fun UnityEngineScaffold( | ||
modifier: Modifier = Modifier, | ||
unityEngineModifier: Modifier = Modifier, | ||
onUnityEngineCreated: () -> Unit = {}, | ||
content: @Composable BoxScope.() -> Unit, | ||
) { | ||
Box(modifier) { | ||
UnityEngineView( | ||
modifier = unityEngineModifier, | ||
onUnityEngineCreated = onUnityEngineCreated, | ||
) | ||
|
||
content() | ||
} | ||
} |
Oops, something went wrong.