Version: 2018.1
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); } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961