DEV Community

Cover image for Integrating Unity3D into .NET Aspire with `Aspire.Unity`
Dutchskull
Dutchskull

Posted on • Edited on

Integrating Unity3D into .NET Aspire with `Aspire.Unity`

.NET Aspire is a new application model for building distributed apps with cloud-native building blocks. It’s great for wiring together APIs, databases, and background workers.

But what if you also want to bring Unity3D into the mix? 🎮

That’s exactly what I’ve been experimenting with in Aspire.Unity. It’s an AppHost extension that allows you to start and stop Unity projects as part of your Aspire application.

⚠️ Heads up: This project is experimental — I’m not sure yet how far I’ll take it. But it’s already fun to play with, and it might spark ideas for others.

Why?

When experimenting with distributed apps, I wanted to:

  • Run a Unity3D project as part of the same lifecycle as my other Aspire services.
  • Eventually, be able to pass environment variables into Unity.
  • Treat Unity as “just another service” in my app topology.

Imagine a scenario where a Unity frontend visualizes data from Aspire-powered APIs, or where Unity runs simulations controlled by your backend services.

Installing the package

1. Add the .NET Aspire Unity package to your AppHost project

dotnet add package Dutchskull.Aspire.Unity
Enter fullscreen mode Exit fullscreen mode

2. Add the Unity integration side

Open Unity and add this Git-based dependency in the Package Manager:

https://github.com/Dutchskull/Aspire.Unity3D.git?path=/AspireIntegration/Packages/AspireIntegration
Enter fullscreen mode Exit fullscreen mode

Usage

Minimal example

Adding a Unity project to your Aspire AppHost is just one line:

var unity = builder.AddUnityProject("game", "..\\..\\AspireIntegration", 1);
Enter fullscreen mode Exit fullscreen mode

This will:

  • Register a Unity project named game.
  • Use the relative path ..\\..\\AspireIntegration.
  • Start Unity on the default URL http://127.0.0.1:54021.
  • And open the scene with index 1 and start unity playmode.

Advanced usage

If you need more control, the method signature is:

public static IResourceBuilder<UnityProjectResource> AddUnityProject(
    this IDistributedApplicationBuilder builder,
    string name,
    string projectPath,
    int? sceneIndex = null,
    string url = "http://127.0.0.1",
    int port = 54021,
    string? customUnityInstallRoot = null) {...}

public static IResourceBuilder<UnityProjectResource> AddUnityProject(
    this IDistributedApplicationBuilder builder,
    string name,
    string projectPath,
    string? sceneName = null,
    string url = "http://127.0.0.1",
    int port = 54021,
    string? customUnityInstallRoot = null) {...}
Enter fullscreen mode Exit fullscreen mode
  • sceneName/sceneIndex → You can pass the scene index or name that should be playing in unity when starting the AppHost. Make sure it exists in the build settings.
  • url → The base URL for Unity (default: http://127.0.0.1).
  • port → The port Unity listens on (default: 54021).
  • customUnityInstallRoot → (optional) Custom path to your Unity installation.

Example with all arguments:

var unity = builder.AddUnityProject(
    name: "game",
    projectPath: "..\\..\\AspireIntegration",
    url: "http://localhost",
    port: 55000,
    customUnityInstallRoot: "E:\\Unity"
);
Enter fullscreen mode Exit fullscreen mode

Roadmap

Planned improvements include:

  • Passing environment variables into Unity.
  • Richer communication between Aspire and Unity (Open telemetry).

Wrapping up

This is still just an experiment, but it already shows what’s possible when bridging Aspire with Unity.

👉 Repo: Dutchskull/Aspire.Unity3D

I’d love to hear your thoughts — how would you use Aspire + Unity together?

Top comments (0)