feat(Chat Example): Bidirectional sync of networkAddress

This commit is contained in:
MrGadget1024 2024-01-01 10:23:24 -05:00
parent e1195391c6
commit 325cc15f5b
2 changed files with 25 additions and 0 deletions

View File

@ -2532,6 +2532,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 7a77ca56c9d91af4b81b73a9907d6112, type: 3}
m_Name:
m_EditorClassIdentifier:
networkAddressInput: {fileID: 1027272349}
usernameInput: {fileID: 851154181}
hostButton: {fileID: 1904406266}
clientButton: {fileID: 1063265580}

View File

@ -6,6 +6,7 @@ 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;
@ -13,11 +14,34 @@ public class LoginUI : MonoBehaviour
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)
{