public static void RegisterHandler (short msgType, Networking.NetworkMessageDelegate handler);

参数

msgType消息类型编号。
handler收到此消息类型时将调用的函数处理程序。

描述

为特定消息类型注册处理程序。

您可以针对多种系统消息类型添加处理程序。您也可以添加自己的消息类型。

using UnityEngine;
using UnityEngine.Networking;

public class MyServer : NetworkManager { void Start() { NetworkServer.Listen(7070); Debug.Log("Registering server callbacks"); NetworkServer.RegisterHandler(MsgType.Connect, OnConnected); }

void OnConnected(NetworkMessage netMsg) { Debug.Log("Client connected"); } }

下面列出了系统消息类型:

class MsgType
{
    public const short ObjectDestroy = 1;
    public const short Rpc = 2;
    public const short ObjectSpawn = 3;
    public const short Owner = 4;
    public const short Command = 5;
    public const short LocalPlayerTransform = 6;
    public const short SyncEvent = 7;
    public const short UpdateVars = 8;
    public const short SyncList = 9;
    public const short ObjectSpawnScene = 10;
    public const short NetworkInfo = 11;
    public const short SpawnFinished = 12;
    public const short ObjectHide = 13;
    public const short CRC = 14;
    public const short LocalClientAuthority = 15;
}

这些消息中的大部分仅供内部使用。用户不应在此范围内定义消息 ID。