Teach you how to integrate Nearby Service to improve game experience and reduce development difficulty

HMS Nearby Service is an important feature of HMS Core. Based on Wi-Fi/Bluetooth underlying technology, it helps gamers quickly find people around, and automatically establishes low-latency, high-reliability, and zero-traffic data transmission channels between devices to improve Game experience.

1. Nearby Service can provide a better experience for your game in the following aspects

1. One-click online

The existing local online solution requires users to access the same router, and if there is no router, the user needs to manually establish a hotspot, which is complicated to operate. Nearby Service can realize one-click local connection without the use of a router.

2. Team up face to face/add friends

Nearby Service can help you form a face-to-face team/add friends, without relying on social software, without GPS, allowing players to easily "open black" with their friends.

3. Face-to-face prop sharing

Nearby Service can help your game quickly realize face-to-face prop sharing, allowing players to easily share game props with real friends, which can help your game refresh and increase user stickiness.

Two, plug-in introduction

Here are two encapsulated plug-ins for everyone. You can use these two plug-ins directly in your application; you can also check the plug-in source code to learn how to integrate Nearby Service.

1. Development preparation

  • Unity development environment

  • Download plugin: GitHub

2. Import the plugin

  • On the Unity Tools menu, open "Assets" --> "Import Package" --> "Custom Package", and select the plug-in: Nearby Player/Discovery Plugin

  • Wait for the processing of the package. After completion, the resource list in the plug-in will be listed, click "Import"

3. Introduction to key codes

3.1、Nearby Player Plugin

Nearby Player Plugin is suitable for face-to-face teaming/adding friends/sharing scenarios. The plugin declares the NearbyManager class, which provides two methods startDiscovery() and SendMessage() for discovering nearby players and sending messages.

  • When the program starts, call startDiscovery to find surrounding players, and they will also be discovered by surrounding players. The calling interface code is as follows:
void Start() {
    AndroidMyCallback cb = new AndroidMyCallback(this);
    nm  = new  NearbyManager(cb);
    nm.startDiscovery(randomName());
}
  • The callback function AndroidMyCallback is used to define the actions that need to be done after the discovery is successful.
// 在发现一名玩家之后,如何处理。在本demo中我们将其添加到玩家列表
public override void onFoundPlayer(string endpointName, string endpointId) {
    mListController.AddPlayerToList(endpointName, endpointId);
}

// 在丢失一名玩家后,如何处理。在本demo中我们将其移除玩家列表
public override void onLostPlayer(string endpointId) {
    mListController.RemovePlayerFromList(endpointId);
}

// 在收到玩家消息时,如何处理。在本demo中我们只展示消息内容
public override void onReceiveMsg(string endpointName, string Msg) {
    mListController.ReceiveMsg(endpointName, Msg);
}
  • After discovering the success of nearby players, you can send messages such as team invitation/friend invitation/props sharing to the player.
// 在本demo中,我们点击玩家列表中的某位玩家,即向其发送组队邀请消息
private void OnClick(string endpointId) {
    nm.log("OnClick. SendMessage to " + endpointId);
    nm.SendMessage(endpointId, "invites you to join a game.");
}

3.2. Nearby Discovery Plugin
Nearby Discovery Plugin is a plug-in developed based on Unity UNET components. Using this plug-in can complete networking without users connecting to the same Wi-Fi. The Plug-in declares the NearbyManager class, which provides two methods startBroadcast() and startDiscovery(). In the actual game scenario, the two interfaces are called separately in two game devices to complete the networking.

  • Call interface code example
private void OnClick() {
    Button btn = this.GetComponent<Button>();
    btn.enabled = false;
    AndroidMyCallback androidMyCallback = new AndroidMyCallback(mNetworkManager);
    NearbyManager nearbyManager = new NearbyManager(androidMyCallback);
    nearbyManager.startBroadcast();
}
  • The callback function AndroidMyCallback is used to do what needs to be done after the networking is successful. In this example, the networkManager interface of UNET is called to start the game after discovery
public class AndroidMyCallback : AndroidCallback {
    private NetworkManager mNetworkManager;

    public AndroidMyCallback(NetworkManager networkManager) : base()  {
        mNetworkManager = networkManager;
    }

    public override void onClientReady(string ipaddr) {
        mNetworkManager.networkAddress = ipaddr;
        mNetworkManager.StartClient();
    }

    public override void onServerReady(string ipaddr) {
        mNetworkManager.StartHost();
    }
}

4. Sample application

In order for you to better understand how to use the plug-in, here are two sample applications that integrate the above plug-ins for your reference.

  • Nearby-Player-Demo
    Insert picture description here

  • UNET-NEARBY-DEMO
    Insert picture description here

4. Other game applications that integrate Nearby Service

  • Tic Tac Toe

Tic Tac Toe is a local online battle game developed based on the Native Android interface of Nearby. After integrating Nearby Service, it can realize online games without network. It is currently on the Huawei app market.

  • NearbyGameSnake

NearbyGameSnake is a multiplayer online game integrated with Nearby Service. The operation interface is simple. Players do not need to configure the network and can join the game with one click.

Five, more details


Original link:
https://developer.huawei.com/consumer/cn/forum/topic/0201395195892590034?fid=18
Author: timer

Guess you like

Origin blog.51cto.com/14772288/2551319