Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 giquon · Dec 26, 2015 at 11:19 AM · c#unity 5networkingmultiplayer

Unity Network Player Classes, different models

Hello Unity Community,

I'm working on a multiplayer game, in which players should be able to pick a class that they want to play. But in the network manager, it is only possible to assign just one player prefab, and I assume I would need more, sinsce classes would all have different models. I'm completely new to unity networking, so if you could help me, I would really eppreciate that. To clarify what I mean with classes, think of Star Wars Battlefront 2, where you can pick what kind of clone you want to be for example.

Sorry for possible bad English. (I'm Dutch).

p.s. I'm using C# for this project.

Comment
Add comment · Show 1
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
avatar image giquon · Dec 24, 2015 at 02:32 PM 0
Share

Also I forgot to mention, I am on the unity free version. Don' t really know if this would be a problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eiseno · Dec 28, 2015 at 02:38 PM

Here is my code.I use playerPrefabIndex and change this value with button.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 using System;
 
 public class MsgTypes
 {
     public const short PlayerPrefab = MsgType.Highest + 1;
 
     public class PlayerPrefabMsg : MessageBase
     {
         public short controllerID;
         public short prefabIndex;
     }
 }
 public class NetworkManager_Custom : NetworkManager
 {
     [SerializeField]
     Vector3 playerSpawnPos;
     short playerPrefabIndex = 0;
 
     // etc.
     public void StartUpHost()
     {
         Debug.Log(playerPrefabIndex + "is host");
 
         SetPort();
         NetworkManager.singleton.StartHost();
 
     }
 
     public void JoinGame()
     {
         Debug.Log(playerPrefabIndex + "is joined");
 
         SetIpAddress();
         SetPort();
         NetworkManager.singleton.StartClient();
     }
 
     private void SetIpAddress()
     {
         string ipAddress = GameObject.Find("InputFieldIPAddress").transform.FindChild("Text").GetComponent<Text>().text;
         NetworkManager.singleton.networkAddress = ipAddress;
     }
 
     void SetPort()
     {
         NetworkManager.singleton.networkPort = 7777;
     }
     public void ChangeVRMode()
     {
         if (playerPrefabIndex == 0)
         {
             playerPrefabIndex = 1;
         }
         else
         {
             playerPrefabIndex = 0;
         }
         Debug.Log(playerPrefabIndex + "new index");
     }
     void OnLevelWasLoaded(int level)
     {
         if (level == 0)
         {
             SetupMenuSceneButtons();
         }
         else
         {
             SetupOtherSceneButtons();
         }
     }
 
     private void SetupMenuSceneButtons()
     {
         //    GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.RemoveAllListeners();
         //  GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.AddListener(StartUpHost);
     }
 
     private void SetupOtherSceneButtons()
     {
 
     }
 
     public override void OnStartServer()
     {
         NetworkServer.RegisterHandler(MsgTypes.PlayerPrefab, OnResponsePrefab);
         base.OnStartServer();
     }
 
     public override void OnClientConnect(NetworkConnection conn)
     {
         client.RegisterHandler(MsgTypes.PlayerPrefab, OnRequestPrefab);
         base.OnClientConnect(conn);
     }
 
     private void OnRequestPrefab(NetworkMessage netMsg)
     {
         MsgTypes.PlayerPrefabMsg msg = new MsgTypes.PlayerPrefabMsg();
         msg.controllerID = netMsg.ReadMessage<MsgTypes.PlayerPrefabMsg>().controllerID;
 
         msg.prefabIndex = playerPrefabIndex;
 
         client.Send(MsgTypes.PlayerPrefab, msg);
     }
 
     private void OnResponsePrefab(NetworkMessage netMsg)
     {
         MsgTypes.PlayerPrefabMsg msg = netMsg.ReadMessage<MsgTypes.PlayerPrefabMsg>();
         playerPrefab = spawnPrefabs[msg.prefabIndex];
         Debug.Log(playerPrefab.name + " spawned!" + msg.prefabIndex);
         base.OnServerAddPlayer(netMsg.conn, msg.controllerID);
     }
 
     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
     {
         MsgTypes.PlayerPrefabMsg msg = new MsgTypes.PlayerPrefabMsg();
         msg.controllerID = playerControllerId;
         NetworkServer.SendToClient(conn.connectionId, MsgTypes.PlayerPrefab, msg);
     }
 
     // I have put a toggle UI on gameObjects called PC1 and PC2 to select two different character types.
     // on toggle, this function is called, which updates the playerPrefabIndex
     // The index will be the number from the registered spawnable prefabs that 
     // you want for your player
     //public void UpdatePC()
     //{
     //    if (GameObject.Find("PC1").GetComponent<Toggle>().isOn)
     //    {
     //        playerPrefabIndex = 0;
     //    }
     //    else if (GameObject.Find("PC2").GetComponent<Toggle>().isOn)
     //    {
     //        playerPrefabIndex = 1;
     //    }
     //}
 
 
 
 }
 
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Pushed back when applying NetworkTransform 1 Answer

How do i check which object was instantiated last? 1 Answer

UNET Display Connected Player Names inGame UI 0 Answers

How to disable the runtime GUI on the Network Manager Hud when you connect to a server?,How do you disable the runtime GUI on the Network Manager Hud when connected to a server 0 Answers


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