Skip to content

Commit

Permalink
Merge pull request #21 from teogor/feature/unity-engine-composable
Browse files Browse the repository at this point in the history
Upgrade Unity Integration with Modern Composables and Documentation
  • Loading branch information
teogor authored Feb 24, 2024
2 parents b5d3fc8 + ba9cdb4 commit d2ea31a
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 103 deletions.
8 changes: 4 additions & 4 deletions app/src/main/kotlin/dev/teogor/drifter/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import dev.teogor.ceres.core.common.utils.OnLifecycleEvent
import dev.teogor.drifter.compose.UvpComposable
import dev.teogor.drifter.compose.UnityEngineView
import dev.teogor.drifter.demo.ui.theme.UnityViewTheme
import dev.teogor.drifter.wallpaper.LiveWallpaperUtility

Expand Down Expand Up @@ -85,21 +85,21 @@ class MainActivity : ComponentActivity() {
val controller = AquariumMessageSender()
val storage = AquariumStorage()

UvpComposable(
UnityEngineView(
modifier = Modifier
.fillMaxWidth(.5f)
.fillMaxHeight(.5f)
.backgroundAndClip(
color = Color.Green,
shape = RoundedCornerShape(20.dp),
),
onCreated = {
onUnityEngineCreated = {
controller.apply {
setEditorMode(true)
animateToWaterColor(storage.waterColor, false)
cycleOption(storage.cycleOption)
}
},
}
)

UnityColorPicker(
Expand Down
69 changes: 52 additions & 17 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Drifter Repository
Drifter simplifies the integration between Unity and Android, enhancing performance seamlessly and effortlessly.

Drifter simplifies the integration between Unity and Android, enhancing performance seamlessly and
effortlessly.

## Download

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Maven Central](https://img.shields.io/maven-central/v/dev.teogor.drifter/bom.svg?label=Maven%20Central)](https://central.sonatype.com/search?q=g%3Adev.teogor.drifter+a%3Abom&smo=true)
[![API](https://img.shields.io/badge/API-24%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=24)
Expand All @@ -23,13 +26,17 @@ Drifter simplifies the integration between Unity and Android, enhancing performa
### Gradle

Add the dependency below to your **module**'s `build.*` file.

- for `build.gradle`

```gradle
dependencies {
implementation platform('dev.teogor.drifter:bom:1.0.0-alpha01')
}
```

- for `build.gradle.kts`

```kotlin
dependencies {
implementation(platform("dev.teogor.drifter:bom:1.0.0-alpha01"))
Expand All @@ -38,35 +45,65 @@ dependencies {

> **Note**: This library has more modules therefore include only the ones that you want to use.
## Simplifying Unity Engine Integration with Jetpack Compose

**Empowering seamless Unity experiences in your Android apps**

Drifter now offers powerful tools to integrate the Unity Engine directly into your Jetpack Compose
UI. Unlock a world of possibilities with the `drifter-compose` module, designed to simplify and
enhance your Compose development journey.

**Effortlessly Integrate Unity Engine:**

* Embed Unity content directly within your Jetpack Compose layouts using intuitive APIs.
* Enjoy smooth and responsive performance thanks to optimized rendering and resource management.
* Leverage pre-built components like `UnityEngineView` and `UnityEngineScaffold` to accelerate your
development.
* Customize and adapt the integration to your specific needs and preferences.

**Explore the Documentation:**

Discover a comprehensive guide for `drifter-compose`
at [link to unity-engine-jetpack-compose](unity-engine-jetpack-compose.md).
Learn step-by-step instructions, explore code examples, and gain best practices to maximize the
potential of Unity Engine in your Compose projects.
n in a markdown code snippet format.

## Modules

### drifter-compose

The `drifter-compose` module provides functionality for working with Jetpack Compose in Android applications. It includes utilities and components to simplify Compose-based UI development.
The `drifter-compose` module provides functionality for working with Jetpack Compose in Android
applications. It includes utilities and components to simplify Compose-based UI development.

[Explore the source code](/drifter-compose)

### drifter-integration

The `drifter-integration` module offers integration points and connectors for third-party libraries and services commonly used in Android development. It allows you to seamlessly integrate your Android app with various services.
The `drifter-integration` module offers integration points and connectors for third-party libraries
and services commonly used in Android development. It allows you to seamlessly integrate your
Android app with various services.

[Explore the source code](/drifter-integration)

### drifter-core

The `drifter-core` module contains core functionality and utilities that are commonly used across Android projects. It provides a foundation for building robust Android applications.
The `drifter-core` module contains core functionality and utilities that are commonly used across
Android projects. It provides a foundation for building robust Android applications.

[Explore the source code](/drifter-core)

### drifter-wallpaper

The `drifter-wallpaper` module focuses on wallpaper-related features and tools for Android. It simplifies the development of live wallpapers and wallpaper-related functionalities.
The `drifter-wallpaper` module focuses on wallpaper-related features and tools for Android. It
simplifies the development of live wallpapers and wallpaper-related functionalities.

[Explore the source code](/drifter-wallpaper)

### drifter-plugin

The `drifter-plugin` module provides a Gradle plugin that can be used to enhance your Android project build process. It offers features such as code generation, resource management, and more.
The `drifter-plugin` module provides a Gradle plugin that can be used to enhance your Android
project build process. It offers features such as code generation, resource management, and more.

This Gradle plugin simplifies Unity integration for your project by providing two essential tasks:

Expand All @@ -76,23 +113,21 @@ This Gradle plugin simplifies Unity integration for your project by providing tw
[Explore the source code](/drifter-plugin)

## Find this repository useful? :heart:
Show your appreciation by starring this project :star: and joining our community of __[stargazers](https://github.com/teogor/drifter/stargazers)__.

Want to stay updated on my latest projects and contributions? Be sure to __[follow me](https://github.com/teogor)__ on GitHub! 🤩
Show your appreciation by starring this project :star: and joining our community of _
_[stargazers](https://github.com/teogor/drifter/stargazers)__.

Want to stay updated on my latest projects and contributions? Be sure to _
_[follow me](https://github.com/teogor)__ on GitHub! 🤩

# License

```xml
Designed and developed by 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
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

http://www.apache.org/licenses/LICENSE-2.0
http://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.
Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.
```
71 changes: 71 additions & 0 deletions docs/unity-engine-jetpack-compose.md
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.
8 changes: 3 additions & 5 deletions drifter-compose/api/drifter-compose.api
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
public final class dev/teogor/drifter/compose/OnLifecycleEventKt {
public static final fun OnLifecycleEvent (Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;I)V
}

public final class dev/teogor/drifter/compose/UvpComposableKt {
public final class dev/teogor/drifter/compose/UnityEngineViewKt {
public static final fun UnityEngineScaffold (Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
public static final fun UnityEngineView (Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function0;Landroidx/compose/runtime/Composer;II)V
public static final fun UvpComposable (Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function0;Landroidx/compose/runtime/Composer;II)V
}

Expand Down
1 change: 1 addition & 0 deletions drifter-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies {
implementation(libs.ui)
implementation(libs.ui.graphics)
implementation(libs.ui.tooling.preview)
implementation(libs.androidx.foundation.layout.android)

implementation(libs.lifecycle.runtime.ktx)
}
Expand Down
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()
}
}
Loading

0 comments on commit d2ea31a

Please sign in to comment.