Skip to content

Commit

Permalink
Add Assetbundle generation support (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
drewcassidy authored Sep 20, 2024
1 parent fb62004 commit 2701fb8
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 11 deletions.
51 changes: 51 additions & 0 deletions .github/actions/build-assetbundles/AssetBundleBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;

namespace KSPBuildTools {
public class AssetBundleBuilder {
public static void BuildBundles() {
string[] args = Environment.GetCommandLineArgs();
bool isProject = true;
string assetbundleName = null;
string path = "";
for (int i = 0; i < args.Length; i++) {
if (args[i] == "-assetbundlePath") {
path = args[i + 1];
i++;
}

if (args[i] == "-assetbundleName") {
isProject = false;
assetbundleName = args[i + 1];
i++;
}
}

Debug.Log($"Building assetbundle for {path}");

PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.StandaloneWindows64, false);
PlayerSettings.SetGraphicsAPIs(BuildTarget.StandaloneWindows64, new GraphicsDeviceType[] {
GraphicsDeviceType.OpenGLCore, GraphicsDeviceType.Direct3D11
});

if (isProject) {
BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.ChunkBasedCompression,
BuildTarget.StandaloneWindows64);
}
else {
var bundleDefinitions = new AssetBundleBuild[] {
new AssetBundleBuild {
assetBundleName = assetbundleName,
assetNames = new string[] { "Assets/Bundle" }
}
};
BuildPipeline.BuildAssetBundles(path, bundleDefinitions, BuildAssetBundleOptions.ChunkBasedCompression,
BuildTarget.StandaloneWindows64);
}

Debug.Log("Done!");
}
}
}
64 changes: 64 additions & 0 deletions .github/actions/build-assetbundles/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
`build-assetbundles` Action
===========================

Builds one or more assetbundles. Wraps the [GameCI Unity Builder](https://github.com/marketplace/actions/unity-builder) action in order to call the Unity editor. Assetbundles are cached to speed up your workflow if none of the assets change from build to build.

## Usage

### Activation
The Unity editor requires a login to function, even with the free edition. You will have to make some repository secrets in order to allow Unity to function. Follow the instructions in the [GameCI documentation](https://game.ci/docs/github/activation) to set these up.

### Using a Unity project
If your repo has a complete Unity project already checked in, you can point the action at it and have it build all your defined asset bundles.

```yaml
- uses: github.com/KSPModdingLibs/KSPBuildTools/.github/actions/build-assetbundles@main
with:
project-path: 'MyUnityProject'
env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
```
### Using a list of assets
Alternatively, you can give the action an assetbundle name and a list of assets to compile, and it will create a temporary project in order to compile them
```yaml
- uses: github.com/KSPModdingLibs/KSPBuildTools/.github/actions/build-assetbundles@main
with:
assetbundle-name: mybundle.shab
asset-files: 'MyAssets/*.shader MyAssets/*.cginc'
env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
```
### Build Method
By default, this action will use the [`KSPBuildTools.KSPBuildTools.AssetBundleBuilder.BuildBundles` build method](AssetBundleBuilder.cs). This function calls on the Unity build pipeline to build your assetbundles. If you need, you can provide your own custom build function and call it with the [`build-method`](#build-method-1) input.

## Inputs

### `unity-version`

Which Unity version to use. Defaults to 2019.4.18f1, which supports KSP 1.12

### `project-dir`

Path to an existing Unity project (the folder that contains "Assets"). Must be relative to `github.workspace` (usually your repository root). Mutually exclusive with `assetbundle-name` and `asset-files`.

### `asset-files`

Glob of assets to bundle. Mutually exclusive with `project-dir`

### `assetbundle-name`

Name of the assetbundle to generate from `asset-files`. Mutually exclusive with `project-dir`

### `output-dir`

Directory in which to place generated assetbundle(s).

### `build-method`

`[Namespace].Class.Function` to call in order to build your assetbundle. Defaults to `KSPBuildTools.KSPBuildTools.AssetBundleBuilder.BuildBundles`
102 changes: 102 additions & 0 deletions .github/actions/build-assetbundles/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
name: Build Assetbundle
description: Build an assetbundle

inputs:
unity-version:
description: Full unity version name to build with. e.g. 2019.4.18f1
default: '2019.4.18f1'

project-dir:
description: Location of the Unity project root (The folder that contains `Assets`) *relative to the github workspace*

asset-files:
description: Glob of assets to bundle. Mutually exclusive with `project-dir`

assetbundle-name:
description: Name of the assetbundle to generate. Mutually exclusive with `project-dir`

output-dir:
description: Destination for built assetbundles
default: 'Bundle'

build-method:
description: Method to call to build the assetbundles. Uses a builtin build script with sensible values by default
default: 'KSPBuildTools.AssetBundleBuilder.BuildBundles'

runs:
using: composite
steps:
- name: Pre-Run (Unity Project)
if: inputs.project-dir != ''
shell: bash
run: |
echo 'ASSETBUNDLE_TMP=Assetbundles_TMP' >> $GITHUB_ENV
echo 'PROJECT_DIR=${{inputs.project-dir}}' >> $GITHUB_ENV
${{ inputs.asset-files != '' && 'echo "Cannot set project directory and asset-files in the same action run" && false'}}
${{ inputs.assetbundle-name!= '' && 'echo "Cannot set project directory and assetbundle-name in the same action run" && false'}}

- name: Pre-Run (Asset list)
if: inputs.project-dir == ''
shell: bash
run: |
echo 'ASSETBUNDLE_TMP=Assetbundle_${{ inputs.assetbundle-name }}_TMP' >> $GITHUB_ENV
echo 'PROJECT_DIR=Project_${{ inputs.assetbundle-name }}_TMP' >> $GITHUB_ENV
- name: Make Temp Project Directory
if: inputs.project-dir == ''
shell: bash
run: |
mkdir -p '${{ env.PROJECT_DIR }}/Assets/Bundle'
cp -r ${{ inputs.asset-files }} '${{ env.PROJECT_DIR }}/Assets/Bundle/'
- name: Make Destination Directory
shell: bash
run: |
mkdir -p ${{ env.ASSETBUNDLE_TMP }}
mkdir -p ${{ inputs.output-dir }}
- name: Copy Editor Tools Into Project
if: startsWith(inputs.build-method, 'KSPBuildTools')
shell: bash
run: |
mkdir -p ${{ env.PROJECT_DIR }}/Assets/KSPBuildTools/Editor
cp ${{ github.action_path }}/AssetBundleBuilder.cs ${{ env.PROJECT_DIR }}/Assets/KSPBuildTools/Editor/
- name: Cache Assetbundles
id: cache-assetbundles
uses: actions/cache@v4
with:
path: ${{ env.ASSETBUNDLE_TMP }}
key: ${{ inputs.assetbundle-name || 'Assetbundles' }}-${{ inputs.build-method }}-${{ env.PROJECT_HASH }}
env:
PROJECT_HASH: ${{ hashFiles(format('{0}/**', env.PROJECT_DIR)) }}

- name: Cache Unity Library
if: steps.cache-assetbundles.outputs.cache-hit != 'true'
uses: actions/cache@v4
with:
path: ${{ env.PROJECT_DIR }}/Library
key: library-${{ hashFiles(format('{0}/**', env.PROJECT_DIR)) }}
restore-keys: |
library-
- uses: game-ci/unity-builder@v4
if: steps.cache-assetbundles.outputs.cache-hit != 'true'
with:
targetPlatform: 'StandaloneWindows64'
unityVersion: ${{ inputs.unity-version }}
projectPath: ${{ env.PROJECT_DIR }}
customParameters: >
-assetbundlePath /github/workspace/${{ env.ASSETBUNDLE_TMP }}
${{ inputs.project-dir == '' && format('-assetbundleName {0}', inputs.assetbundle-name) }}
buildMethod: ${{ inputs.build-method }}
allowDirtyBuild: true
versioning: None

- name: Copy bundles to target directory
shell: bash
run: |
rm -f ${{ env.ASSETBUNDLE_TMP }}/${{ env.ASSETBUNDLE_TMP }}
rm -f ${{ env.ASSETBUNDLE_TMP }}/*.manifest
cp ${{ env.ASSETBUNDLE_TMP }}/* ${{ inputs.output-dir }}/
26 changes: 15 additions & 11 deletions .github/workflows/internal-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ env:
NuGetDirectory: ${{ github.workspace}}/nuget

jobs:
test-plugin:
uses: './.github/workflows/internal-test-plugin.yml'

test-plugin-nuget:
uses: './.github/workflows/internal-test-plugin-nuget.yml'
needs: [ build ]

test-plugin-legacy:
uses: './.github/workflows/internal-test-plugin-legacy.yml'

build:
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
Expand Down Expand Up @@ -52,9 +42,23 @@ jobs:
if-no-files-found: error
path: ${{ env.NuGetDirectory }}/*.nupkg

test-plugin:
uses: './.github/workflows/internal-test-plugin.yml'

test-plugin-nuget:
uses: './.github/workflows/internal-test-plugin-nuget.yml'
needs: [ build ]

test-plugin-legacy:
uses: './.github/workflows/internal-test-plugin-legacy.yml'

test-assetbundle:
uses: './.github/workflows/internal-test-assetbundle.yml'
secrets: inherit

deploy-nuget:
runs-on: ubuntu-latest
needs: [ test-plugin, test-plugin-nuget, test-plugin-legacy ]
needs: [ test-plugin, test-plugin-nuget, test-plugin-legacy, test-assetbundle ]
environment:
name: "NuGet"
url: "https://www.nuget.org/packages/KSPBuildTools"
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/internal-test-assetbundle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This is an internal test for KSPBuildTools and not intended to be used by other projects
name: Test Assetbundle

on:
workflow_call:
secrets:
UNITY_LICENSE:
UNITY_EMAIL:
UNITY_PASSWORD:
env:
TESTDIR: tests/plugin-mod

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/checkout@v4
with:
repository: 'drewcassidy/Resurfaced'
path: 'Resurfaced'

- uses: ./.github/actions/build-assetbundles
with:
assetbundle-name: foo.shab
asset-files: 'Resurfaced/*.shader Resurfaced/*.cginc'
env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}

- uses: actions/upload-artifact@v4
with:
path: 'Bundle'
name: assetbundle
if-no-files-found: 'warn'
2 changes: 2 additions & 0 deletions KSPBuildTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@
<ItemGroup>
<Compile Remove="$(MSBuildThisFileDirectory)tests/**" />
<None Remove="$(MSBuildThisFileDirectory)tests/**" />
<Compile Remove="$(MSBuildThisFileDirectory).github/**" />
<None Remove="$(MSBuildThisFileDirectory).github/**" />
</ItemGroup>
</Project>

0 comments on commit 2701fb8

Please sign in to comment.