Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by HuskyPanda213 · Sep 09, 2013 at 05:24 PM · networkhostmigration

Host migration or so?

I would like to add host migration in my game, but it needs to have it that the level is NOT reset. Also I have read one that every answer I saw referred to. Though posting a chunk of code would be really useful I will provide my network code below.

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class NetworkMenu : MonoBehaviour {

 public string CurrentMenu = "";
 
 public string servername = "Server Name";
 
 public int Port = 24444;
 
 public List<MPPlayer> PlayerList = new List<MPPlayer>();
 
 public GameObject Spawnpoint;
 
 public GameObject Spawnedpl;
 
 public bool matchstarted;
 
 public bool Connected;
 
 public string _Playername;
 
 public static NetworkMenu ins;
 
 public GameObject networkScoreManager;
 
 public ScoreManager score;
 
 public bool _start;
 
 public GUIStyle text_style;
 
 
 public GUIStyle gui_Style;
 
 void Awake(){
     ins = this;
     NextMenu("Main");
     networkScoreManager = GameObject.FindGameObjectWithTag("ScoreManager");
     score = networkScoreManager.GetComponent<ScoreManager>();
     _Playername = PlayerPrefs.GetString("PlayerName");
 }
 
 
 
 void Start(){
     Set_GUI_Style();
 }
 
 private void Set_GUI_Style(){
     gui_Style.alignment = TextAnchor.MiddleCenter;
     text_style.alignment = TextAnchor.MiddleCenter;
 }
 
 void OnGUI(){
     if(CurrentMenu == "Main"){
     GUIMain();
     }
     if(CurrentMenu == "Start"){
     GUIStart();
     }
     if(CurrentMenu == "Browser"){
     GUIBrowser();
     }
     Lobby();
 }
 
 
 //GUI
 private void GUIMain(){
 if(!Network.isClient && !Network.isServer){
         
 if(GUI.Button(new Rect(10,10,200,25),"Browser",gui_Style)){
     NextMenu("Browser");
     MasterServer.RequestHostList("354545Battle");
 }
 if(GUI.Button(new Rect(10,40,200,25),"Host",gui_Style)){
     NextMenu("Start");
 }
 _Playername = GUI.TextArea(new Rect(10,70,200,25),_Playername,15,text_style);
 if(GUI.Button(new Rect(10,100,200,25),"Save Name",gui_Style)){
 PlayerPrefs.SetString("PlayerName", _Playername);            
 }
 if(GUI.Button(new Rect(10,130,200,25),"Quit",gui_Style)){
     Application.Quit();
 }
 }
 }
 
 private void GUIStart(){
 //Starts The server
 if(!Network.isClient && !Network.isServer){
 if(GUI.Button(new Rect(10,10,200,25),"Start Server",gui_Style)){
 StartServer();
 Debug.Log("Started Server with name" + servername);
 }
 servername = GUI.TextArea(new Rect(10,40,200,25),servername,20,text_style);
 if(GUI.Button(new Rect(10,70,200,25),"Back",gui_Style)){
 NextMenu("Main");            
 }
 }
 }
 
 void Update(){
 //Allows for starting
 if(Network.isServer){
 if(Network.connections.Length >= 0 && !matchstarted){
 //Server_SendSettings();
 //Spawn();
 _start = true;
 }
 }
 }
 
 public void Server_SendSettings(){
 networkView.RPC("Player_LoadSettings",RPCMode.All,true);
 networkScoreManager.networkView.RPC("FoundAllPlayers",RPCMode.All,true,Network.connections.Length + 1);
 Debug.Log("Sending Match Settings");    
 }
 
 public void Lobby(){
 //Starts The match
 if(Network.isServer && !matchstarted){
 if(_start){
 if(GUI.Button(new Rect(10,10,200,25),"Start Match",gui_Style)){
 Server_SendSettings();
 Network.maxConnections = 0;
 }
 if(GUI.Button(new Rect(10,40,200,25),"End Match",gui_Style)){
 Network.Disconnect();
 Application.LoadLevel(0);
 }
 }        
 }
 }    
 
 private void GUIBrowser(){
 if(!Network.isClient && !Network.isServer){
 if(GUI.Button(new Rect(10,10,200,25),"Back",gui_Style)){
     NextMenu("Main");
 }
 if(GUI.Button(new Rect(10,40,200,25),"Refresh",gui_Style)){
     MasterServer.RequestHostList("354545Battle");
 }
 GUILayout.BeginArea(new Rect(Screen.width / 2, 0,Screen.width / 2,Screen.height),"Server List","box");
 GUILayout.Space(20);
 foreach(HostData hd in MasterServer.PollHostList()){
 GUILayout.BeginHorizontal();
 GUILayout.Label(hd.gameName);
 if(hd.connectedPlayers < hd.playerLimit){
 GUILayout.Label(hd.connectedPlayers + "/" + hd.playerLimit);
 }
 else{
 GUILayout.Label("Match is full or has started");                
 }
 if(GUILayout.Button("Connect")){
 Network.Connect(hd);
 }
 GUILayout.EndHorizontal();
 }
 GUILayout.EndArea();
 }
 }
 
 void OnPlayerConnected(NetworkPlayer player){
 }
 
 void OnConnectedToServer(){
 Debug.Log("Connecting");
 networkView.RPC("PlayerConnectedToServer",RPCMode.Others,_Playername);
 if(matchstarted){
 Network.Disconnect();
 Application.LoadLevel(0);
 }
 Debug.Log("Done");
 }
 
 public void PlayerLeave(GameObject player){
     Network.Destroy(player);
     networkScoreManager.networkView.RPC("PlayerLeft",RPCMode.All,_Playername);
 }
 
 public void HostQuit(GameObject host){
     networkView.RPC("KickAllPlayers",RPCMode.All);
 }
 
 void OnServerInitialized(){
     
 }
 
 
 public void StartServer(){
     Network.InitializeServer(15,Port,false);
 //    Network.InitializeSecurity();
     MasterServer.RegisterHost("354545Battle",servername,"Hosted Game");
 }
 
 public void Spawn(){
 GameObject PlayerSpawned = Network.Instantiate(Spawnedpl,Spawnpoint.transform.position,Quaternion.identity,1) as GameObject;
 PlayerSpawned.GetComponent<NetworkManager>()._name = _Playername;
 }
 
 //Next Menu Void
 public void NextMenu(string nxt){
     CurrentMenu = nxt;
 }
 
 void OnDisconnectedFromServer(){
     Application.LoadLevel(0);
 }
 
 void OnPlayerDisconnected(NetworkPlayer id){
     Network.DestroyPlayerObjects(id);
     Network.RemoveRPCs(id);
 }
 
 
 
 //RPCs
 
 [RPC]
 void Player_Joined(){
     
 }
 
 [RPC]
 void Player_LoadSettings(bool _matchstarted){
 matchstarted = _matchstarted;
 Debug.Log("Got Settings!");
 Spawn();
 }
 

}

public class MPPlayer{ public string PlayerName; public NetworkPlayer PlayerNetwork; }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Automatic Network Migration Manager 0 Answers

Create server without port 0 Answers

What is the easiest way to set up a LAN-game? 0 Answers

Host Migration ? 1 Answer

How can i acess the local Player from Script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges