Importing Custom Assets

Process for creating assets bundles and importing into your mod.

The method of importing custom assets described here requires knowledge of the basics of plugin style mods. There are alternative tools for replacing assets in the game, but using those tools/methods is not covered here. For reference, a list of common Unity modding tools can be found here.

Below, I'll describe the process for setup (installation of tools), creating your own prefab in the Unity Editor, packaging it into an asset bundle and using a BepInEx plugin to extract and spawn that asset in game. Note that while this guide will focus on spawning a model with physics, this same method can be used to import any prefab object you can create inside the Unity Editor.

### Install git

First, install git standalone (see basics page for download link for all tools referenced here). When installing git, you do no need to change any of the installation options unless you want to. Just click next, allow it to install and finish. Once you have done this, you will need to restart your computer.

### Install Unity Editor 2019.4.40f1 and Asset Bundle Browser package

Next, we need to install Unity Editor. Star Valor requires us to use version 2019.4.40f1. From the Unity Hub, select "Installs" from the left side, then click the "Install Editor" button at the top-right. Here, we can select the version we want to install.

Next, create a new unity project. At the top of the create project window, you can see the targeted Unity Editor version. This will always default to the latest one you have installed, so if you have multiple editor versions, ensure 2019.4.40f1 is selected in this drop-down list. Select the 3D core template and give it a name. I created a single project called "AssetTemp" that I use to create all game objects I will ever import.

Once in the Unity editor, from the top menu select Window > Package Manager. Click the small '+' icon at the top-left and select "Add package from git URL." Paste the following link and allow Unity to install the asset bundle browser package:

https://github.com/Unity-Technologies/AssetBundles-Browser.git

### Create your mod prefab

Okay, now we can create any Unity prefab we like. For the purposes of this example, we will just be adding a basic unity cube with a rigid body for collision.

Right-click in the hierarchy browser (in default Unity layout, this is on the left side), select 3D object and then Cube:

Drago the new game object from the hierarchy down to the Project explorer Assets folder (by default this will just have two sub folders in it "Scenes" and "TextMeshPro"). Now we have created our prefab. For the example, we will call this "MyLovelyCube".

Double click on the prefab in the project browser to open it. Add a rigid body component to the cube by selecting the Cube object from the hierarchy view, then in the Properties on the right, select Add Component > Physics > Rigid Body. The only property we're going to change here is restrict the movement on the Y axis. To do this, simple expand Constraints and check the Y box next to Freeze Position.

Note: The Y-axis is the depth axis in Star Valor. Restricting movement on this will prevent our cube flying away from or towards the camera in game. It can be useful to create your prefabs from the in-game perspective. In the preview pane in the middle of the screen, right click the widget at the top right (the cube with the axis cones pointing at it) and select "Top". This is less important for this simple cube we're making, but if you're planning to do anything more complex, if can be useful.

Okay, quick save of the project and moving on to creating our asset bundle.

### Export your prefab to an asset bundle

From Unity, select Window > Asset Bundle Browser. Select, then drag and drop your assets into this window. If you select multiple assets, I recommend selecting the "Creat 1 Bundle" option. It will make mod distribution and importing the bundle easier.

On the left, you can rename the bundle. This will be the filename. For the example, I will call this "MyBundle". On the right side, you will see a list of assets in the currently selected bundle.

Select build and ensure the build target is set correctly. This will normally be "Standalone Windows". It may differ for other distributions of the game, I am not certain.

Leave the other settings at defaults and click Build.

### Importing the asset bundle into a plugin

Please take a look at the basics page if you want further information on things not explained here.

In your plugin project, you will need to import a reference to the UnityEngine.AssetBundleModule.dll assembly.

The code below is the complete content of the mod class. It imports the asset in the awake method and stores it in the myLovelyCube field for later use. The update method spawns it at the current player location when the backspace key is pressed.

```
public const string pluginGuid = "mc.starvalor.mymod";
public const string pluginName = "My Mod";
public const string pluginVersion = "1.0.0";

GameObject myLovelyCube;

public void Awake()
{
 string pluginfolder = System.IO.Path.GetDirectoryName(GetType().Assembly.Location);
 string bundleName = "MyBundle";
 string prefabName = "Assets/MyLovelyCube.prefab";

 myLovelyCube = AssetBundle.LoadFromFile($"{pluginfolder}\\{bundleName}").LoadAsset<GameObject>(prefabName);
}

public void Update()
{
 if (Input.GetKeyDown(KeyCode.Backspace))
 {
 GameObject newGO = Instantiate(myLovelyCube);
 newGO.transform.position = GameManager.instance.Player.transform.position;
 }
}

```

Compile, copy and paste the .dll for the plugin and the asset bundle created earlier to the .\BepInEx\plugins\ folder. Your mod distribution will need to contain both of these files too.

And that's it. Ez :). Anything you can make a prefab of in the Unity Engine can be imported and dropped into Star Valor.