Will It Host? / Unity Editor

Can you run a Minecraft server in Unity?

It hosted#14 of 18 by difficulty

Yes. The process listening on port 25565 is the Unity Editor, and a vanilla Minecraft client connects to it and plays on it. The world is the scene: place a block and a cube appears in the Hierarchy, delete the cube or drag it with the move gizmo and the block follows, and Collapse World switches gravity on so the whole build falls down in both at once.

verdict
It hosted
A vanilla Minecraft client connected and played on it.
difficulty
#14 of 18
Ranked against everything else in the series, hardest first.
built withC#
protocolMinecraft 1.8.9
read the source on github β†’

How it works

Two C# files in a folder called Editor, which Unity compiles and loads in to the editor on its own. No plugin system, no mod loader and no play mode. A reader thread blocks on the socket, a 20ms tick thread handles keep-alives, and everything bound for Unity crosses on a ConcurrentQueue applied in EditorApplication.update. One loop compares every tracked cube with the cell it is filed under, so a deleted cube, a dragged cube and a falling cube all produce the same block change.

The part that nearly stopped it

Play mode. Entering it reloads the domain, which wipes every static in the process, the server included, and physics normally only runs in play mode. So the collapse needed physics without the one thing that turns physics on.

What got it over the line

Running physics by hand in edit mode. Physics.simulationMode is set to Script and Physics.Simulate is called from the editor tick, so cubes fall while the server keeps its connection. Unity also slows its update loop when unfocused, which is why the setup asks for No Throttling: without it the cubes arrive in bursts.

What it cannot do

Minecraft 1.8.9, the same target as the rest of the series. One player, a world sixteen blocks tall, and nothing is saved. Any edit to either script recompiles and reloads the domain, which resets the world on both sides.

Why this is possible at all

A Minecraft Java server is a smaller thing than its reputation suggests. It is a socket, a packet codec and a game loop. The client opens one TCP connection, sends a handshake naming its protocol version, moves through login into the play state, and from then on both sides exchange length-prefixed packets describing chunks, entities and movement. None of that needs a game engine. It needs something that can hold a socket open and put the right bytes on it in the right order, which is why Unity Editor can be made to do it. In every episode the host is the hard part.

Every build in this series targets Minecraft 1.8.9 for one specific reason. In 1.8 a chunk is a flat byte array you can send uncompressed. From 1.9 onward chunk data is palette-encoded into bit-packed longs and the connection expects zlib, which is not something you can hand-write in VBA. 1.8.9 is the last release where the whole protocol is within reach of a scripting runtime that was never meant for it.

We have written both halves of that up properly: how the Minecraft server protocol works covers the wire format, and what a Minecraft server actually needs to run covers the four things a real one cannot do without.

Questions

Can Unity run a Minecraft server?

Yes, inside the editor. Scripts in a folder named Editor are compiled against UnityEditor and loaded in to the editor process, so C# there can open a TcpListener on port 25565 as soon as the project loads. Unity 6 Personal is enough. The full source is on GitHub.

Why does nothing touch Unity from the socket thread?

Because Unity's API is main thread only and throws from anywhere else. The socket threads push events on to a queue and the editor update drains it, so every GameObject is created, moved and destroyed on the main thread.

The rest of the series

See the full Will It Host? board β†’

Or just host it properly

Unity Editor is not where your survival world should live. AMD Ryzen 9700X, hard-reserved RAM, NVMe, online in under a minute with a free address, DDoS protection and a 7-day money-back guarantee.