Exemplo n.º 1
0
    private void InitJoin()
    {
        var guest = new BleSock.GuestPeer();

        guest.onBluetoothRequire += () =>
        {
            Log("Bluetoothを有効にしてください");
        };

        guest.onReady += () =>
        {
            Log("初期化が完了しました");

            try
            {
                guest.StartScan();
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Log("スキャン開始できません");
                return;
            }

            Log("デバイスを探索しています..");
            devicesDropdown.ClearOptions();
        };

        guest.onFail += () =>
        {
            Log("失敗しました");
        };

        guest.onDiscover += (deviceName, deviceId) =>
        {
            Log("デバイスを発見: {0} [{1}]", deviceName, deviceId);
            devicesDropdown.options.Add(new DeviceOptionData(deviceName, deviceId));

            if (!devicesDropdown.interactable)
            {
                devicesDropdown.interactable = true;
                devicesDropdown.value        = 0;
                devicesDropdown.RefreshShownValue();
                connectButton.interactable = true;
            }
        };

        guest.onConnect += () =>
        {
            Log("接続されました");

            joinObject.SetActive(false);

            AddMyPlayerCharacter();
        };

        guest.onDisconnect += () =>
        {
            Log("切断されました");

            mPeer.Dispose();
            mPeer = null;
        };

        guest.onPlayerJoin  += OnPlayerJoin;
        guest.onPlayerLeave += OnPlayerLeave;
        guest.onReceive     += OnReceive;

        try
        {
            guest.Initialize(PROTOCOL_IDENTIFIER, playerNameInputField.text);
        }
        catch (Exception e)
        {
            Debug.LogException(e);
            Log("初期化できません");
            return;
        }

        Log("初期化しています..");
        mPeer = guest;
    }
Exemplo n.º 2
0
    private void Start()
    {
        modeSelectObject.SetActive(true);

        playerNameInputField.onEndEdit.AddListener((name) =>
        {
            bool interactable       = !string.IsNullOrEmpty(name);
            hostButton.interactable = interactable;
            joinButton.interactable = interactable;
        });

        // Host

        hostButton.interactable = false;
        hostButton.onClick.AddListener(() =>
        {
            modeSelectObject.SetActive(false);
            testObject.SetActive(true);

            backButton.interactable = true;

            var host            = new BleSock.HostPeer();
            host.MaximumPlayers = 2;

            host.onReady += () =>
            {
                Log("初期化が完了しました");

                try
                {
                    host.StartAdvertising(playerNameInputField.text);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                    Log("アドバタイズ開始できません");
                }

                Log("接続を待っています..");
            };

            host.onBluetoothRequire += () =>
            {
                Log("Bluetoothを有効にしてください");
            };

            host.onFail += () =>
            {
                Log("失敗しました");
            };

            host.onPlayerJoin += (player) =>
            {
                Log("{0} が参加しました", player.PlayerName);
                sizesDropdown.interactable = true;
                testButton.interactable    = true;
            };

            host.onPlayerLeave += (player) =>
            {
                Log("{0} が離脱しました", player.PlayerName);
                sizesDropdown.interactable = false;
                testButton.interactable    = false;
            };

            host.onReceive += OnReceive;

            try
            {
                host.Initialize(PROTOCOL_IDENTIFIER, playerNameInputField.text);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Log("初期化できません");
                return;
            }

            Log("初期化しています..");
            mPeer = host;
        });

        // Guest

        connectObject.SetActive(false);

        joinButton.interactable = false;
        joinButton.onClick.AddListener(() =>
        {
            modeSelectObject.SetActive(false);
            connectObject.SetActive(true);
            testObject.SetActive(true);

            devicesDropdown.interactable = false;
            connectButton.interactable   = false;

            backButton.interactable = true;

            var guest = new BleSock.GuestPeer();

            guest.onBluetoothRequire += () =>
            {
                Log("Bluetoothを有効にしてください");
            };

            guest.onReady += () =>
            {
                Log("初期化が完了しました");

                try
                {
                    guest.StartScan();
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                    Log("スキャン開始できません");
                    return;
                }

                Log("デバイスを探索しています..");
                devicesDropdown.ClearOptions();
            };

            guest.onFail += () =>
            {
                Log("失敗しました");
            };

            guest.onDiscover += (deviceName, deviceId) =>
            {
                Log("デバイスを発見: {0} [{1}]", deviceName, deviceId);
                devicesDropdown.options.Add(new ValueOptionData(deviceName, deviceId));

                if (!devicesDropdown.interactable)
                {
                    devicesDropdown.interactable = true;
                    devicesDropdown.value        = 0;
                    devicesDropdown.RefreshShownValue();
                    connectButton.interactable = true;
                }
            };

            guest.onConnect += () =>
            {
                Log("接続されました");
                sizesDropdown.interactable = true;
                testButton.interactable    = true;
            };

            guest.onDisconnect += () =>
            {
                Log("切断されました");
                sizesDropdown.interactable = false;
                testButton.interactable    = false;
            };

            guest.onPlayerJoin += (player) =>
            {
                Log("{0} が参加しました", player.PlayerName);
            };

            guest.onPlayerLeave += (player) =>
            {
                Log("{0} が離脱しました", player.PlayerName);
            };

            guest.onReceive += OnReceive;

            try
            {
                guest.Initialize(PROTOCOL_IDENTIFIER, playerNameInputField.text);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Log("初期化できません");
                return;
            }

            Log("初期化しています..");
            mPeer = guest;
        });

        connectButton.onClick.AddListener(() =>
        {
            var optionData = (ValueOptionData)devicesDropdown.options[devicesDropdown.value];
            try
            {
                ((BleSock.GuestPeer)mPeer).Connect(optionData.value);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Log("エラー");
                return;
            }

            Log("デバイスに接続しています..");
            devicesDropdown.interactable = false;
            connectButton.interactable   = false;
        });

        // Test

        testObject.SetActive(false);

        sizesDropdown.interactable = false;
        for (int i = 8; i < 13; ++i)
        {
            int size = 1 << i;
            sizesDropdown.options.Add(new ValueOptionData(size.ToString(), size));
        }
        sizesDropdown.value = 0;

        testButton.interactable = false;
        testButton.onClick.AddListener(() =>
        {
            int size  = ((ValueOptionData)sizesDropdown.options[sizesDropdown.value]).value;
            var bytes = new byte[size];

            mRandom.GetBytes(bytes);

            bytes[0] = 0;

            try
            {
                mPeer.Send(bytes, bytes.Length, BleSock.Address.Others);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Log("エラー");
                return;
            }

            Log("スピード計測中..");
            mStartTime = Environment.TickCount;
            sizesDropdown.interactable = false;
            testButton.interactable    = false;
        });

        backButton.interactable = false;
        backButton.onClick.AddListener(() =>
        {
            if (mPeer != null)
            {
                mPeer.Dispose();
                mPeer = null;
            }

            mLogs.Clear();

            modeSelectObject.SetActive(true);
            connectObject.SetActive(false);
            testObject.SetActive(false);
        });
    }
Exemplo n.º 3
0
    private void Start()
    {
        modeSelectObject.SetActive(true);

        playerNameInputField.onEndEdit.AddListener((name) =>
        {
            bool interactable       = !string.IsNullOrEmpty(name);
            hostButton.interactable = interactable;
            joinButton.interactable = interactable;
        });

        // Host

        hostObject.SetActive(false);

        hostButton.interactable = false;
        hostButton.onClick.AddListener(() =>
        {
            modeSelectObject.SetActive(false);
            hostObject.SetActive(true);
            chatObject.SetActive(true);

            advertiseButton.interactable = false;
            stopButton.interactable      = false;

            backButton.interactable = true;

            var host = new BleSock.HostPeer();

            host.onReady += () =>
            {
                Log("初期化が完了しました");
                advertiseButton.interactable = true;
                sendInputField.interactable  = true;
            };

            host.onBluetoothRequire += () =>
            {
                Log("Bluetoothを有効にしてください");
            };

            host.onFail += () =>
            {
                Log("失敗しました");
            };

            host.onPlayerJoin += (player) =>
            {
                Log("{0} が参加しました", player.PlayerName);
            };

            host.onPlayerLeave += (player) =>
            {
                Log("{0} が離脱しました", player.PlayerName);
            };

            host.onReceive += (message, messageSize, sender) =>
            {
                Log("{0}: {1}", sender.PlayerName, Encoding.UTF8.GetString(message, 0, messageSize));
            };

            try
            {
                host.Initialize(PROTOCOL_IDENTIFIER, playerNameInputField.text);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Log("初期化できません");
                return;
            }

            Log("初期化しています..");
            mPeer = host;
        });

        advertiseButton.onClick.AddListener(() =>
        {
            try
            {
                ((BleSock.HostPeer)mPeer).StartAdvertising(playerNameInputField.text);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Log("アドバタイズ開始できません");
                return;
            }

            Log("アドバタイズしています..");
            advertiseButton.interactable = false;
            stopButton.interactable      = true;
        });

        stopButton.onClick.AddListener(() =>
        {
            ((BleSock.HostPeer)mPeer).StopAdvertising();

            Log("アドバタイズを停止しました");
            advertiseButton.interactable = true;
            stopButton.interactable      = false;
        });

        // Guest

        guestObject.SetActive(false);

        joinButton.interactable = false;
        joinButton.onClick.AddListener(() =>
        {
            modeSelectObject.SetActive(false);
            guestObject.SetActive(true);
            chatObject.SetActive(true);

            devicesDropdown.interactable  = false;
            connectButton.interactable    = false;
            disconnectButton.interactable = false;

            backButton.interactable = true;

            var guest = new BleSock.GuestPeer();

            guest.onBluetoothRequire += () =>
            {
                Log("Bluetoothを有効にしてください");
            };

            guest.onReady += () =>
            {
                Log("初期化が完了しました");

                try
                {
                    guest.StartScan();
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                    Log("スキャン開始できません");
                    return;
                }

                Log("デバイスを探索しています..");
                devicesDropdown.ClearOptions();
            };

            guest.onFail += () =>
            {
                Log("失敗しました");
            };

            guest.onDiscover += (deviceName, deviceId) =>
            {
                Log("デバイスを発見: {0} [{1}]", deviceName, deviceId);
                devicesDropdown.options.Add(new DeviceOptionData(deviceName, deviceId));

                if (!devicesDropdown.interactable)
                {
                    devicesDropdown.interactable = true;
                    devicesDropdown.value        = 0;
                    devicesDropdown.RefreshShownValue();
                    connectButton.interactable = true;
                }
            };

            guest.onConnect += () =>
            {
                Log("接続されました");

                foreach (var player in guest.Players)
                {
                    Log(player.PlayerName);
                }

                sendInputField.interactable = true;
            };

            guest.onDisconnect += () =>
            {
                Log("切断されました");
                sendInputField.interactable   = false;
                disconnectButton.interactable = false;
            };

            guest.onPlayerJoin += (player) =>
            {
                Log("{0} が参加しました", player.PlayerName);
            };

            guest.onPlayerLeave += (player) =>
            {
                Log("{0} が離脱しました", player.PlayerName);
            };

            guest.onReceive += (message, messageSize, sender) =>
            {
                Log("{0}: {1}", sender.PlayerName, Encoding.UTF8.GetString(message, 0, messageSize));
            };

            try
            {
                guest.Initialize(PROTOCOL_IDENTIFIER, playerNameInputField.text);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Log("初期化できません");
                return;
            }

            Log("初期化しています..");
            mPeer = guest;
        });

        connectButton.onClick.AddListener(() =>
        {
            var optionData = (DeviceOptionData)devicesDropdown.options[devicesDropdown.value];
            try
            {
                ((BleSock.GuestPeer)mPeer).Connect(optionData.deviceId);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Log("エラー");
                return;
            }

            Log("デバイスに接続しています..");
            devicesDropdown.interactable  = false;
            connectButton.interactable    = false;
            disconnectButton.interactable = true;
        });

        disconnectButton.onClick.AddListener(() =>
        {
            ((BleSock.GuestPeer)mPeer).Disconnect();
        });

        // Chat

        chatObject.SetActive(false);

        sendInputField.interactable = false;
        sendInputField.onEndEdit.AddListener((text) =>
        {
            var bytes = Encoding.UTF8.GetBytes(text);
            try
            {
                mPeer.Send(bytes, bytes.Length, BleSock.Address.All);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Log("エラー");
                return;
            }

            sendInputField.text = "";
        });

        backButton.interactable = false;
        backButton.onClick.AddListener(() =>
        {
            if (mPeer != null)
            {
                mPeer.Dispose();
                mPeer = null;
            }

            mLogs.Clear();

            modeSelectObject.SetActive(true);
            hostObject.SetActive(false);
            guestObject.SetActive(false);
            chatObject.SetActive(false);
        });
    }