private void Start() { if (PairingRole == Role.Connector) { pairMaker = new DirectPairConnector(RemoteAddress, RemotePort); } else { pairMaker = new DirectPairReceiver(LocalPort); } pairingAdapter = new PairingAdapter(); pairingAdapter.SuccessEvent += OnPairingConnectionSucceeded; pairingAdapter.FailureEvent += OnPairingConnectionFailed; // Register to listen for disconnections, so we can reconnect automatically if (SharingStage.IsInitialized) { sharingMgr = SharingStage.Instance.Manager; if (sharingMgr != null) { connectionAdapter = new NetworkConnectionAdapter(); connectionAdapter.DisconnectedCallback += OnDisconnected; NetworkConnection pairedConnection = sharingMgr.GetPairedConnection(); pairedConnection.AddListener((byte)MessageID.StatusOnly, connectionAdapter); } } StartPairing(); }
private void OnDestroy() { // SharingStage's OnDestroy() might execute first in the script order. Therefore we should check if // SharingStage.Instance still exists. Without the instance check, the execution of GetPairingManager // on a disposed sharing manager will crash the Unity Editor and application. if (SharingStage.IsInitialized && sharingMgr != null) { PairingManager pairingMgr = sharingMgr.GetPairingManager(); pairingMgr.CancelPairing(); // Safe to call, even if no pairing is in progress. Will not cause a disconnect // Remove our listener from the paired connection NetworkConnection pairedConnection = sharingMgr.GetPairedConnection(); pairedConnection.RemoveListener((byte)MessageID.StatusOnly, connectionAdapter); } }