unity-walkThrough

*We assure you that we do not spam. You may receive occasional emails from us.
 You can always unsubscribe.

Unity Realtime Multiplayer Sample

View Source And Download

To help you get started, we have created a sample project that illustrates how to get the pieces together and set up the basic code required. Complete source code for the sample is available in our Git Repo. Below are some of the relevant code snippets. There are two main script files which contain the appwarp integration. They are appwarp.cs and Listener.cs.

appwarp.cs implements the main MonoBehaviour class which has the Update loop. You need to replace your API Key, Secret Key and Room Id where indicated in this file. These are the values you get from AppHq console. The keys are used to initialize the SDK. You need to pass in your Api key and Secret key.

WarpClient.initialize(apiKey,secretKey);

This file also contains code to send position updates about the local player and handles remote player updates and moves the remote player on screen. To receive notifications, connection and room join/leave callbacks, you need to add corresponding listeners as defined in Listeners.cs

Listener warpLayer = new Listener();
WarpClient.GetInstance().AddConnectionRequestListener(warpLayer);
WarpClient.GetInstance().AddNotificationListener(warpLayer);
WarpClient.GetInstance().AddRoomRequestListener(warpLayer);

Its IMPORTANT to call the Update API on WarpClent in MonoBehavior’s main update loop. This is required so that AppWarp SDK can pump callbacks on to the main thread.

WarpClient.GetInstance().Update();

Now simply call connect to establish your connection with the server.

// join with a unique name (current time stamp)
username = System.DateTime.UtcNow.Ticks.ToString();
WarpClient.GetInstance().Connect(username);

Once your connection is established and authenticated, you will simply join a room and subscribe it to receive its notifications. This is the room that you created from AppHQ (developer console). Code to do this is in Listener.cs file.

WarpClient.GetInstance().SubscribeRoom(appwarp.roomid);
WarpClient.GetInstance().JoinRoom(appwarp.roomid);

This file abstracts all the AppWarp SDK callbacks from the main appwarp.cs manager class and forwards only the ones which are relevant in this example.

After you have joined the room, you can start sending your user’s coordinates to other users in the room. In this example we will use our Chat api to do that

string json = "{\"x\":\""+transform.position.x+"\",\"y\":\""+transform.position.y+"\",\"z\":\""+transform.position.z+"\"}";
WarpClient.GetInstance().SendChat(json);

Once the remote player also joins, you will start receiving messages from it. Handle them and move the cylinder representing it

public void onChatReceived (ChatEvent eventObj)
{
    Log(eventObj.getSender() + " sent " + eventObj.getMessage());
    SimpleJSON.JSONNode msg =  SimpleJSON.JSON.Parse(eventObj.getMessage());
    if(eventObj.getSender() != appwarp.username)
    {
        appwarp.movePlayer(msg["x"].AsFloat,msg["y"].AsFloat,msg["z"].AsFloat);
    }
}

And thats it! Simply build and run your Unity3D application on two different endpoints and you will be able to see each other’s endpoint movements in realtime (a simple white cylinder).