Skip to content

Commit

Permalink
Merge pull request #29 from teogor/refactor/build-task-unity-native
Browse files Browse the repository at this point in the history
Refactor BuildIl2CppTask to UnityNativeBuildTask and Add Initialization Check
  • Loading branch information
teogor authored Aug 20, 2024
2 parents f426468 + 537bb39 commit 87aa093
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 101 deletions.
91 changes: 68 additions & 23 deletions docs/plugin-usage-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Overview

The Unity Integration Plugin simplifies the process of integrating Unity projects into your
Gradle-based project. It provides two essential tasks: `BuildIl2CppTask`
Gradle-based project. It provides two essential tasks: `UnityNativeBuildTask`
and `RefreshUnityAssetsTask`. This guide explains how to use these tasks effectively.

## Prerequisites
Expand Down Expand Up @@ -31,57 +31,101 @@ To get started, add the Unity Integration Plugin to your `build.gradle` file:
}
```

## Using `BuildIl2CppTask`
## Using `UnityNativeBuildTask`

### Purpose

The `BuildIl2CppTask` automates the compilation and building of Il2Cpp for Unity integration. It
enhances the efficiency of your development workflow by handling complex build processes.
The `UnityNativeBuildTask` compiles and builds native code for Unity integration, managing
architecture-specific binaries and output directories. It enhances the efficiency of your
development workflow by automating the complex build processes required for Unity native code.

### Configuration

To use `BuildIl2CppTask`, follow these steps:
To use `UnityNativeBuildTask`, follow these steps:

1. Configure Unity options in your `build.gradle` file:

=== "Groovy"

```groovy title="build.gradle"
unityOptions {
exportFolder = 'path/to/unity/export/folder'
libraryName = 'YourUnityLibrary'
// Add more configuration options as needed
android {
namespace = "dev.teogor.drifter.demo.module.unity"

unityOptions {
splashMode = 0
splashEnable = true
buildId = "ea574d1a-3365-44b3-9676-33bceabcf351"
notchConfig = "portrait|landscape"
version = "2022.3.7f1"

ndkVersion = "23.1.7779620"
ndkPath = getSafeDrifterUnityPathNdk()

platforms = [
PlatformArch.Arm64,
PlatformArch.Armv7
]
configuration = Configuration.Release
streamingAssets.addAll(unityStreamingAssetsList)

exportedProjectLocation = getSafeDrifterUnityPathExport()
libraryName = 'YourUnityLibrary'
}
}
```

=== "Kotlin"

```kotlin title="build.gradle.kts"
val unityOptions = mapOf(
"exportFolder" to "path/to/unity/export/folder",
"libraryName" to "YourUnityLibrary"
// Add more configuration options as needed
)
val unityStreamingAssets: String? by project
val unityStreamingAssetsList: List<String> = unityStreamingAssets?.split(",") ?: emptyList()

android {
namespace = "dev.teogor.drifter.demo.module.unity"

unityOptions(
androidConfig = this,
) {
splashMode = 0
splashEnable = true
buildId = "ea574d1a-3365-44b3-9676-33bceabcf351"
notchConfig = "portrait|landscape"
version = "2022.3.7f1"

ndkVersion = "23.1.7779620"
ndkPath = getSafeDrifterUnityPathNdk()

platforms = listOf(
PlatformArch.Arm64,
PlatformArch.Armv7
)
configuration = Configuration.Release
streamingAssets += unityStreamingAssetsList

exportedProjectLocation = getSafeDrifterUnityPathExport()
libraryName = "YourUnityLibrary"
}
}
```

2. Create the task and set Unity options:

=== "Groovy"

```groovy title="build.gradle"
createBuildIl2CppTask(unityOptions)
createUnityNativeBuildTask(unityOptions)
```

=== "Kotlin"

```kotlin title="build.gradle.kts"
createBuildIl2CppTask(unityOptions)
createUnityNativeBuildTask(unityOptions)
```

3. Execute the task:

```shell
./gradlew buildIl2Cpp
./gradlew unityNativeBuild
```

Here's a Kotlin code snippet for your `build.gradle` file that demonstrates the configuration and
Expand All @@ -97,11 +141,11 @@ usage:
// Add more configuration options as needed
)

// Create and set up the BuildIl2CppTask
createBuildIl2CppTask(unityOptions)
// Create and set up the UnityNativeBuildTask
createUnityNativeBuildTask(unityOptions)

// Execute the BuildIl2CppTask
tasks.named("buildIl2Cpp").configure {
// Execute the UnityNativeBuildTask
tasks.named("unityNativeBuild").configure {
// Add any additional configuration or dependencies here if needed
// For example:
// dependsOn("someOtherTask")
Expand All @@ -113,7 +157,8 @@ usage:
### Purpose

The `RefreshUnityAssetsTask` streamlines the synchronization and updating of Unity exported assets
with your project. It prepares the project for Unity integration by updating essential folders.
with your project. It prepares the project for Unity integration by updating essential folders and
removing outdated content.

### Configuration

Expand Down Expand Up @@ -167,6 +212,6 @@ any issues or have feature requests, please don't hesitate to contribute or reac
## Quick References

1. [Installation](#installation)
2. [Using `BuildIl2CppTask`](#using-buildil2cpptask)
2. [Using `UnityNativeBuildTask`](#using-unitynativebuildtask)
3. [Using `RefreshUnityAssetsTask`](#using-refreshunityassetstask)
4. [Conclusion](#conclusion)
43 changes: 24 additions & 19 deletions gradle-plugin/api/gradle-plugin.api
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
public class dev/teogor/drifter/plugin/BuildIl2CppTask : org/gradle/api/DefaultTask {
public fun <init> ()V
public final fun buildIl2Cpp ()V
public final fun setUnityOptions (Ldev/teogor/drifter/plugin/models/UnityOptions;)V
}

public final class dev/teogor/drifter/plugin/BuildIl2CppTaskKt {
public static final fun createBuildIl2CppTask (Lorg/gradle/api/Project;Ldev/teogor/drifter/plugin/models/UnityOptions;)V
}

public final class dev/teogor/drifter/plugin/BuildIl2CppTaskKt$inlined$sam$i$org_gradle_api_Action$0 : org/gradle/api/Action {
public fun <init> (Lkotlin/jvm/functions/Function1;)V
public final synthetic fun execute (Ljava/lang/Object;)V
}

public final class dev/teogor/drifter/plugin/ContentCopyException : java/lang/RuntimeException {
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand Down Expand Up @@ -67,12 +52,28 @@ public final class dev/teogor/drifter/plugin/UnityManifestPlaceholdersKt {
public static final fun applyUnityManifestPlaceholders (Lcom/android/build/api/dsl/CommonExtension;Ldev/teogor/drifter/plugin/models/UnityOptions;)V
}

public final class dev/teogor/drifter/plugin/UnityOptionsKt {
public static final fun unityOptions (Lorg/gradle/api/Project;Lcom/android/build/api/dsl/CommonExtension;Lkotlin/jvm/functions/Function1;)V
public class dev/teogor/drifter/plugin/UnityNativeBuildTask : org/gradle/api/DefaultTask {
public static final field Companion Ldev/teogor/drifter/plugin/UnityNativeBuildTask$Companion;
public static final field TASK_NAME Ljava/lang/String;
public fun <init> ()V
public final fun buildUnityNative ()V
public final fun setUnityOptions (Ldev/teogor/drifter/plugin/models/UnityOptions;)V
}

public final class dev/teogor/drifter/plugin/UnityOptionsNotInitializedException : java/lang/RuntimeException {
public fun <init> ()V
public final class dev/teogor/drifter/plugin/UnityNativeBuildTask$Companion {
}

public final class dev/teogor/drifter/plugin/UnityNativeBuildTaskKt {
public static final fun createUnityNativeBuildTask (Lorg/gradle/api/Project;Ldev/teogor/drifter/plugin/models/UnityOptions;)V
}

public final class dev/teogor/drifter/plugin/UnityNativeBuildTaskKt$inlined$sam$i$org_gradle_api_Action$0 : org/gradle/api/Action {
public fun <init> (Lkotlin/jvm/functions/Function1;)V
public final synthetic fun execute (Ljava/lang/Object;)V
}

public final class dev/teogor/drifter/plugin/UnityOptionsKt {
public static final fun unityOptions (Lorg/gradle/api/Project;Lcom/android/build/api/dsl/CommonExtension;Lkotlin/jvm/functions/Function1;)V
}

public final class dev/teogor/drifter/plugin/models/Configuration : java/lang/Enum {
Expand Down Expand Up @@ -154,3 +155,7 @@ public final class dev/teogor/drifter/plugin/models/UnityOptions {
public fun toString ()Ljava/lang/String;
}

public final class dev/teogor/drifter/plugin/utils/error/UnityOptionsNotInitializedException : java/lang/RuntimeException {
public fun <init> ()V
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package dev.teogor.drifter.plugin

import dev.teogor.drifter.plugin.models.UnityOptions
import dev.teogor.drifter.plugin.utils.error.UnityOptionsNotInitializedException
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.tasks.TaskAction
Expand Down Expand Up @@ -179,19 +180,6 @@ open class RefreshUnityAssetsTask : DefaultTask() {
}
}

/**
* Exception thrown when attempting to use the task before initializing UnityOptions.
*
* @see [RefreshUnityAssetsTask.checkInitialized]
*/
class UnityOptionsNotInitializedException : RuntimeException(
"""
|UnityOptions is not initialized. Please call setUnityOptions() before using this task.
|
|To report this problem, file an issue on GitHub: https://github.com/teogor/drifter/issues
""".trimMargin(),
)

/**
* Exception thrown when folder deletion fails during the task execution.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun Project.unityBuildTask(
commonExtension: CommonExtension<*, *, *, *, *, *>,
unityOptions: UnityOptions,
) {
val buildIl2CppTask = project.tasks.getByName("buildIl2Cpp")
val unityNativeBuildTask = project.tasks.getByName(UnityNativeBuildTask.TASK_NAME)

if (unityOptions.ndkPath.isEmpty()) {
unityOptions.ndkPath = drifterUnityPathNdk ?: ""
Expand Down Expand Up @@ -59,11 +59,11 @@ fun Project.unityBuildTask(
afterEvaluate {
if (project(path).tasks.findByName("mergeDebugJniLibFolders") != null) {
project(path).tasks.named("mergeDebugJniLibFolders").get()
.dependsOn(buildIl2CppTask)
.dependsOn(unityNativeBuildTask)
}
if (project(path).tasks.findByName("mergeReleaseJniLibFolders") != null) {
project(path).tasks.named("mergeReleaseJniLibFolders").get()
.dependsOn(buildIl2CppTask)
.dependsOn(unityNativeBuildTask)
}
}

Expand Down
Loading

0 comments on commit 87aa093

Please sign in to comment.