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 rsud · Dec 14, 2014 at 06:26 AM · multiplayerserversetup

setting up a server based game on my pc?

I am running windows 7 professional on my laptop.

Is it possible to setup a server multiplayer game instance of my unity game on my laptop?

I want to run the client on my laptop and connect it to the server that is running on my laptop.

I would also want other PCs or MACs to connect.

What do I need on my pc to run the server side? I see documentation but its in pieces and i dont find one that describe the whole setup process

Is there documentation that shows step by step to do this setup from the ground up from just having windows 7?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by honeywaffles · Dec 14, 2014 at 08:42 AM

It can be hard to find information on the in-built unity networking - here is a script from my canceled multiplayer game.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class NetworkManager : MonoBehaviour {
     public float connectionMaxTimeout = 10f;
     float currentTimeOut = 0f;
     bool connectionFailed = false;
     string connectionError = "";
     public GameObject playerPrefab;
     public GameObject myGO;
     private const string typeName = "anythinghere";
     private const string gameName = "RoomName";
     private HostData[] hostList;
     int state = 0;
     string serverName = "";
     string serverPassword = "";
     string joiningPassword = "";
     HostData serverToJoin;
     bool optionsOpen = false;
 
     public static bool singlePlayer = true;
     public static int playerCount = 0;
 
     List<Message> messages = new List<Message>();
 
     void Awake() {
         DontDestroyOnLoad (gameObject);
     }
 
     private void StartServer()
     {
         if ( serverPassword != "" ) { 
             Network.incomingPassword = serverPassword;
         }
         Network.InitializeServer(4, 25000, true);
         MasterServer.RegisterHost(typeName, serverName);
     }
 
     void OnServerInitialized()
     {
         Network.isMessageQueueRunning = false;
         LoadLevel (1);
     }
 
     void Update() {
         if ( Input.GetKeyDown(KeyCode.G)) {
             PostMessage (false, "JHDECGW", "HAHAHA!");
         }
         if ( Input.GetKeyDown(KeyCode.Escape)) {
             if ( optionsOpen ) {
                 optionsOpen = false;
             }
             else {
                 optionsOpen = true;
             }
         }
     }
 
     void OnGUI() {
         int mm = 1;
             foreach(Message m in messages) {
                 GUI.Label(new Rect(10, Screen.height - (20 + 20 * mm), 800, 20),m.playerName + "" + m.message);
                 mm++;
             }
             if (!Network.isClient && !Network.isServer) {
                 if ( Network.peerType != NetworkPeerType.Connecting ) {
                     if ( !connectionFailed ) {
                     currentTimeOut = 0f;
                         if ( state == 0 ) {
                             GUI.Box(new Rect(270, 10, Screen.width - 280, Screen.height - 20), "");
                             if (GUI.Button(new Rect(10, 10, 250, 100), "Start Server")) {
                                 state = 1;
                             }
                             if (GUI.Button(new Rect(10, 120, 250, 100), "Refresh Hosts")) {
                                 RefreshHostList();
                             }
                             if (hostList != null) {
                                 for (int i = 0; i < hostList.Length; i++) {
                                     string name = hostList[i].gameName;
                                     if ( hostList[i].passwordProtected ) { name += " |Password Protected|"; }
                                     if (GUI.Button(new Rect(280, 20 + (110 * i), Screen.width - 300, 20), name)) {
                                     if ( hostList[i].passwordProtected ) {
                                         state = 2;
                                         serverToJoin = hostList[i];
                                     }
                                     else {
                                         JoinServer(hostList[i]);
                                     }
                                 }
                             }
                         }
                     }
                     if ( state == 1 ) {
                         GUI.Box(new Rect(10, 10, 250, 200), "Server Creation");
                         GUI.Label(new Rect(20,38,100,20), "Server Name:");
                         serverName = GUI.TextField(new Rect(120, 40, 120, 20), serverName);
                         GUI.Label(new Rect(20,58,100,20), "Server Pass:");
                         serverPassword = GUI.TextField(new Rect(120, 60, 120, 20), serverPassword);
                         if (GUI.Button(new Rect(90, 85, 100, 25), "Create!")) {
                             state = 0;
                             StartServer();
                         }
                         if (GUI.Button(new Rect(90, 115, 100, 25), "Back")) {
                             serverName = "";
                             serverPassword = "";
                             state = 0;
                         }
                     }
                     if ( state == 2) {
                         GUI.Box(new Rect(10, 10, 150, 100), "Input Password");
                         joiningPassword = GUI.TextField(new Rect(10, 50, 100, 20), joiningPassword);
                         if (GUI.Button(new Rect(75, 50, 50, 25), "Join!")) {
                             JoinServer(serverToJoin);
                         }
                         if (GUI.Button(new Rect(105, 50, 50, 25), "Back")) {
                             joiningPassword = "";
                             state = 0;
                         }
                     }
                 }
                 else {
                     PostMessage(false, "", connectionError);
                     connectionFailed = false;
                 }
             }
             else {
                 if ( Network.peerType == NetworkPeerType.Connecting ) {
                     currentTimeOut += Time.deltaTime;
                     if (currentTimeOut <= connectionMaxTimeout) {
                         GUI.Box(new Rect(Screen.width/2-100, Screen.height/2-10, 200, 20), "Connecting...");
                     }
                     else {
                         GUI.Box(new Rect(Screen.width/2-100, Screen.height/2-10, 200, 20), "Failed to connect.");
                         if (currentTimeOut >= connectionMaxTimeout+2) {
                             Network.Disconnect();
                         }
                     }
                 }
             }
         }
         if ( Network.isClient || Network.isServer ) {
             if ( optionsOpen ) {
                 GUI.Window(0, new Rect (Screen.width / 2 - 400, Screen.height / 2 - 300, 800, 600), DoOptionsWindow, "Options");
             }
         }
     }
 
     void DoOptionsWindow(int windowID) {
         GUI.Label (new Rect(10, 20, 200, 20), "Light Quality: ");
         GUI.Label (new Rect(10, 43, 300, 20), "Anti-Aliasing: ");
         GUI.Label (new Rect (10, 72, 300, 20), "Animation Quality: ");
         QualitySettings.pixelLightCount = System.Convert.ToInt32(GUI.HorizontalSlider (new Rect(200, 25, 300, 20), QualitySettings.pixelLightCount, 2, 10));
         if (QualitySettings.antiAliasing == 0 ){GUI.color=Color.green;}else{GUI.color=Color.red;}if (GUI.Button (new Rect (200, 40, 24, 24), "0x")) { QualitySettings.antiAliasing = 0; }
         if (QualitySettings.antiAliasing == 2 ){GUI.color=Color.green;}else{GUI.color=Color.red;}if (GUI.Button (new Rect (230, 40, 24, 24), "2x")) { QualitySettings.antiAliasing = 2; }
         if (QualitySettings.antiAliasing == 4 ){GUI.color=Color.green;}else{GUI.color=Color.red;}if (GUI.Button (new Rect (260, 40, 24, 24), "4x")) { QualitySettings.antiAliasing = 4; }
         if (QualitySettings.antiAliasing == 8 ){GUI.color=Color.green;}else{GUI.color=Color.red;}if (GUI.Button (new Rect (290, 40, 24, 24), "8x")) { QualitySettings.antiAliasing = 8; }
         if (QualitySettings.blendWeights == BlendWeights.OneBone ){GUI.color=Color.green;}else{GUI.color=Color.red;}if (GUI.Button (new Rect (200, 70, 40, 24), "Low")) { QualitySettings.blendWeights = BlendWeights.OneBone; }
         if (QualitySettings.blendWeights == BlendWeights.TwoBones ){GUI.color=Color.green;}else{GUI.color=Color.red;}if (GUI.Button (new Rect (250, 70, 40, 24), "Med")) { QualitySettings.blendWeights = BlendWeights.TwoBones; }
         if (QualitySettings.blendWeights == BlendWeights.FourBones ){GUI.color=Color.green;}else{GUI.color=Color.red;}if (GUI.Button (new Rect (300, 70, 40, 24), "High")) { QualitySettings.blendWeights = BlendWeights.FourBones; }
     }
     
     void OnFailedToConnect(NetworkConnectionError error) {
         if (error == NetworkConnectionError.NATPunchthroughFailed) {
             connectionFailed = true;
             connectionError = "Connection Failed (NAT Punchthrough failed).";
         }
         if (error == NetworkConnectionError.InvalidPassword) {
             connectionFailed = true;
             connectionError = "Connection Failed (Invalid password).";
         }
         if (error == NetworkConnectionError.ConnectionBanned) {
             connectionFailed = true;
             connectionError = "Connection Failed (Banned).";
         }
         if (error == NetworkConnectionError.EmptyConnectTarget) {
             connectionFailed = true;
             connectionError = "Connection Failed (Invaild target).";
         }
         if (error == NetworkConnectionError.NoError) {
             connectionFailed = true;
             connectionError = "Connection Failed (Unknown error).";
         }
         if (error == NetworkConnectionError.TooManyConnectedPlayers) {
             connectionFailed = true;
             connectionError = "Connection Failed (Server full).";
         }
     }
 
     private void RefreshHostList() {
         MasterServer.RequestHostList(typeName);
     }
     
     void OnMasterServerEvent(MasterServerEvent msEvent) {
         if (msEvent == MasterServerEvent.HostListReceived)
             hostList = MasterServer.PollHostList();
     }
 
     private void JoinServer(HostData hostData) {
         Network.Connect(hostData, joiningPassword);
     }
     
     void OnConnectedToServer() {
         UpdatePlayerCount();
         LoadLevel (1);
     }
 
     void LoadLevel(int i) {
         Network.isMessageQueueRunning = false;
         Application.LoadLevel (i);
     }
 
     void OnLevelWasLoaded() {
         if ( Application.loadedLevel == 1 ) {
             Network.isMessageQueueRunning = true;
             myGO = Network.Instantiate(playerPrefab, new Vector3(0f, 2.5f, 0f), Quaternion.identity, 0) as GameObject;
             myGO.transform.Find("Main Camera").GetComponent<Camera> ().enabled = true;
             myGO.transform.Find("Main Camera").GetComponent<MouseLook> ().enabled = true;
             myGO.GetComponent<MouseLook> ().enabled = true;
             GetComponent<ScreenGUI>().CreateMyGUI();
         }
     }
 
     void OnPlayerDisconnected(NetworkPlayer player) {
         Debug.Log("Clean up after player " + player);
         Network.RemoveRPCs(player);
         Network.DestroyPlayerObjects(player);
         Debug.Log ("Player Disconnected");
         UpdatePlayerCount();
     }
 
     void OnPlayerConnected(NetworkPlayer player) {
         Debug.Log ("Player Connected");
         UpdatePlayerCount();
        }
 
     void UpdatePlayerCount() {
         playerCount = Network.connections.Length;
         if ( playerCount < 1 ) {
             singlePlayer = true;
         }
         else {
             singlePlayer = false;
         }
         Debug.Log (playerCount.ToString ());
     }
 
     [System.Serializable]
     public class Message {
         public bool hasPlayer = false;
         public string playerName = "";
         public string message = "";
     }
 
     [RPC] void PostMessage(bool hasPlayer, string playerName, string message) {
         Message msg = new Message();
         msg.hasPlayer = hasPlayer;
         msg.playerName = playerName;
         msg.message = message;
         messages.Add (msg);
     }
 }

This is for a general server (for FPS and stuff), I don't know how to set up a master server.

It's not perfect, and has some debug elements in it. This script creates a GUI, with the ability to host and join servers, once you join or host a server, your player gameobject(which you have to set up in the inspector) will be spawned in scene index 1. This is untested by itself, so it may require some patch work to get working.

Comment
Add comment · Share
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

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

27 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 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

Unity networking tutorial? 6 Answers

attached prefabs before client joins 2 Answers

Iphone, android multiplayer app 2 Answers

Most efficient way to structure a huge online "virtual world" 0 Answers

Multiplayer game with Hamachi ,help 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