Learn To Create A Local Multiplayer Game In Unity with Huawei Mobile Services

YusufAltun
Huawei Developers
Published in
5 min readNov 26, 2021

--

In this article, I will explain how you can integrate Huawei Nearby Service in your Unity games.

What is Nearby Service?

First of all, I want to start by explaining this Huawei Nearby Service.

Nearby Service includes Nearby Data Communication and Contact Shield. We will focus Data Commication part. Nearby Data Communication allows apps to easily discover nearby devices and set up communication channels with them using technologies such as Bluetooth and Wi-Fi. Contact Shield is a contact tracing service developed based on the Bluetooth low energy (BLE) technology.

It facilitates nearby communication in various scenarios with the following services: Nearby Connection, Nearby Message, and Nearby Wi-Fi Sharing. As I mentioned before we will use Nearby Connection. Which means its enables nearby devices to set up an Internet-free local network for direct data transmission.

The Nearby Service SDK includes the following major modules: broadcast and scanning, connection setup, and data transmission.
In this section, you will build a demo app to learn how to transfer data between devices using APIs of the HMS Core Nearby Service SDK.

You can visit this link to learn more about Nearby Service.

Preparations

Before we dive into the details we have a few steps we need to do in order to start developing our app.

Please check HMS Unity plugin integration details below:

Make sure to download the corresponding unity package for the Unity version you are using from the release section

  1. Create an app in AppGallery Connect.
  2. Create Unity Project.
  3. Add the app package name.
  4. Sign in to AppGallery Connect and select My projects.
  5. To use Nearby Service, enable it in AppGallery Connect first.
  6. If you have enabled, add the agconnect-services.json file to your game.
  7. We need to install Huawei Nearby Service SDK to our project. For this we need to check gradle version on project. This plugin handles gradle files with script because of that if we want change something we can update this scripts.

Game Logic

This project is a basic hypercasual game for Unity mobile platform with Huawei Mobile Services Plugin Nearby Service integration.

We have two phone had this game and we can ise one of them client other one as a server. If user click “Create a Game” button that user will be a server and it will wait other player to join. Other player also can join the game with “Join a Game” button. After clicking buttons they will be disappear. We have a movement and fight buttons also, we will send location and fight affect datas between users.

Before we start we should understand data logic.

Data Transmission Between Endpoints(Player 1 to Player 2):

For details please read this section.

Generally the process can be divided into four phases.

  1. Broadcasting and scanning
  2. Connection setup
  3. Data transmission
  4. Disconnection

Lets Code

In HMS Unity pluging we have HMSNearbyServiceManager.cs class, this is singleton and it’s help us to use this service easly. We will directly invoke ready functions for dealing main features.

Unity view for structure:

First step we create a GameManager class and it initialize player objects, default we use Left Deer is for broadcasting which mean its a server, right one is scanning which mean its a client. If users click “Create a Game” button they can control Left Deer, on the contrary if users click “Join a Game” button its can control Right Deer.

GameManager class user NearbyServe and Nearby Client for callbacks.

Second step we need to manage broadcasting for this we wrote NearbyServer class it invoke broadcasting functions.

After the discoverer requests to set up a connection with the broadcaster, the discoverer notifies the two parties of the connection setup by calling back the onEstablish(EnpointID, ConnectInfo) method of ConnectCallback. Both parties must accept the connection by calling AcceptConnectionRequest. The connection can be set up only when both endpoints accept the connection. If one or both of them choose to reject the connection, the connection fails. In either mode, the connection result is notified through the OnResult method

Server Side:

Client side:

Data Transmission

After a connection is set up, perform the following operations: (Serve and Client both have this funtions)

  1. Send data by calling SendData . The Data object can be of the BYTES, FILE, or STREAM type. We are sending data with UpdateEverySecond funtions.
  2. Receive data by calling the onReceived(EndPointID, Data) method in the DataCallback callback class.
    Obtain the data sending and receiving progress by calling the onTransferUpdate(Endpointid, TransferStateUpdate) method in the DataCallback callback class.

--

--