Ejemplo n.º 1
0
        void ShowSessionList(Dictionary <Guid, UdpSession> sessionList)
        {
            foreach (var session in sessionList)
            {
                UdpSession udpSession = session.Value as UdpSession;

                // Skip if is not a Photon session
                if (udpSession.Source != UdpSessionSource.Photon)
                {
                    BoltLog.Info("UdpSession with different Source: {0}", udpSession.Source);
                    continue;
                }

                PhotonSession photonSession = udpSession as PhotonSession;

                if (photonSession == null)
                {
                    continue;
                }

                string sessionDescription = String.Format("{0} ({1})",
                                                          photonSession.HostName, photonSession.Id);

                IProtocolToken token = photonSession.GetProtocolToken();

                // Normal Token
                RoomProtocolToken roomToken = token as RoomProtocolToken;

                if (roomToken != null)
                {
                    sessionDescription += String.Format(" :: {0}", roomToken.ArbitraryData);
                }

                object prop_type = -1;
                object prop_map  = -1;

                if (photonSession.Properties.ContainsKey("t"))
                {
                    prop_type = photonSession.Properties["t"];
                }

                if (photonSession.Properties.ContainsKey("m"))
                {
                    prop_map = photonSession.Properties["m"];
                }

                sessionDescription += String.Format(" :: {0} / {1}", prop_type, prop_map);

                if (GUILayout.Button(sessionDescription, GUILayout.ExpandWidth(true)))
                {
                    ServerConnectToken connectToken = new ServerConnectToken
                    {
                        data = "ConnectTokenData"
                    };

                    BoltNetwork.Connect(photonSession, connectToken);
                }
            }
        }
Ejemplo n.º 2
0
        void OnGUI()
        {
            GUILayout.BeginArea(new Rect(10, 10, Screen.width - 20, Screen.height - 20));

            switch (_state)
            {
            // starting Bolt is the same regardless of the transport layer
            case State.SelectMode:
                if (GUILayout.Button("Start Server", GUILayout.ExpandWidth(true)))
                {
                    BoltLauncher.StartServer();
                    _state = State.ModeServer;
                }

                if (GUILayout.Button("Start Client", GUILayout.ExpandWidth(true)))
                {
                    BoltLauncher.StartClient();
                    _state = State.ModeClient;
                }

                break;

            // Publishing a session into the matchmaking server
            case State.ModeServer:
                if (BoltNetwork.IsRunning && BoltNetwork.IsServer)
                {
                    if (GUILayout.Button("Publish HostInfo And Load Map", GUILayout.ExpandWidth(true)))
                    {
                        RoomProtocolToken token = new RoomProtocolToken()
                        {
                            ArbitraryData = "My DATA",
                            password      = "******"
                        };

                        // Uncomment if you want to pass custom properties into your room
                        // This is just an example data
                        //PhotonCloudRoomProperties token = new PhotonCloudRoomProperties();
                        //properties.AddRoomProperty("t", 1);
                        //properties.AddRoomProperty("m", 4);

                        string matchName = "MyPhotonGame #" + UnityEngine.Random.Range(1, 100);

                        BoltNetwork.SetServerInfo(matchName, token);
                        BoltNetwork.LoadScene("Level1");
                    }
                }
                break;

            // for the client, after Bolt is innitialized, we should see the list
            // of available sessions and join one of them
            case State.ModeClient:

                if (BoltNetwork.IsRunning && BoltNetwork.IsClient)
                {
                    GUILayout.Label("Session List");

                    foreach (var session in BoltNetwork.SessionList)
                    {
                        // Simple session
                        UdpSession udpSession = session.Value as UdpSession;

                        // Skip if is not a Photon session
                        if (udpSession.Source != UdpSessionSource.Photon)
                        {
                            continue;
                        }

                        // Photon Session
                        PhotonSession photonSession = udpSession as PhotonSession;

                        string sessionDescription = String.Format("{0} / {1} ({2})",
                                                                  photonSession.Source, photonSession.HostName, photonSession.Id);

                        RoomProtocolToken token = photonSession.GetProtocolToken() as RoomProtocolToken;

                        if (token != null)
                        {
                            sessionDescription += String.Format(" :: {0}", token.ArbitraryData);
                        }
                        else
                        {
                            object value_t = -1;
                            object value_m = -1;

                            if (photonSession.Properties.ContainsKey("t"))
                            {
                                value_t = photonSession.Properties["t"];
                            }

                            if (photonSession.Properties.ContainsKey("m"))
                            {
                                value_t = photonSession.Properties["m"];
                            }

                            sessionDescription += String.Format(" :: {0}/{1}", value_t, value_m);
                        }

                        if (GUILayout.Button(sessionDescription, GUILayout.ExpandWidth(true)))
                        {
                            ServerConnectToken connectToken = new ServerConnectToken
                            {
                                data = "ConnectTokenData"
                            };

                            BoltNetwork.Connect(photonSession, connectToken);
                        }
                    }
                }
                break;
            }

            GUILayout.EndArea();
        }