LoginUI.cs 1.83 KB
Newer Older
cann-alberto's avatar
cann-alberto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using UnityEngine;
using UnityEngine.UI;

namespace Mirror.Examples.Chat
{
    public class LoginUI : MonoBehaviour
    {
        [Header("UI Elements")]
        [SerializeField] internal InputField networkAddressInput;
        [SerializeField] internal InputField usernameInput;
        [SerializeField] internal Button hostButton;
        [SerializeField] internal Button clientButton;
        [SerializeField] internal Text errorText;

        public static LoginUI instance;

        string originalNetworkAddress;

        void Awake()
        {
            instance = this;
        }

        void Start()
        {
            // if we don't have a networkAddress, set a default one.
            if (string.IsNullOrWhiteSpace(NetworkManager.singleton.networkAddress))
                NetworkManager.singleton.networkAddress = "localhost";

            // cache the original networkAddress for resetting if blank.
            originalNetworkAddress = NetworkManager.singleton.networkAddress;
        }

        void Update()
        {
            // bidirectional sync of networkAddressInput and NetworkManager.networkAddress
            // Order of operations is important here...Don't switch the order of these steps.
            if (string.IsNullOrWhiteSpace(NetworkManager.singleton.networkAddress))
                NetworkManager.singleton.networkAddress = originalNetworkAddress;

            if (networkAddressInput.text != NetworkManager.singleton.networkAddress)
                networkAddressInput.text = NetworkManager.singleton.networkAddress;
        }

        // Called by UI element UsernameInput.OnValueChanged
        public void ToggleButtons(string username)
        {
            hostButton.interactable = !string.IsNullOrWhiteSpace(username);
            clientButton.interactable = !string.IsNullOrWhiteSpace(username);
        }
    }
}