/// <summary> /// 初始化创建主服务器. /// </summary> public void InitCreateServer() { if (NetworkRootMovie.GetInstance() != null) { //循环动画场景间隔一定时间主动创建服务器. if (Time.time - TimeLastCreatServer < 9f) { //Debug.Log("InitCreateServer -> test..."); return; } TimeLastCreatServer = Time.time; if (NetworkRootMovie.GetInstance().ePlayerGameNetState == PlayerGameNetType.Null) { StartCoroutine(DelayInitCreateServer((Random.Range(0, 100) % 4) * 3)); } else { Debug.Log("InitCreateServer -> player into Game!"); } return; } else { if (mNetworkRootGame != null) { //非循环动画场景直接创建服务器. if (mNetworkRootGame.ePlayerGameNetState == PlayerGameNetType.Null && ePlayerPortState == PlayerPortType.Server) { IsCreateServer = false; } } } }
public void Init(Loading loadingCom) { bool isActiveStartBt = false; mLoadingCom = loadingCom; switch (NetworkRootMovie.GetInstance().eNetState) { case NetworkRootMovie.GameNetType.NoLink: { isActiveStartBt = true; break; } case NetworkRootMovie.GameNetType.Link: { if (loadingCom.mGameModeSelect.eGameMode == NetworkRootMovie.GameMode.Link) { isActiveStartBt = true; } else { isActiveStartBt = true; } break; } } SetActiveStartBt(isActiveStartBt); for (int i = 0; i < 4; i++) { if (YiWanChengUI[i] != null) { if (GlobalData.GetInstance().YiWanChengLvList.Contains(i + 1)) { YiWanChengUI[i].SetActive(true); } else { YiWanChengUI[i].SetActive(false); } } } gameObject.SetActive(true); if (loadingCom.mGameLinkPlayer != null) { loadingCom.mGameLinkPlayer.SetAcitveStartBt(false); loadingCom.mGameLinkPlayer.SetActiveLinkNameParent(false); } }
void OnMasterServerEvent(MasterServerEvent msEvent) { //Debug.Log("OnMasterServerEvent: " + msEvent + ", time " + Time.time); if (msEvent == MasterServerEvent.RegistrationSucceeded) { Debug.Log("MasterServer registered, GameLevel " + (GameScene)Application.loadedLevel); if ((GameScene)Application.loadedLevel == GameScene.Movie) { //只在循环动画场景执行! //ServerLinkInfo.GetInstance().HiddenServerLinkInfo(); NetworkRootMovie.GetInstance().mNetworkRpcMsgSpawn.CreateNetworkRpc(); } } }
/// <summary> /// 删除当前客户端. /// </summary> public void RemoveClientHost(NetworkDisconnection info = NetworkDisconnection.LostConnection) { if (Network.peerType == NetworkPeerType.Client) { if (NetworkRootMovie.GetInstance().mNetworkRpcMsgScript != null) { NetworkRootMovie.GetInstance().mNetworkRpcMsgScript.RemoveSelf(); } if (info == NetworkDisconnection.LostConnection) { Network.Disconnect(); } } }
void OnFailedToConnectToMasterServerEvent() { if (!IsFailedToConnectMasterServer) { Debug.Log("GameLinkPlayer -> OnFailedToConnectToMasterServerEvent..."); IsFailedToConnectMasterServer = true; //因为网络故障强制切换为单机模式. NetworkRootMovie.GetInstance().ePlayerSelectGameMode = NetworkRootMovie.GameMode.NoLink; SetActiveUISeverWeiHu(true); SetActiveUICreateSever(false); SetActiveLinkNameParent(false); SetAcitveStartBt(false); mSSTimeUpCom = gameObject.AddComponent <SSTimeUpCtrl>(); mSSTimeUpCom.OnTimeUpOverEvent += OnTimeUpOverServerWeiHuEvent; mSSTimeUpCom.Init(5f); } }
void ClickSetEnterBtEvent(InputEventCtrl.ButtonState val) { if (val == InputEventCtrl.ButtonState.DOWN) { return; } if (m_HasBegin) { return; } if (NetworkRootMovie.GetInstance().ePlayerSelectGameMode == NetworkRootMovie.GameMode.Link) { //选择联机游戏后不允许进入设置界面. return; } if (IsLoadingSetPanel) { return; } IsLoadingSetPanel = true; if (NetworkServerNet.GetInstance() != null) { NetworkServerNet.GetInstance().mRequestMasterServer.SetIsNetScene(false); switch (Network.peerType) { case NetworkPeerType.Server: { NetworkServerNet.GetInstance().RemoveMasterServerHost(); break; } case NetworkPeerType.Client: { NetworkServerNet.GetInstance().RemoveClientHost(); break; } } } StartCoroutine(DelayLoadingSetPanel()); }
void OnDisconnectedFromServer(NetworkDisconnection info) { Debug.Log("OnDisconnectedFromServer -> info is " + info + ", peerType " + Network.peerType); switch (info) { case NetworkDisconnection.Disconnected: { if (Network.peerType == NetworkPeerType.Client) { RemoveClientHost(NetworkDisconnection.Disconnected); if (NetworkRootMovie.GetInstance() != null) { //循环动画场景中,玩家是客户端时需要重置masterServer信息. NetworkEvent.GetInstance().OnFailedToConnectToMasterServerTrigger(); } } break; } } }
void OnPlayerConnected(NetworkPlayer playerNet) { Debug.Log("NetworkServerNet::OnPlayerConnected -> ip " + playerNet.ipAddress + ", appGameLevel " + Application.loadedLevel); if (NetworkRootMovie.GetInstance() != null) { //循环动画场景. if (NetworkRootMovie.GetInstance().mNetworkRpcMsgScript != null) { NetworkRootMovie.GetInstance().mNetworkRpcMsgScript.NetSetSpawnPlayerIndex(playerNet, Network.connections.Length); } LinkServerPlayerNum_Movie = Network.connections.Length; } if (mNetworkRootGame != null) { //游戏场景. LinkServerPlayerNum_Game = Network.connections.Length; NetworkEvent.GetInstance().OnPlayerConnectedTrigger(); } }
void OnServerInitialized() { Debug.Log("OnServerInitialized -> appLevel " + Application.loadedLevel); IsCreateServer = true; if (NetworkRootMovie.GetInstance() != null) { //循环动画场景. LinkServerPlayerNum_Movie = 0; //初始化. LinkServerPlayerNum_Game = 0; //重置. SetIndexSpawnPlayer(0); NetworkRootMovie.GetInstance().CreateNetworkRpc(); } if (mNetworkRootGame != null) { //游戏场景. SetIndexSpawnPlayer(0); NetworkEvent.GetInstance().OnServerInitializedTrigger(); } }
IEnumerator OnNetCallPlayerIntoGame(int level) { float timeVal = Network.peerType == NetworkPeerType.Server ? 3f : 0f; mLoadSceneCount = level; mGameLinkPlayer.OnClickStartBt(); yield return(new WaitForSeconds(timeVal)); if (m_IsBeginOk && !m_HasBegin) { //开始联机游戏. Debug.Log("Start link game, timeVal == " + timeVal + ", peerType " + Network.peerType); SSGameCtrl.GetInstance().eGameMode = NetworkRootMovie.GameMode.Link; NetworkRootMovie.GetInstance().ePlayerGameNetState = NetworkServerNet.PlayerGameNetType.MovieIntoGame; if (NetworkServerNet.GetInstance() != null) { NetworkServerNet.GetInstance().mRequestMasterServer.SetMasterServerComment(RequestMasterServer.MasterServerComment.GameNet); if (Network.peerType == NetworkPeerType.Server) { NetworkServerNet.GetInstance().ePlayerPortState = NetworkServerNet.PlayerPortType.Server; NetworkServerNet.GetInstance().RemoveMasterServerHost(); } if (Network.peerType == NetworkPeerType.Client) { NetworkServerNet.GetInstance().ePlayerPortState = NetworkServerNet.PlayerPortType.Client; NetworkServerNet.GetInstance().RemoveClientHost(); } } m_BeginSource.Play(); m_Loading.SetActive(true); timmerstar = true; m_HasBegin = true; } }
void OnClickBeginBt() { switch (NetworkRootMovie.GetInstance().eNetState) { case NetworkRootMovie.GameNetType.Link: { if (mGameModeSelect == null) { if (GameMode == ReadGameInfo.GameMode.Oper.ToString()) { if (m_InserNum >= Convert.ToInt32(CoinNumSet)) { //创建游戏模式选择UI界面. SpawnGameModeUI(); m_BeginSource.Play(); m_IsStartGame = true; m_InserNum -= Convert.ToInt32(CoinNumSet); UpdateInsertCoin(); ReadGameInfo.GetInstance().WriteInsertCoinNum(m_InserNum.ToString()); if (pcvr.bIsHardWare) { pcvr.GetInstance().mPcvrTXManage.SubPlayerCoin(Convert.ToInt32(CoinNumSet), pcvrTXManage.PlayerCoinEnum.player01); } m_Tishi.SetActive(false); } } else { //创建游戏模式选择UI界面. SpawnGameModeUI(); m_BeginSource.Play(); m_IsStartGame = true; m_Tishi.SetActive(false); } } else { if (mGameModeSelect.eGameMode == NetworkRootMovie.GameMode.NoLink) { //玩家选择单机游戏. if (mLevelSelectUI == null) { //产生选择游戏场景UI. SpawnLevelSelectUI(); m_BeginSource.Play(); m_IsStartGame = true; m_Tishi.SetActive(false); mGameModeSelect.HiddenSelf(); } else { if (m_IsBeginOk && !m_HasBegin) { m_BeginSource.Play(); m_Loading.SetActive(true); timmerstar = true; m_HasBegin = true; mLevelSelectUI.HiddenSelf(); SSGameCtrl.GetInstance().eGameMode = NetworkRootMovie.GameMode.NoLink; NetworkRootMovie.GetInstance().ePlayerSelectGameMode = NetworkRootMovie.GameMode.NoLink; NetworkServerNet.GetInstance().mRequestMasterServer.SetIsNetScene(false); NetworkServerNet.GetInstance().RemoveMasterServerHost(); } } } if (mGameModeSelect.eGameMode == NetworkRootMovie.GameMode.Link) { //玩家选择联机游戏. if (mGameLinkPlayer == null) { NetworkRootMovie.GetInstance().ePlayerSelectGameMode = NetworkRootMovie.GameMode.Link; m_BeginSource.Play(); mGameModeSelect.HiddenSelf(); SpawnGameLinkPlayerUI(); } else { if (mLevelSelectUI.StartBtObj.activeInHierarchy) { //联机游戏,主服务器选择游戏关卡. mLevelSelectUI.HiddenSelf(); mGameLinkPlayer.SetActiveLinkNameParent(true); mGameLinkPlayer.SetAcitveStartBt(true); //mGameLinkPlayer.ChangeUINameScale(0); mGameLinkPlayer.SetUIPanelBeiJing(0); } else { if (mGameLinkPlayer.StartBtObj.activeInHierarchy) { //发送网络消息-开始联机游戏. NetworkRootMovie.GetInstance().mNetworkRpcMsgScript.NetSendLoadLevel(mLevelSelectUI.mSelectLevel); } } } } } break; } case NetworkRootMovie.GameNetType.NoLink: { if (PlayerControllerForMoiew.IsLoadMovieLevel) { return; } if (mLevelSelectUI == null) { if (GameMode == ReadGameInfo.GameMode.Oper.ToString()) { //运营模式. if (m_InserNum >= Convert.ToInt32(CoinNumSet)) { //产生选择游戏场景UI. SpawnLevelSelectUI(); m_BeginSource.Play(); m_IsStartGame = true; m_InserNum -= Convert.ToInt32(CoinNumSet); UpdateInsertCoin(); ReadGameInfo.GetInstance().WriteInsertCoinNum(m_InserNum.ToString()); if (pcvr.bIsHardWare) { pcvr.GetInstance().mPcvrTXManage.SubPlayerCoin(Convert.ToInt32(CoinNumSet), pcvrTXManage.PlayerCoinEnum.player01); } m_Tishi.SetActive(false); } } else { //免费模式. //产生选择游戏场景UI. SpawnLevelSelectUI(); m_BeginSource.Play(); m_IsStartGame = true; m_Tishi.SetActive(false); } } else { if (m_IsBeginOk && !m_HasBegin) { mLevelSelectUI.HiddenSelf(); m_BeginSource.Play(); m_Loading.SetActive(true); timmerstar = true; m_HasBegin = true; SSGameCtrl.GetInstance().eGameMode = NetworkRootMovie.GameMode.NoLink; } } break; } } }
void TryToCreateServer() { if (NetworkRootMovie.GetInstance() != null) { //循环动画场景. if (NetworkRootMovie.GetInstance().ePlayerSelectGameMode != NetworkRootMovie.GameMode.Link) { //只有当玩家选择了联机游戏时,才允许创建主服务器. return; } if (NetworkRootMovie.GetInstance().ePlayerGameNetState == PlayerGameNetType.MovieIntoGame) { //如果循环动画正在加载游戏场景时,不允许创建服务器. return; } } if (mNetworkRootGame != null) { //游戏场景 if (mNetworkRootGame.ePlayerGameNetState == PlayerGameNetType.GameBackMovie) { //如果游戏场景正在加载循环动画时,不允许创建服务器. return; } if (ePlayerPortState != PlayerPortType.Server) { //不是Server端的不允许创建服务器. return; } } if (IsCreateServer) { return; } IsCreateServer = true; //初始化MasterServer. MasterServer.ipAddress = MasterServerIp; Network.natFacilitatorIP = MasterServerIp; Debug.Log("start create server, time " + Time.time.ToString("f2") + ", ePlayerPortState " + ePlayerPortState + ", level " + Application.loadedLevel); Network.InitializeServer(3, mPort, true); Debug.Log("masterServer.ip " + MasterServer.ipAddress + ", port " + MasterServer.port + ", updateRate " + MasterServer.updateRate); MasterServer.dedicatedServer = true; switch (mRequestMasterServer.eMasterComment) { case RequestMasterServer.MasterServerComment.Movie: mRequestMasterServer.SetMasterServerIp(Network.player.ipAddress); MasterServer.RegisterHost(mGameTypeName, "My game", mRequestMasterServer.MasterServerMovieComment); break; case RequestMasterServer.MasterServerComment.GameNet: MasterServer.RegisterHost(mGameTypeName, "My game", mRequestMasterServer.MasterServerGameNetComment); break; } }
void Awake() { NetworkRootMovie.GetInstance().mNetworkRpcMsgScript = this; }
void OnGUI() { HostData[] data = MasterServer.PollHostList(); if (data.Length < 0) { return; } foreach (var element in data) { #if SHOW_NET_INFO var name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit; GUILayout.BeginHorizontal(); GUILayout.Box(name); GUILayout.Space(5); var hostInfo = "["; foreach (var host in element.ip) { hostInfo = hostInfo + host + ":" + element.port + " "; } hostInfo = hostInfo + "]"; GUILayout.Box(hostInfo); GUILayout.Space(5); GUILayout.Box(element.comment); GUILayout.Space(5); GUILayout.FlexibleSpace(); #endif if (Network.peerType == NetworkPeerType.Disconnected && IsNetScene) { if (!IsClickConnectServer) { bool isConnectServer = false; if (NetworkServerNet.GetInstance().mNetworkRootGame != null && NetworkServerNet.GetInstance().ePlayerPortState == NetworkServerNet.PlayerPortType.Client && element.comment == MasterServerGameNetComment && element.ip[0] != Network.player.ipAddress && ServerIp == element.ip[0]) { //游戏场景中. if (NetworkServerNet.GetInstance().mNetworkRootGame.ePlayerGameNetState == NetworkServerNet.PlayerGameNetType.GameBackMovie) { //如果游戏场景正在加载循环动画时,不允许链接服务器. } else { if (Time.realtimeSinceStartup - TimeConnectServer > RandConnectTime) { isConnectServer = true; TimeConnectServer = Time.realtimeSinceStartup; RandConnectTime = (Random.Range(0, 100) % 5) + 3f; } } } if (NetworkRootMovie.GetInstance() != null && NetworkRootMovie.GetInstance().ePlayerSelectGameMode == NetworkRootMovie.GameMode.Link && element.comment == MasterServerMovieComment && element.ip[0] != Network.player.ipAddress && element.connectedPlayers < element.playerLimit) { //循环动画场景中. if (NetworkRootMovie.GetInstance().ePlayerGameNetState == NetworkServerNet.PlayerGameNetType.MovieIntoGame) { //如果循环动画正在加载游戏场景时,不允许链接服务器. } else { if (Time.realtimeSinceStartup - TimeConnectServer > RandConnectTime) { isConnectServer = true; TimeConnectServer = Time.realtimeSinceStartup; RandConnectTime = (Random.Range(0, 100) % 5) + 3f; } } } if (isConnectServer) { if (Network.peerType == NetworkPeerType.Client || Network.peerType == NetworkPeerType.Server) { //清理网络垃圾信息. Network.RemoveRPCs(Network.player); Network.DestroyPlayerObjects(Network.player); } MasterServer.dedicatedServer = false; Network.Connect(element); IsClickConnectServer = true; if (NetworkRootMovie.GetInstance() != null) { //循环动画场景中. SetMasterServerIp(element.ip[0]); TimeConnect = 0f; } Debug.Log("Connect ip -> " + element.ip[0] + ", comment " + element.comment + ", time " + Time.time.ToString("f2")); } } else { if (NetworkServerNet.GetInstance().mNetworkRootGame != null) { //游戏场景中. if (element.comment == MasterServerGameNetComment && ServerIp == element.ip[0]) { TimeConnect += Time.deltaTime; if (TimeConnect >= 2f) { TimeConnect = 0f; IsClickConnectServer = false; } } } if (NetworkRootMovie.GetInstance() != null) { //循环动画场景中. TimeConnect += Time.deltaTime; if (TimeConnect >= 2f) { TimeConnect = 0f; IsClickConnectServer = false; } } } } #if SHOW_NET_INFO GUILayout.EndHorizontal(); #endif } }
void CheckMasterServerList() { int masterServerNum = 0; bool isCreatMasterServer = true; HostData[] data = MasterServer.PollHostList(); if (NetworkRootMovie.GetInstance() != null && data.Length > 0) { //循环动画场景. foreach (var element in data) { if (element.comment == MasterServerMovieComment) { masterServerNum++; if (Network.peerType == NetworkPeerType.Disconnected) { if (masterServerNum > 0) { isCreatMasterServer = false; } } else if (Network.peerType == NetworkPeerType.Server) { if (masterServerNum > 1 && Random.Range(0, 100) % 2 == 1) { //随机删除1个循环动画场景的masterServer. isCreatMasterServer = false; Debug.Log("random remove masterServer..."); } } } } } if (!IsNetScene) { //不需要网络链接的游戏场景中不进行主服务器MasterServer的创建. isCreatMasterServer = false; } switch (Network.peerType) { case NetworkPeerType.Disconnected: { if (isCreatMasterServer) { if (NetworkRootMovie.GetInstance() != null) { //循环动画场景. if (NetworkRootMovie.GetInstance().ePlayerSelectGameMode != NetworkRootMovie.GameMode.Link) { //只有当玩家选择了联机游戏时,才允许创建主服务器. return; } if (NetworkRootMovie.GetInstance().ePlayerGameNetState == NetworkServerNet.PlayerGameNetType.MovieIntoGame) { //如果循环动画正在加载游戏场景时,不允许创建服务器. return; } } NetworkServerNet.GetInstance().InitCreateServer(); } break; } case NetworkPeerType.Server: { if (!isCreatMasterServer) { NetworkServerNet.GetInstance().RemoveMasterServerHost(); } break; } } }