Wednesday, December 17, 2014

How to play test networking in Unity

One of the very basic challenges I ran into when developing networking for our game was play testing. In a non-networked game, the dev cycle is: make changes, run the game, make changes, run the game, all happening fast from within the Unity editor. You cannot do the same for a client and server game because you cannot open one project twice in Unity. Do you have two separate projects one for client, second for server? That's not so great if they share code. Do you build the server into standalone and run the client from Unity editor? That's not so great if your build time is in minutes. Instead I use folder synchronization together with folder conventions. But lets start at the beginning. The following applies to Unity on Mac. My guess is Windows setup would be similar.

The first challenge is getting two instances of Unity to run at all. If you just double-click on Unity app a second time, it will always switch to the already running instance. So, create a shell script start_unity.sh:
#!/bin/bash
# start_unity.sh
/Applications/Unity/Unity.app/Contents/MacOS/Unity
Also in Unity, enable Unity -> Preferences -> Always show project wizard. Now when you run start_unity.sh it will start another instance of Unity.

The second challenge is opening the same project twice in Unity. That's not possible. But you can duplicate your project and open it in another Unity instance. Now if only we didn't have to manually copy the project and restart Unity all the time. So, create a shell script sync_client.sh:
#!/bin/bash
# sync_client.sh
rsync -avz --delete server/Assets client
The folder structure is:
myamazinggame
  -- server
  -- client
  sync_client.sh

The main Unity project is in server folder. Create a fresh new Unity project in client folder. Run sync_client.sh and open client project in another Unity instance. So now you got two Unity instances, running identical project, one from server, second from client folders. In my workflow, I make manual changes only to the server project, including editing client code and adding client assets. Then I sync the client project with sync_client.sh. This workflow is almost as fast as for non-networked game.

You might have some libs on the server that would break the build for the client (eg. database access dlls will fail the build for iOS). In this case put all server specific assets into Server folders (doesn't have to be just one):
myamazinggame
  -- server
    -- Assets
      -- Plugins
        -- Server
      -- Scripts
        -- Server
  -- client
  sync_client.sh

And modify sync_client.sh to:
#!/bin/bash
# sync_client.sh
rsync -avz --delete --exclude=Server/ --exclude=Server.meta server/Assets client
Now the sync to client folder will skip all Server folders.

No comments:

Post a Comment