Near the bottom, I have an enum state check, and if it’s StartServer, it makes it start a server and join a server, only if the player is not associated with a network at the moment, It spawns nothing when I take away the manager.StartClient, but adding it spawns 2.
PS yes this is a GUI button made from scratch. I don’t like on the UI how if you click and hover off, it stays highlighted so I constructed my own and it actually works pretty good.
public class Button : MonoBehaviour {
public bool isActive;
private bool mouseOver;
private bool mouseDown;
public float fadeFrac;
public enum States
{
Normal,
Highlighted,
Clicked,
Disabled
};
public States state;
public Color normalColor;
public Color highlightedColor;
public Color clickColor;
public Color disabledColor;
public Image image;
public enum Actions
{
StartServer,
JoinServer
};
public Actions action;
public NetworkManager manager;
void Update () {
if (state == States.Normal)
{
image.color = Color.Lerp (image.color, normalColor, fadeFrac);
}
if (state == States.Highlighted)
{
image.color = Color.Lerp (image.color, highlightedColor, fadeFrac);
}
if (state == States.Clicked)
{
image.color = Color.Lerp (image.color, clickColor, fadeFrac);
}
if (state == States.Disabled)
{
image.color = Color.Lerp (image.color, disabledColor, fadeFrac);
}
if (isActive == true && mouseOver == false && state == States.Clicked && Input.GetMouseButtonDown (0))
{
state = States.Normal;
}
}
void OnMouseOver () {
if (isActive == true && mouseDown == false && state != States.Clicked)
{
state = States.Highlighted;
}
mouseOver = true;
}
void OnMouseExit () {
if (isActive == true && mouseDown == false)
{
state = States.Normal;
}
mouseOver = false;
}
void OnMouseDown () {
if (isActive == true && mouseOver == true)
{
mouseDown = true;
}
}
void OnMouseUp () {
if (isActive == true && mouseOver == true && mouseDown == true)
{
state = States.Clicked;
if (action == Actions.StartServer)
{
if (manager.networkAddress != "" && manager.isNetworkActive == false)
{
manager.StartServer ();
manager.StartClient ();
}
}
if (action == Actions.JoinServer)
{
if (manager.networkAddress != "" && manager.isNetworkActive == false)
{
manager.StartClient ();
}
}
}
mouseDown = false;
}
}