- Home /
Question by
Vanderlolbroek · May 20, 2019 at 03:28 PM ·
scripting problemnetworkingclienthosting
why do I have so many bugs in my custom networkmanager HUD script
I got many bugs in my custom network manager HUD script and I don't know what is the problem. I think because if a client leaves the server the server doesn't know the client has left. But I have tried many things but I don't know what the problem really is.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Net;
using System.Net.Sockets;
using UnityEngine.SceneManagement;
using TMPro;
public class NetworkManagerHUDCustom : NetworkBehaviour
{
public GameObject button1Host;
public GameObject button2Host;
public GameObject button1Join;
public GameObject button2Join;
public GameObject canvas;
public GameObject buttons;
public string ip;
public string shortIp;
public TMP_Text ipText;
public TMP_InputField ipInput;
public bool updateblabla;
private bool isConnected;
// Runtime variable
bool m_ShowServer;
public NetworkManager manager;
void Awake()
{
manager = GameObject.FindObjectOfType<NetworkManager>();
MakeShortIp();
FirstPartOfIp();
manager = NetworkManager.singleton;
}
private void Update()
{
//manager.IsClientConnected
if (!ipInput)
{
return;
}
else
{
ip = FirstPartOfIp() + ipInput.text;
}
if (SceneManager.GetActiveScene().name == "Main")
{
canvas.SetActive(true);
buttons.SetActive(true);
}
else
{
canvas.SetActive(false);
buttons.SetActive(false);
}
}
public string FirstPartOfIp()
{
string localIp = LocalIPAddress();
while (localIp.Substring(localIp.Length - 1, 1) != ".")
{
localIp = localIp.Remove(localIp.Length - 1);
}
return (localIp);
}
public void MakeShortIp()
{
string localIp = LocalIPAddress();
while (localIp.Substring(localIp.Length - 1, 1) != ".")
{
shortIp = shortIp.Insert(0, localIp.Substring(localIp.Length - 1, 1));
localIp = localIp.Remove(localIp.Length - 1);
}
ipText.text = shortIp;
}
public string LocalIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
}
public void HostParty(GameObject button)
{
if (!manager.IsClientConnected() && !NetworkServer.active && manager.matchMaker == null)
{
if (UnityEngine.Application.platform != RuntimePlatform.WebGLPlayer)
{
manager.StartHost();
}
}
SwitchButton(button1Host, button2Host);
button1Join.SetActive(false);
button2Join.SetActive(false);
}
public void JoinParty(GameObject button)
{
if (!manager.IsClientConnected() && !NetworkServer.active && manager.matchMaker == null)
{
manager.networkAddress = ip;
manager.StartClient();
}
SwitchButton(button1Join, button2Join);
button1Host.SetActive(false);
button2Host.SetActive(false);
}
public void LeaveParty(GameObject button)
{
if (manager.IsClientConnected())
{
manager.StopMatchMaker();
manager.StopClient();
}
SwitchButton(button2Join, button1Join);
button1Host.SetActive(true);
button2Host.SetActive(true);
}
public void StopHostParty(GameObject button)
{
if (NetworkServer.active)
{
if (manager.IsClientConnected())
{
manager.StopHost();
}
}
SwitchButton(button2Host, button1Host);
button1Join.SetActive(true);
button2Join.SetActive(true);
}
public void SwitchButton(GameObject obj1, GameObject obj2)
{
obj1.SetActive(false);
obj2.SetActive(true);
}
}
Comment