aha
This commit is contained in:
54
Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyCreate.cs
Normal file
54
Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyCreate.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using Edgegap;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Mirror.Examples.EdgegapLobby
|
||||
{
|
||||
public class UILobbyCreate : MonoBehaviour
|
||||
{
|
||||
public UILobbyList List;
|
||||
public Button CancelButton;
|
||||
public InputField LobbyName;
|
||||
public Text SlotCount;
|
||||
public Slider SlotSlider;
|
||||
public Button HostButton;
|
||||
public Button ServerButton;
|
||||
private EdgegapLobbyKcpTransport _transport => (EdgegapLobbyKcpTransport)NetworkManager.singleton.transport;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
ValidateName();
|
||||
LobbyName.onValueChanged.AddListener(_ =>
|
||||
{
|
||||
ValidateName();
|
||||
});
|
||||
CancelButton.onClick.AddListener(() =>
|
||||
{
|
||||
List.gameObject.SetActive(true);
|
||||
gameObject.SetActive(false);
|
||||
});
|
||||
SlotSlider.onValueChanged.AddListener(arg0 =>
|
||||
{
|
||||
SlotCount.text = ((int)arg0).ToString();
|
||||
});
|
||||
HostButton.onClick.AddListener(() =>
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
_transport.SetServerLobbyParams(LobbyName.text, (int)SlotSlider.value);
|
||||
NetworkManager.singleton.StartHost();
|
||||
});
|
||||
ServerButton.onClick.AddListener(() =>
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
_transport.SetServerLobbyParams(LobbyName.text, (int)SlotSlider.value);
|
||||
NetworkManager.singleton.StartServer();
|
||||
});
|
||||
}
|
||||
void ValidateName()
|
||||
{
|
||||
bool valid = !string.IsNullOrWhiteSpace(LobbyName.text);
|
||||
HostButton.interactable = valid;
|
||||
ServerButton.interactable = valid;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6d48c41753254160ac6a02c9585880f0
|
||||
timeCreated: 1709967491
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 129321
|
||||
packageName: Mirror
|
||||
packageVersion: 96.0.1
|
||||
assetPath: Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyCreate.cs
|
||||
uploadId: 736421
|
37
Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyEntry.cs
Normal file
37
Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyEntry.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Edgegap;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Mirror.Examples.EdgegapLobby
|
||||
{
|
||||
public class UILobbyEntry : MonoBehaviour
|
||||
{
|
||||
public Button JoinButton;
|
||||
public Text Name;
|
||||
public Text PlayerCount;
|
||||
|
||||
private LobbyBrief _lobby;
|
||||
private UILobbyList _list;
|
||||
private void Awake()
|
||||
{
|
||||
JoinButton.onClick.AddListener(() =>
|
||||
{
|
||||
_list.Join(_lobby);
|
||||
});
|
||||
}
|
||||
|
||||
public void Init(UILobbyList list, LobbyBrief lobby, bool active = true)
|
||||
{
|
||||
gameObject.SetActive(active && lobby.is_joinable);
|
||||
JoinButton.interactable = lobby.available_slots > 0;
|
||||
_list = list;
|
||||
_lobby = lobby;
|
||||
Name.text = lobby.name;
|
||||
PlayerCount.text = $"{lobby.player_count}/{lobby.capacity}";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9bd3228b44c0a7e478964d95c512cebf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 129321
|
||||
packageName: Mirror
|
||||
packageVersion: 96.0.1
|
||||
assetPath: Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyEntry.cs
|
||||
uploadId: 736421
|
91
Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyList.cs
Normal file
91
Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyList.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Edgegap;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
namespace Mirror.Examples.EdgegapLobby
|
||||
{
|
||||
public class UILobbyList : MonoBehaviour
|
||||
{
|
||||
public UILobbyCreate Create;
|
||||
|
||||
public GameObject EntryPrefab;
|
||||
public Transform LobbyContent;
|
||||
public GameObject Loading;
|
||||
public Button RefreshButton;
|
||||
public InputField SearchInput;
|
||||
public Button CreateButton;
|
||||
public Text Error;
|
||||
private List<UILobbyEntry> _entries = new List<UILobbyEntry>();
|
||||
|
||||
private EdgegapLobbyKcpTransport _transport => (EdgegapLobbyKcpTransport)NetworkManager.singleton.transport;
|
||||
private void Awake()
|
||||
{
|
||||
SearchInput.onValueChanged.AddListener(arg0 =>
|
||||
{
|
||||
SetLobbies(_transport.Api.Lobbies);
|
||||
});
|
||||
RefreshButton.onClick.AddListener(Refresh);
|
||||
CreateButton.onClick.AddListener(() =>
|
||||
{
|
||||
Create.gameObject.SetActive(true);
|
||||
gameObject.SetActive(false);
|
||||
});
|
||||
}
|
||||
public void Start()
|
||||
{
|
||||
Refresh();
|
||||
}
|
||||
|
||||
private void Refresh()
|
||||
{
|
||||
Loading.SetActive(true);
|
||||
_transport.Api.RefreshLobbies(SetLobbies, s =>
|
||||
{
|
||||
Error.text = s;
|
||||
Loading.SetActive(false);
|
||||
});
|
||||
}
|
||||
|
||||
public void Join(LobbyBrief lobby)
|
||||
{
|
||||
NetworkManager.singleton.networkAddress = lobby.lobby_id;
|
||||
NetworkManager.singleton.StartClient();
|
||||
}
|
||||
|
||||
public void SetLobbies(LobbyBrief[] lobbies)
|
||||
{
|
||||
Loading.SetActive(false);
|
||||
Error.text = "";
|
||||
// Create enough entries
|
||||
for (int i = _entries.Count; i < lobbies.Length; i++)
|
||||
{
|
||||
var go = Instantiate(EntryPrefab, LobbyContent);
|
||||
_entries.Add(go.GetComponent<UILobbyEntry>());
|
||||
}
|
||||
|
||||
// Update entries
|
||||
var searchText = SearchInput.text;
|
||||
for (int i = 0; i < lobbies.Length; i++)
|
||||
{
|
||||
_entries[i].Init(
|
||||
this,
|
||||
lobbies[i],
|
||||
// search filter
|
||||
searchText.Length == 0 ||
|
||||
#if UNITY_2021_3_OR_NEWER
|
||||
lobbies[i].name.Contains(searchText, StringComparison.InvariantCultureIgnoreCase)
|
||||
#else
|
||||
lobbies[i].name.IndexOf(searchText, StringComparison.InvariantCultureIgnoreCase) >= 0
|
||||
#endif
|
||||
);
|
||||
}
|
||||
|
||||
// hide entries that are too many
|
||||
for (int i = lobbies.Length; i < _entries.Count; i++)
|
||||
{
|
||||
_entries[i].gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c5e5ad322f314077a66f889b58485188
|
||||
timeCreated: 1709962378
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 129321
|
||||
packageName: Mirror
|
||||
packageVersion: 96.0.1
|
||||
assetPath: Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyList.cs
|
||||
uploadId: 736421
|
121
Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyStatus.cs
Normal file
121
Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyStatus.cs
Normal file
@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using Edgegap;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
namespace Mirror.Examples.EdgegapLobby
|
||||
{
|
||||
public class UILobbyStatus : MonoBehaviour
|
||||
{
|
||||
public GameObject[] ShowDisconnected;
|
||||
public GameObject[] ShowServer;
|
||||
public GameObject[] ShowHost;
|
||||
public GameObject[] ShowClient;
|
||||
public Button StopServer;
|
||||
public Button StopHost;
|
||||
public Button StopClient;
|
||||
public Text StatusText;
|
||||
private Status _status;
|
||||
private EdgegapLobbyKcpTransport _transport;
|
||||
enum Status
|
||||
{
|
||||
Offline,
|
||||
Server,
|
||||
Host,
|
||||
Client
|
||||
}
|
||||
void Awake()
|
||||
{
|
||||
Refresh();
|
||||
StopServer.onClick.AddListener(() =>
|
||||
{
|
||||
NetworkManager.singleton.StopServer();
|
||||
});
|
||||
StopHost.onClick.AddListener(() =>
|
||||
{
|
||||
NetworkManager.singleton.StopHost();
|
||||
});
|
||||
StopClient.onClick.AddListener(() =>
|
||||
{
|
||||
NetworkManager.singleton.StopClient();
|
||||
});
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
_transport = (EdgegapLobbyKcpTransport)NetworkManager.singleton.transport;
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
var status = GetStatus();
|
||||
if (_status != status)
|
||||
{
|
||||
_status = status;
|
||||
Refresh();
|
||||
}
|
||||
if (_transport)
|
||||
{
|
||||
StatusText.text = _transport.Status.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
StatusText.text = "";
|
||||
}
|
||||
}
|
||||
private void Refresh()
|
||||
{
|
||||
switch (_status)
|
||||
{
|
||||
|
||||
case Status.Offline:
|
||||
SetUI(ShowServer, false);
|
||||
SetUI(ShowHost, false);
|
||||
SetUI(ShowClient, false);
|
||||
SetUI(ShowDisconnected, true);
|
||||
break;
|
||||
case Status.Server:
|
||||
SetUI(ShowDisconnected, false);
|
||||
SetUI(ShowHost, false);
|
||||
SetUI(ShowClient, false);
|
||||
SetUI(ShowServer, true);
|
||||
break;
|
||||
case Status.Host:
|
||||
SetUI(ShowDisconnected, false);
|
||||
SetUI(ShowServer, false);
|
||||
SetUI(ShowClient, false);
|
||||
SetUI(ShowHost, true);
|
||||
break;
|
||||
case Status.Client:
|
||||
SetUI(ShowDisconnected, false);
|
||||
SetUI(ShowServer, false);
|
||||
SetUI(ShowHost, false);
|
||||
SetUI(ShowClient, true);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
private void SetUI(GameObject[] gos, bool active)
|
||||
{
|
||||
foreach (GameObject go in gos)
|
||||
{
|
||||
go.SetActive(active);
|
||||
}
|
||||
}
|
||||
private Status GetStatus()
|
||||
{
|
||||
if (NetworkServer.active && NetworkClient.active)
|
||||
{
|
||||
return Status.Host;
|
||||
}
|
||||
if (NetworkServer.active)
|
||||
{
|
||||
return Status.Server;
|
||||
}
|
||||
if (NetworkClient.active)
|
||||
{
|
||||
return Status.Client;
|
||||
}
|
||||
return Status.Offline;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 44d2f1170bbe4432bf6f388bcfabefee
|
||||
timeCreated: 1710138272
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 129321
|
||||
packageName: Mirror
|
||||
packageVersion: 96.0.1
|
||||
assetPath: Assets/Mirror/Examples/EdgegapLobby/Scripts/UILobbyStatus.cs
|
||||
uploadId: 736421
|
Reference in New Issue
Block a user