浏览代码

Players have cursors that they control that will collide with symbol objects. On a click, the host checks for proximity to a symbol object and "destroys" one if it was clicked on.

/main/staging/ngo_minigame_structure
nathaniel.buck@unity3d.com 3 年前
当前提交
7e8eeb05
共有 9 个文件被更改,包括 318 次插入6 次删除
  1. 13
      Assets/Prefabs/InGame/SymbolObject.prefab
  2. 15
      Assets/Scripts/Game/InGame/InGameRunner.cs
  3. 2
      Assets/Scripts/Game/InGame/SetupInGame.cs
  4. 157
      Assets/Prefabs/InGame/PlayerCursor.prefab
  5. 7
      Assets/Prefabs/InGame/PlayerCursor.prefab.meta
  6. 85
      Assets/Scripts/Game/InGame/PlayerCursor.cs
  7. 11
      Assets/Scripts/Game/InGame/PlayerCursor.cs.meta
  8. 23
      Assets/Scripts/Game/InGame/SymbolObject.cs
  9. 11
      Assets/Scripts/Game/InGame/SymbolObject.cs.meta

13
Assets/Prefabs/InGame/SymbolObject.prefab


- component: {fileID: 1734492152380024498}
- component: {fileID: 2622345469831693260}
- component: {fileID: 1363360377255918887}
- component: {fileID: -8192876538761676823}
m_Layer: 0
m_Name: SymbolObject
m_TagString: Untagged

serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!114 &-8192876538761676823
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8828823320646980938}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 63adacb71a52dcb41be1952af26573d8, type: 3}
m_Name:
m_EditorClassIdentifier:

15
Assets/Scripts/Game/InGame/InGameRunner.cs


[SerializeField] private NetworkObject m_symbolObjectPrefab;
private NetworkList<ulong> m_connectedPlayerIds;
private ulong m_localClientId; // This is not necessarily the same as the OwnerClientId, since all clients will see all spawned objects regardless of ownership.
public void Initialize(Action onConnectionVerified, int expectedPlayerCount)
{

{
if (IsHost)
FinishInitialize();
VerifyConnection_ServerRpc(OwnerClientId);
m_localClientId = NetworkManager.Singleton.LocalClientId;
VerifyConnection_ServerRpc(m_localClientId);
}
public override void OnNetworkDespawn()

[ClientRpc]
private void VerifyConnection_ClientRpc(ulong clientId)
{
if (clientId == OwnerClientId)
VerifyConnectionConfirm_ServerRpc(OwnerClientId);
if (clientId == m_localClientId)
VerifyConnectionConfirm_ServerRpc(m_localClientId);
}
/// <summary>
/// Once the connection is confirmed, check if all players have connected.

{
NetworkObject playerCursor = NetworkObject.Instantiate(m_playerCursorPrefab);
playerCursor.SpawnWithOwnership(clientId);
playerCursor.name += clientId;
if (!m_connectedPlayerIds.Contains(clientId))
m_connectedPlayerIds.Add(clientId);
bool areAllPlayersConnected = m_connectedPlayerIds.Count >= m_expectedPlayerCount; // The game will begin at this point, or else there's a timeout for booting any unconnected players.

private void VerifyConnectionConfirm_ClientRpc(ulong clientId, bool shouldStartImmediately)
{
if (clientId == OwnerClientId)
if (clientId == m_localClientId)
m_onConnectionVerified?.Invoke();
if (shouldStartImmediately)
Locator.Get.Messenger.OnReceiveMessage(MessageType.GameBeginning, null);

Vector3 pendingPos = m_pendingSymbolPositions.Dequeue();
NetworkObject symbolObj = NetworkObject.Instantiate(m_symbolObjectPrefab);
symbolObj.Spawn();
symbolObj.name = "Symbol" + (k_symbolCount - m_pendingSymbolPositions.Count);
symbolObj.TrySetParent(m_symbolContainerInstance, false);
symbolObj.transform.localPosition = pendingPos;
}

2
Assets/Scripts/Game/InGame/SetupInGame.cs


private GameObject m_inGameManagerObj;
private NetworkManager m_networkManager;
private InGameObjectSpawner m_objectSpawner;
private InGameRunner m_inGameRunner;
private int m_playerCount; // The server will need to know this.

{
m_inGameManagerObj = GameObject.Instantiate(m_prefabNetworkManager);
m_networkManager = m_inGameManagerObj.GetComponentInChildren<NetworkManager>();
m_objectSpawner = m_inGameManagerObj.GetComponentInChildren<InGameObjectSpawner>();
m_inGameRunner = m_inGameManagerObj.GetComponentInChildren<InGameRunner>();
m_inGameRunner.Initialize(OnConnectionVerified, m_playerCount);

157
Assets/Prefabs/InGame/PlayerCursor.prefab


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &3227847727972158006
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 3227847727972158004}
- component: {fileID: -1321688216342888635}
- component: {fileID: 1724769857655924793}
- component: {fileID: 4403165428829500588}
- component: {fileID: 3227847727972158005}
- component: {fileID: -1476701697690489341}
m_Layer: 0
m_Name: PlayerCursor
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &3227847727972158004
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3227847727972158006}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &-1321688216342888635
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3227847727972158006}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3}
m_Name:
m_EditorClassIdentifier:
GlobalObjectIdHash: 951099334
AlwaysReplicateAsRoot: 0
DontDestroyWithOwner: 0
AutoObjectParentSync: 1
--- !u!114 &1724769857655924793
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3227847727972158006}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e96cb6065543e43c4a752faaa1468eb1, type: 3}
m_Name:
m_EditorClassIdentifier:
SyncPositionX: 1
SyncPositionY: 1
SyncPositionZ: 1
SyncRotAngleX: 0
SyncRotAngleY: 0
SyncRotAngleZ: 0
SyncScaleX: 0
SyncScaleY: 0
SyncScaleZ: 0
PositionThreshold: 0.001
RotAngleThreshold: 0.01
ScaleThreshold: 0.01
InLocalSpace: 0
Interpolate: 0
CanCommitToTransform: 0
--- !u!114 &4403165428829500588
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3227847727972158006}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1dff2bc874952454c9da192885efd32b, type: 3}
m_Name:
m_EditorClassIdentifier:
m_renderer: {fileID: 3227847727972158005}
--- !u!212 &3227847727972158005
SpriteRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3227847727972158006}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 0
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 0
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Sprite: {fileID: 7482667652216324306, guid: 48e93eef0688c4a259cb0eddcd8661f7, type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 1, y: 1}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!65 &-1476701697690489341
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3227847727972158006}
m_Material: {fileID: 0}
m_IsTrigger: 1
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 0.2}
m_Center: {x: 0, y: 0, z: 0}

7
Assets/Prefabs/InGame/PlayerCursor.prefab.meta


fileFormatVersion: 2
guid: 905594b4ee5bb864a84af916cc445d1b
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

85
Assets/Scripts/Game/InGame/PlayerCursor.cs


using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
namespace LobbyRelaySample.inGame
{
/// <summary>
/// Each player's cursor needs to be controlled by them and visible to the other players.
/// </summary>
public class PlayerCursor : NetworkBehaviour
{
private Camera m_mainCamera;
private NetworkVariable<Vector3> m_position = new NetworkVariable<Vector3>(NetworkVariableReadPermission.Everyone, Vector3.zero);
// The host is responsible for determining if a player has successfully selected a symbol object, since collisions should be handled serverside.
private List<SymbolObject> m_currentlyCollidingSymbols;
// We can't pass object references as RPC calls by default, and we don't have a different convenient way to, I think,
// get the object spawned on the server to assign some member on the client, so instead let's retrieve dynamically what we need.
// I guess I'd just have a "singleton" to hold the references?
public override void OnNetworkSpawn()
{
m_mainCamera = GameObject.Find("InGameCamera").GetComponent<Camera>();
if (IsHost)
m_currentlyCollidingSymbols = new List<SymbolObject>();
}
// Don't love having the input here, but it doesn't need to be anywhere else.
private bool IsSelectInputHit()
{
return Input.GetMouseButtonDown(0);
}
public void Update() // TODO: FixedUpdate?
{
transform.position = m_position.Value;
if (m_mainCamera == null || !IsOwner)
return;
Vector3 targetPos = (Vector2)m_mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -m_mainCamera.transform.position.z));
SetPosition_ServerRpc(targetPos); // Client can't set a network variable value.
if (IsSelectInputHit())
SendInput_ServerRpc();
}
[ServerRpc] // Leave RequireOwnership = true for these so that only the player whose cursor this is can make updates.
private void SetPosition_ServerRpc(Vector3 position)
{
m_position.Value = position;
}
[ServerRpc]
private void SendInput_ServerRpc()
{
if (m_currentlyCollidingSymbols.Count > 0)
{
SymbolObject symbol = m_currentlyCollidingSymbols[0];
m_currentlyCollidingSymbols.RemoveAt(0);
symbol.OnSelect();
}
}
public void OnTriggerEnter(Collider other)
{
if (!IsHost)
return;
SymbolObject symbol = other.GetComponent<SymbolObject>();
if (symbol == null)
return;
if (!m_currentlyCollidingSymbols.Contains(symbol))
m_currentlyCollidingSymbols.Add(symbol);
}
public void OnTriggerExit(Collider other)
{
if (!IsHost)
return;
SymbolObject symbol = other.GetComponent<SymbolObject>();
if (symbol == null)
return;
if (m_currentlyCollidingSymbols.Contains(symbol))
m_currentlyCollidingSymbols.Remove(symbol);
}
}
}

11
Assets/Scripts/Game/InGame/PlayerCursor.cs.meta


fileFormatVersion: 2
guid: 1dff2bc874952454c9da192885efd32b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

23
Assets/Scripts/Game/InGame/SymbolObject.cs


using Unity.Netcode;
using UnityEngine;
namespace LobbyRelaySample.inGame
{
public class SymbolObject : NetworkBehaviour
{
public void OnSelect()
{
Destroy_ServerRpc();
}
[ServerRpc(RequireOwnership = false)]
private void Destroy_ServerRpc()
{
// Actually destroying the symbol objects can cause garbage collection and other delays that might lead to desyncs.
// Instead, just deactivate the object, and it will be cleaned up once the NetworkManager is destroyed.
// (If object pooling, this is where to instead return it to the pool.)
this.transform.localPosition = Vector3.down * 500;
// TODO: Visually disappear immediately.
}
}
}

11
Assets/Scripts/Game/InGame/SymbolObject.cs.meta


fileFormatVersion: 2
guid: 63adacb71a52dcb41be1952af26573d8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存