Beispiel #1
0
        /// <summary>
        /// virtual so that inheriting classes' OnValidate() can call base.OnValidate() too
        /// </summary>
        public virtual void OnValidate()
        {
            // add transport if there is none yet. makes upgrading easier.
            if (transport == null)
            {
                // was a transport added yet? if not, add one
                transport = GetComponent <AsyncTransport>();
                if (transport == null)
                {
                    transport = gameObject.AddComponent <AsyncTcpTransport>();
                    Debug.Log("NetworkManager: added default Transport because there was none yet.");
                }
#if UNITY_EDITOR
                UnityEditor.Undo.RecordObject(gameObject, "Added default Transport");
#endif
            }

            // add NetworkServer if there is none yet. makes upgrading easier.
            if (GetComponent <NetworkServer>() == null)
            {
                server = gameObject.AddComponent <NetworkServer>();
                Debug.Log("NetworkManager: added NetworkServer because there was none yet.");
#if UNITY_EDITOR
                UnityEditor.Undo.RecordObject(gameObject, "Added NetworkServer");
#endif
            }

            // add NetworkClient if there is none yet. makes upgrading easier.
            if (GetComponent <NetworkClient>() == null)
            {
                client = gameObject.AddComponent <NetworkClient>();
                Debug.Log("NetworkManager: added NetworkClient because there was none yet.");
#if UNITY_EDITOR
                UnityEditor.Undo.RecordObject(gameObject, "Added NetworkClient");
#endif
            }
        }
Beispiel #2
0
        internal static void OnAnimationTriggerServerMessage(NetworkMessage netMsg)
        {
            AnimationTriggerMessage msg = netMsg.ReadMessage <AnimationTriggerMessage>();

            if (LogFilter.Debug)
            {
                Debug.Log("OnAnimationTriggerMessage for netId=" + msg.netId + " conn=" + netMsg.conn);
            }

            GameObject go = NetworkServer.FindLocalObject(msg.netId);

            if (go == null)
            {
                return;
            }
            NetworkAnimator animSync = go.GetComponent <NetworkAnimator>();

            if (animSync != null)
            {
                animSync.HandleAnimTriggerMsg(msg.hash);

                NetworkServer.SendToReady(go, (short)MsgType.AnimationTrigger, msg);
            }
        }
        /// <summary>
        /// ここがシーン跨いで呼ばれる
        /// </summary>
        /// <param name="newSceneName"></param>
        public virtual void ServerChangeScene(string newSceneName)
        {
            if (string.IsNullOrEmpty(newSceneName))
            {
                Debug.LogError("ServerChangeScene empty scene name");
                return;
            }

            if (LogFilter.Debug)
            {
                Debug.Log("ServerChangeScene " + newSceneName);
            }
            NetworkServer.SetAllClientsNotReady();
            networkSceneName = newSceneName;

            loadingSceneAsync = SceneManager.LoadSceneAsync(newSceneName);

            SceneMessage msg = new SceneMessage(networkSceneName);

            NetworkServer.SendToAll(msg);

            startPositionIndex = 0;
            startPositions.Clear();
        }
Beispiel #4
0
        internal static void OnAnimationParametersServerMessage(NetworkMessage netMsg)
        {
            AnimationParametersMessage msg = netMsg.ReadMessage <AnimationParametersMessage>();

            if (LogFilter.Debug)
            {
                Debug.Log("OnAnimationParametersMessage for netId=" + msg.netId + " conn=" + netMsg.conn);
            }

            GameObject go = NetworkServer.FindLocalObject(msg.netId);

            if (go == null)
            {
                return;
            }
            NetworkAnimator animSync = go.GetComponent <NetworkAnimator>();

            if (animSync != null)
            {
                NetworkReader reader = new NetworkReader(msg.parameters);
                animSync.HandleAnimParamsMsg(msg, reader);
                NetworkServer.SendToReady(go, (short)MsgType.AnimationParameters, msg);
            }
        }
        internal static void ConnectHost()
        {
            if (LogFilter.Debug)
            {
                Debug.Log("Client Connect Host to Server");
            }

            RegisterSystemHandlers(true);

            connectState = ConnectState.Connected;

            // create local connection objects and connect them
            ULocalConnectionToServer connectionToServer = new ULocalConnectionToServer();
            ULocalConnectionToClient connectionToClient = new ULocalConnectionToClient();

            connectionToServer.connectionToClient = connectionToClient;
            connectionToClient.connectionToServer = connectionToServer;

            connection = connectionToServer;
            connection.SetHandlers(handlers);

            // create server connection to local client
            NetworkServer.SetLocalConnection(connectionToClient);
        }
Beispiel #6
0
        void SceneLoadedForPlayer(NetworkConnection conn, GameObject roomPlayer)
        {
            if (LogFilter.Debug)
            {
                Debug.LogFormat("NetworkRoom SceneLoadedForPlayer scene: {0} {1}", SceneManager.GetActiveScene().path, conn);
            }

            if (IsSceneActive(RoomScene))
            {
                // cant be ready in room, add to ready list
                PendingPlayer pending;
                pending.conn       = conn;
                pending.roomPlayer = roomPlayer;
                pendingPlayers.Add(pending);
                return;
            }

            GameObject gamePlayer = OnRoomServerCreateGamePlayer(conn, roomPlayer);

            if (gamePlayer == null)
            {
                // get start position from base class
                Transform startPos = GetStartPosition();
                gamePlayer = startPos != null
                    ? Instantiate(playerPrefab, startPos.position, startPos.rotation)
                    : Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
            }

            if (!OnRoomServerSceneLoadedForPlayer(conn, roomPlayer, gamePlayer))
            {
                return;
            }

            // replace room player with game player
            NetworkServer.ReplacePlayerForConnection(conn, gamePlayer, true);
        }
Beispiel #7
0
        /// <summary>
        /// Disconnect from server.
        /// <para>The disconnect message will be invoked.</para>
        /// </summary>
        public static void Disconnect()
        {
            connectState = ConnectState.Disconnected;
            ClientScene.HandleClientDisconnect(connection);

            // local or remote connection?
            if (isLocalClient)
            {
                if (isConnected)
                {
                    NetworkServer.localConnection.Send(new DisconnectMessage());
                }
                NetworkServer.RemoveLocalConnection();
            }
            else
            {
                if (connection != null)
                {
                    connection.Disconnect();
                    connection = null;
                    RemoveTransportHandlers();
                }
            }
        }
Beispiel #8
0
        public override void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage extraMessage)
        {
            Debug.Log("OnServerAddPlayer");
            if (SceneManager.GetActiveScene().name != LobbyScene)
            {
                Debug.Log("lobby じゃなかったので帰る");
                return;
            }

            if (lobbySlots.Count == maxConnections)
            {
                return;
            }

            allPlayersReady = false;

            if (LogFilter.Debug)
            {
                Debug.LogFormat("NetworkLobbyManager.OnServerAddPlayer playerPrefab:{0}", lobbyPlayerPrefab.name);
            }

            GameObject newLobbyGameObject = OnLobbyServerCreateLobbyPlayer(conn);

            if (newLobbyGameObject == null)
            {
                newLobbyGameObject = (GameObject)Instantiate(lobbyPlayerPrefab.gameObject, Vector3.zero, Quaternion.identity);
            }

            NetworkLobbyPlayer newLobbyPlayer = newLobbyGameObject.GetComponent <NetworkLobbyPlayer>();

            lobbySlots.Add(newLobbyPlayer);

            RecalculateLobbyPlayerIndices();

            NetworkServer.AddPlayerForConnection(conn, newLobbyGameObject);
        }
 public virtual void OnServerDisconnect(NetworkConnection conn)
 {
     NetworkServer.DestroyPlayerForConnection(conn);
     if (LogFilter.Debug) Debug.Log("OnServerDisconnect: Client disconnected.");
 }
Beispiel #10
0
 /// <summary>
 /// connect host mode
 /// </summary>
 public static void ConnectLocalServer()
 {
     NetworkServer.OnConnected(NetworkServer.localConnection);
     NetworkServer.localConnection.Send(new ConnectMessage());
 }
 void RebuildTeamObservers(string teamId)
 {
     foreach (NetworkIdentity netIdentity in teamObjects[teamId])
         if (netIdentity != null)
             NetworkServer.RebuildObservers(netIdentity, false);
 }
Beispiel #12
0
        public bool StartServer()
        {
            InitializeSingleton();

            if (m_RunInBackground)
            {
                Application.runInBackground = true;
            }

            NetworkServer.useWebSockets = m_UseWebSockets;

            if (m_ServerBindToIP && !string.IsNullOrEmpty(m_ServerBindAddress))
            {
                if (!NetworkServer.Listen(m_ServerBindAddress, m_NetworkPort, m_MaxConnections))
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("StartServer listen on " + m_ServerBindAddress + " failed.");
                    }
                    return(false);
                }
            }
            else
            {
                if (!NetworkServer.Listen(m_NetworkPort, m_MaxConnections))
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("StartServer listen failed.");
                    }
                    return(false);
                }
            }

            // call OnStartServer AFTER Listen, so that NetworkServer.active is
            // true and we can call NetworkServer.Spawn in OnStartServer
            // overrides.
            // (useful for loading & spawning stuff from database etc.)
            //
            // note: there is no risk of someone connecting after Listen() and
            //       before OnStartServer() because this all runs in one thread
            //       and we don't start processing connects until Update.
            OnStartServer();

            // this must be after Listen(), since that registers the default message handlers
            RegisterServerMessages();

            if (LogFilter.logDebug)
            {
                Debug.Log("NetworkManager StartServer port:" + m_NetworkPort);
            }
            isNetworkActive = true;

            // Only change scene if the requested online scene is not blank, and is not already loaded
            string loadedSceneName = SceneManager.GetSceneAt(0).name;

            if (!string.IsNullOrEmpty(m_OnlineScene) && m_OnlineScene != loadedSceneName && m_OnlineScene != m_OfflineScene)
            {
                ServerChangeScene(m_OnlineScene);
            }
            else
            {
                NetworkServer.SpawnObjects();
            }
            return(true);
        }
Beispiel #13
0
 // this is invoked by the UnityEngine
 public static void UNetStaticUpdate()
 {
     NetworkServer.Update();
     NetworkClient.UpdateClients();
     NetworkManager.UpdateScene();
 }
 public override void OnStartServer()
 {
     base.OnStartServer();
     NetworkServer.RegisterHandler<CharacterSelectMessage>(OnCharacterSelectMessage);
 }
 static void NetworkLateUpdate()
 {
     //Debug.Log("NetworkLateUpdate @ " + Time.time);
     NetworkServer.NetworkLateUpdate();
     NetworkClient.NetworkLateUpdate();
 }
        public override void OnInspectorGUI()
        {
            if (m_ServerOnlyProperty == null)
            {
                m_Initialized = false;
            }

            Init();

            serializedObject.Update();

            if (m_ServerOnlyProperty.boolValue)
            {
                EditorGUILayout.PropertyField(m_ServerOnlyProperty, m_ServerOnlyLabel);
                EditorGUILayout.LabelField("Local Player Authority cannot be set for server-only objects");
            }
            else if (m_LocalPlayerAuthorityProperty.boolValue)
            {
                EditorGUILayout.LabelField("Server Only cannot be set for Local Player Authority objects");
                EditorGUILayout.PropertyField(m_LocalPlayerAuthorityProperty, m_LocalPlayerAuthorityLabel);
            }
            else
            {
                EditorGUILayout.PropertyField(m_ServerOnlyProperty, m_ServerOnlyLabel);
                EditorGUILayout.PropertyField(m_LocalPlayerAuthorityProperty, m_LocalPlayerAuthorityLabel);
            }

            serializedObject.ApplyModifiedProperties();

            if (!Application.isPlaying)
            {
                return;
            }

            // Runtime actions below here

            EditorGUILayout.Separator();

            if (m_NetworkIdentity.observers != null && m_NetworkIdentity.observers.Count > 0)
            {
                m_ShowObservers = EditorGUILayout.Foldout(m_ShowObservers, "Observers");
                if (m_ShowObservers)
                {
                    EditorGUI.indentLevel += 1;
                    foreach (NetworkConnection observer in m_NetworkIdentity.observers)
                    {
                        if (observer.playerController != null)
                        {
                            EditorGUILayout.ObjectField("Connection " + observer.connectionId, observer.playerController.gameObject, typeof(GameObject), false);
                        }
                        else
                        {
                            EditorGUILayout.TextField("Connection " + observer.connectionId);
                        }
                    }
                    EditorGUI.indentLevel -= 1;
                }
            }

            PrefabType prefabType = PrefabUtility.GetPrefabType(m_NetworkIdentity.gameObject);

            if (prefabType == PrefabType.Prefab)
            {
                return;
            }

            if (m_NetworkIdentity.gameObject.activeSelf && m_NetworkIdentity.netId.IsEmpty() && NetworkServer.active)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(m_SpawnLabel);
                if (GUILayout.Toggle(false, "Spawn", EditorStyles.miniButtonLeft))
                {
                    NetworkServer.Spawn(m_NetworkIdentity.gameObject);
                    EditorUtility.SetDirty(target);  // preview window STILL doens't update immediately..
                }
                EditorGUILayout.EndHorizontal();
            }
        }
Beispiel #17
0
        /**
         * When the server receives the add player command, it creates a dummy object of the player.
         * This is necessary so the client could begin receiving RPC calls.
         * The dummy player is replaced with the actual player in SpawnPlayerAfterRoundStart()
         */
        public override void OnServerAddPlayer(NetworkConnection conn)
        {
            GameObject player = Instantiate(playerDummyPrefab);

            NetworkServer.AddPlayerForConnection(conn, player);
        }