- Home /
Question by
joshrwalter · Dec 07, 2017 at 09:53 PM ·
unity 5networkingcharacter3d modelscreation
Unity networking create & join with custom character?,
Im new to unity, sorry if this is a bad question.
What I want to happen: User selects what parts they want and pressed build. They then join server. Other users can see their player model, and likewise, the player can see other users character models.
Selection Script
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class controller : MonoBehaviour
{
string prevh;
string prevl;
public GameObject legs;
private List<GameObject> headModels;
public GameObject heads;
public GameObject NewParent;
private List<GameObject> legModels;
Dictionary<string, int> LegDict = new Dictionary<string, int>();
Dictionary<string, int> HeadDict = new Dictionary<string, int>();
[SerializeField] Dropdown DropLegs;
[SerializeField] Dropdown DropHeads;
private void Start()
{
//Generate list of body parts and hide them
legModels = new List<GameObject>();
int x = 0;
foreach (Transform t in legs.transform)
{
//Legs
legModels.Add(t.gameObject);
t.gameObject.SetActive(false);
LegDict.Add(t.ToString(), x);
x++;
DropLegs.options.Add(new Dropdown.OptionData(t.ToString()));
}
//Heads
x = 0;
headModels = new List<GameObject>();
foreach (Transform t in heads.transform)
{
t.gameObject.SetActive(false);
headModels.Add(t.gameObject);
HeadDict.Add(t.ToString(), x);
x++;
DropHeads.options.Add(new Dropdown.OptionData(t.ToString()));
}
}
private void Update()
{
//Shows built parts infront of camera
//Display Head
int menuIndex = DropHeads.GetComponent<Dropdown>().value;
List<Dropdown.OptionData> menuOptions = DropHeads.GetComponent<Dropdown>().options;
string valueh = menuOptions[menuIndex].text;
//Prevents from constantly updating
if (valueh != prevh)
{
//Clears other models
if (HeadDict.ContainsKey(valueh))
{
//Hides other models
foreach (Transform t in heads.transform)
{
t.gameObject.SetActive(false);
}
//Displays the current model
headModels[HeadDict[valueh]].SetActive(true);
}
}
prevh = valueh;
//Repeat of the code for heads but with legs
//Display legs
menuIndex = DropLegs.GetComponent<Dropdown>().value;
menuOptions = DropLegs.GetComponent<Dropdown>().options;
string valuel = menuOptions[menuIndex].text;
if (valuel != prevl)
{
if (LegDict.ContainsKey(valuel))
{
foreach (Transform t in legs.transform)
{
t.gameObject.SetActive(false);
}
legModels[LegDict[valuel]].SetActive(true);
}
}
prevl = valuel;
}
//Called when the build button is pressed.
public void Build()
{
//Save The Chosen Parts to a GameObject
GameObject myLeg = Instantiate(legs) as GameObject;
myLeg.transform.parent = NewParent.transform;
myLeg.transform.position = NewParent.transform.position;
GameObject myHead = Instantiate(heads) as GameObject;
myHead.transform.parent = NewParent.transform;
myHead.transform.position = NewParent.transform.position;
Debug.Log("Built Mech with head: " + prevh + " and legs: " + prevl);
}
}
Network Script
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
using UnityEngine.UI;
public class MyNetManager : NetworkManager {
public GameObject PlayerPrefab;
public GameObject UI;
public void StartupHost()
{
SetPort();
NetworkManager.singleton.StartHost();
HideUI();
}
public void HideUI()
{
UI.SetActive(false);
}
public void JoinGame()
{
SetIPAddress();
SetPort();
NetworkManager.singleton.StartClient();
HideUI();
}
void SetIPAddress()
{
string ipaddress = GameObject.Find("InputFieldIPAddress").transform.Find("Text").GetComponent<Text>().text;
NetworkManager.singleton.networkAddress = ipaddress;
}
void SetPort()
{
NetworkManager.singleton.networkPort = 7777;
}
private void LevelWasLoaded(int level)
{
if (level == 0)
{
Debug.Log("Loaded First");
SetupMenuSceneButtons();
}
else
{
Debug.Log("Loaded 2nd");
SetupOtherSceneButtons();
}
}
void SetupMenuSceneButtons()
{
GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.RemoveAllListeners();
GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.AddListener(StartupHost);
GameObject.Find("ButtonJoinGame").GetComponent<Button>().onClick.RemoveAllListeners();
GameObject.Find("ButtonJoinGame").GetComponent<Button>().onClick.AddListener(JoinGame);
}
void SetupOtherSceneButtons()
{
GameObject.Find("ButtonDisconnect").GetComponent<Button>().onClick.RemoveAllListeners();
GameObject.Find("ButtonDisconnect").GetComponent<Button>().onClick.AddListener(NetworkManager.singleton.StopHost);
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader)
{
int id = 0;
if (extraMessageReader != null)
{
IntegerMessage i = extraMessageReader.ReadMessage<IntegerMessage>();
id = i.value;
}
GameObject playerPrefab = PlayerPrefab;
GameObject player;
Transform startPos = GetStartPosition();
if (startPos != null)
{
player = (GameObject)Instantiate(playerPrefab, startPos.position, startPos.rotation);
}
else
{
player = (GameObject)Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
}
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}
,
Comment
Your answer
Follow this Question
Related Questions
NetworkManager.ServerChangeScene from client to server 0 Answers
Photon Network instantiate problem when Master changes 0 Answers
New Unity 5.1 Networking :- Spawning is not synchronized!!!! 2 Answers
Best method for Skyrim-Like character customization? 1 Answer
need help with syncing ui canvas 0 Answers