Version: 2018.2
public Action<int> NetworkEventAvailable ;

説明

Defines the callback delegate which you can use to get a notification when the host (defined by hostID) has a network event. The callback is called for all event types except NetworkEventType.Nothing.

See Also: NetworkEventType

This callback will be called from the IO thread. If you execute a long operation from this callback it will affect your IO operation performance, therefore you should return from these operations as soon as possible.

using System;
using UnityEngine;
using UnityEngine.Networking;

public class Test : MonoBehaviour { static int receiveCallbackOld = 0; static int receiveCallbackNew = 0; static int recHostId = -1;

//... public static void ReceiveCallback(int hostId) { ++receiveCallbackNew; //Notify main thread that ReceiveFromHost(hostId, ...) should be called, because there is incoming event waiting to read recHostId = hostId; }

void Start() { GlobalConfig config = new GlobalConfig(); config.NetworkEventAvailable = ReceiveCallback;

//.... }

void Update() { int recConnectionId = 0; int recChannelId = -1; var recArray = new byte[1400]; int recSize = 0; byte error; if (receiveCallbackNew != receiveCallbackOld) { receiveCallbackNew = receiveCallbackOld; var networkEvent = NetworkTransport.ReceiveFromHost(recHostId, out recConnectionId, out recChannelId, recArray, (ushort)recArray.Length, out recSize, out error); Debug.Assert(networkEvent != NetworkEventType.Nothing); } } }