浏览代码
Adding in nameplates to player cursors, so that player names are shown except for that of the local client. Adding a networked data store to live on the server, since we'll need to retrieve the names for the end-of-game UI as well.
/main/staging/ngo_minigame_cleanup
Adding in nameplates to player cursors, so that player names are shown except for that of the local client. Adding a networked data store to live on the server, since we'll need to retrieve the names for the end-of-game UI as well.
/main/staging/ngo_minigame_cleanup
nathaniel.buck@unity3d.com
3 年前
当前提交
3c745e1e
共有 10 个文件被更改,包括 401 次插入 和 29 次删除
-
14Assets/Prefabs/InGame/InGameLogic.prefab
-
220Assets/Prefabs/InGame/PlayerCursor.prefab
-
44Assets/Scripts/Netcode/InGameRunner.cs
-
29Assets/Scripts/Netcode/PlayerCursor.cs
-
2Assets/Scripts/Netcode/Scorer.cs
-
8Assets/Scripts/Netcode/SetupInGame.cs
-
20Assets/Scripts/Netcode/LobbyUserData.cs
-
11Assets/Scripts/Netcode/LobbyUserData.cs.meta
-
71Assets/Scripts/Netcode/NetworkedDataStore.cs
-
11Assets/Scripts/Netcode/NetworkedDataStore.cs.meta
|
|||
using Unity.Netcode; |
|||
|
|||
namespace LobbyRelaySample.ngo |
|||
{ |
|||
/// <summary>
|
|||
/// An example of a custom type serialized for use in RPC calls.
|
|||
/// </summary>
|
|||
public struct LobbyUserData : INetworkSerializable |
|||
{ |
|||
public string name; |
|||
public ulong id; |
|||
public LobbyUserData(string name, ulong id) { this.name = name; this.id = id; } |
|||
|
|||
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter |
|||
{ |
|||
serializer.SerializeValue(ref name); |
|||
serializer.SerializeValue(ref id); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 3e81b917b9edfc64f95cebddd2deb7b2 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Unity.Netcode; |
|||
|
|||
namespace LobbyRelaySample.ngo |
|||
{ |
|||
/// <summary>
|
|||
/// A place to store data needed by networked behaviors. Each client has an instance, but the server's instance stores the actual data.
|
|||
/// </summary>
|
|||
public class NetworkedDataStore : NetworkBehaviour |
|||
{ |
|||
// Using a singleton here since we need spawned PlayerCursors to be able to find it, but we don't need the flexibility offered by the Locator.
|
|||
public static NetworkedDataStore Instance; |
|||
|
|||
private Dictionary<ulong, string> m_playerNames = new Dictionary<ulong, string>(); |
|||
private ulong m_localId; |
|||
|
|||
public void Awake() |
|||
{ |
|||
Instance = this; |
|||
} |
|||
public override void OnDestroy() |
|||
{ |
|||
base.OnDestroy(); |
|||
if (Instance == this) |
|||
Instance = null; |
|||
} |
|||
|
|||
public override void OnNetworkSpawn() |
|||
{ |
|||
m_localId = NetworkManager.Singleton.LocalClientId; |
|||
} |
|||
|
|||
public void AddPlayer(ulong id, string name) |
|||
{ |
|||
if (!IsServer) |
|||
return; |
|||
|
|||
if (!m_playerNames.ContainsKey(id)) |
|||
m_playerNames.Add(id, name); |
|||
else |
|||
m_playerNames[id] = name; |
|||
} |
|||
|
|||
// NetworkList and NetworkDictionary are not considered suitable for production, so instead we use RPC calls to retrieve names from the host.
|
|||
private Action<string> m_onGetCurrent; |
|||
public void GetPlayerName(ulong ownerId, Action<string> onGet) |
|||
{ |
|||
m_onGetCurrent = onGet; |
|||
GetPlayerName_ServerRpc(ownerId, m_localId); |
|||
} |
|||
|
|||
[ServerRpc(RequireOwnership = false)] |
|||
private void GetPlayerName_ServerRpc(ulong id, ulong callerId) |
|||
{ |
|||
string name = string.Empty; |
|||
if (m_playerNames.ContainsKey(id)) |
|||
name = m_playerNames[id]; |
|||
GetPlayerName_ClientRpc(callerId, name); |
|||
} |
|||
|
|||
[ClientRpc] |
|||
public void GetPlayerName_ClientRpc(ulong callerId, string name) |
|||
{ |
|||
if (callerId == m_localId) |
|||
{ m_onGetCurrent?.Invoke(name); |
|||
m_onGetCurrent = null; |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 5a054eb742dd9f748afc05982dc263b0 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue