Example #1
0
    public void SetStatus()
    {
        //Find connection state panel
        GameObject connectionPanel = null;

        foreach (GameObject obj in displayedGUI)
        {
            if (obj.name.Equals(connectionPanelName))
            {
                connectionPanel = obj;
                break;
            }
        }

        if (connectionPanel != null)
        {
            Text   serverStatusLabel = null;
            Button reconnectButton   = null;

            //get reconnect button and server status label
            foreach (Transform t in connectionPanel.transform)
            {
                if (t.gameObject.name.Equals("ServerStatusLabel"))
                {
                    serverStatusLabel = t.gameObject.GetComponent <Text>();
                }
                else if (t.gameObject.name.Equals("ReconnectButton"))
                {
                    t.gameObject.SetActive(true);
                    reconnectButton = t.gameObject.GetComponent <Button>();
                }

                if (serverStatusLabel != null && reconnectButton != null)
                {
                    break;
                }
            }

            if (serverStatusLabel != null && reconnectButton != null)
            {
                switch (connectionState)
                {
                case ConnectionState.CONNECTED:
                {
                    serverStatusLabel.text  = "Connected to server.";
                    serverStatusLabel.color = Color.green;
                    reconnectButton.gameObject.SetActive(false);
                    break;
                }

                case ConnectionState.DISCONNECTED:
                {
                    serverStatusLabel.text  = "Disconnected.";
                    serverStatusLabel.color = Color.red;
                    reconnectButton.gameObject.SetActive(true);
                    reconnectButton.onClick.AddListener(delegate {
                            wsManager.OpenConnection();
                        });
                    break;
                }
                }
            }
        }
    }