Configuring the part in Unity

From KSP 2 Modding Wiki

This page is only for part mod. If your mod don't include parts, you can skip what follows.

Configuring the part in Unity

Prerequisites
Setting up Unity
Preparing the mesh for Unity
Useful links
Tutorial for creating a part in Unity: https://luxstice.notion.site/Tutorial-My-First-Part-5f0cf456d7f4443d8c92658c7cc58314
Tutorial for configuring a part in Unity: https://www.youtube.com/watch?v=9fQg-oMqcH8

Setting up the scene

  1. Open Unity Hub.
  2. Open the Unity project.
  3. Click on the three dots in the Hierarchy menu then “Add a new scene”.
  4. Right-click on the scene > Save Scene.
  5. Save the scene in the Asset folder of the Unity project.
  6. In the Asset folder, create a folder for the mod.
  7. In this folder, create the “Materials” and “Parts” folders. This is just to keep the folder well organized.
  8. Delete the camera and light and import the meshes and textures into the corresponding folders.
Good practice for organizing the Assets folder
Assets
|
\-Modname
  |
  \-Parts
    |
    \-(separate folders for the type of parts)
      |
      \-Folder for each part
  |
  \-Materials

Associating textures to the meshes

  1. Create a material in Unity for each material in Substance Painter (right-click on the project window > Create > Material)
  2. Select all materials. In the Inspector window, in Shader, select KSP > Parts > Paintable.
  3. For each material, assign the textures to the Albedo, Metallic, Normal, Emissive and Paintable channels.
  4. For Normal channels, click the “Fix Now” button.
  5. For each mesh, assign the materials to the corresponding locations. Click on “Apply”.

Adding meshes to the scene

  1. Create an Empty GameObject. Let's name it myMod_myPart. The name must be unique to any part in KSP2, including other mods.
  2. Set its position to 0;0;0.
  3. Create another empty object as a child of this object and call it model.
  4. Drop the mesh as a child of model.
  5. Use the “Transform” panel to optionally modify the position, rotation and size of the mesh.
  6. Right click on the mesh > Prefab > Unpack completely.
  7. Select all colliders.
  8. In the Inspector window:
    • Delete the “Mesh Renderer” component.
    • Click on Add Component > Mesh Collider.
    • Check “Convex”.
  9. Repeat the operation for all meshes.

Note: Materials should not have shaders.

Configuring the part data

Follow these instructions: Configuring the core part data.

Configuring the addressables

Creating the addressables

  1. Select all prefabs in the Asset folder and check “Addressable” in the Inspector window.
  2. Select all JSON in the Asset folder and check “Addressable” in the Inspector window.
  3. Select all icon images in the Asset folder and check “Addressable” in the Inspector window.

Creating a group of addressables

  1. Go to Window > Asset Management > Addressables > Groups.
  2. Click Create > Group > Packed Asset.
  3. Rename the group and move the mod elements into it.
  4. Rename each addressable name to remove the path. The final name must be the same as the part name + extension.
  5. Add the parts_data label to the JSON. If it does not exist, create it with “Manage labels” > “+”.

Creating a build profile

  1. Go to Window > Asset Management > Addressables > Groups.
  2. Click on Profile > Manage Profiles.
  3. Click on Create > Profile.
  4. Rename the new profile.
  5. For a mod with code, enter the values as follows, where XXX = the namespace of the .NET project previously configured.
Field name Value
LocalBuildPath Library/com.unity.addressables/aa/Windows/StandaloneWindows64
LocalLoadedPath {XXX.XXXPlugin.Path}/addressables/StandaloneWindows64

For a codeless mod, enter the values as follows, where XXX = the mod's name.

Field name Value
LocalBuildPath Library/com.unity.addressables/aa/Windows/StandaloneWindows64
LocalLoadedPath {SpaceWarpPaths.XXX}/addressables/StandaloneWindows64