NetworkRunner - cheona-thousand-man/Unity-myBasics-Wiki GitHub Wiki

๊ฐœ์š”

  • Fusion Network์—์„œ Peer(or Client)๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Unity ์ปดํฌ๋„ŒํŠธ
  • ํ†ต์ œ ํ•ญ๋ชฉ messasing, matchmaking, connecting, spawning, simulation, state replication
  • ํ•˜๋‚˜์˜ Unity ์ธ์Šคํ„ด์Šค์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ NetworkRunner ์ธ์Šคํ„ด์Šค ์‹คํ–‰ ๊ฐ€๋Šฅ
    image

์‚ฌ์šฉ๋ฒ•

1. ์ƒ์„ฑ

  • ๋Ÿฐํƒ€์ž„ ์‹œ Prefab์—์„œ ์ธ์Šคํ„ด์Šคํ™”
  • ์”ฌ ์˜ค๋ธŒ์ ํŠธ๋กœ ๋กœ๋“œ
  • ๋Ÿฐํƒ€์ž„ ์‹œ ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ์— NetworkRunner ์ปดํฌ๋„ŒํŠธ ๋™์ ์œผ๋กœ ์ถ”๊ฐ€

2. ์‹œ์ž‘ ๋ฐ ์—ฐ๊ฒฐ

ํ•œ ๋ฒˆ ์ƒ์„ฑ๋œ NetworkRunner ์ธ์Šคํ„ด์Šค๋Š” matchmaking์— ์—ฐ๊ฒฐ or ๋ฐฉ(๋ฃธ) ์ƒ์„ฑ/์ฐธ๊ฐ€ ๊ฐ€๋Šฅ

3. ๋ฐฉ(๋ฃธ) ์ƒ์„ฑ ๋˜๋Š” ์ฐธ๊ฐ€

  • StartGame(StartGameArgs args) ํ˜ธ์ถœ ์‹œ args์— ๋”ฐ๋ผ, Peer๊ฐ€ ๋ฐฉ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ฐธ๊ฐ€(์‹ฑ๊ธ€ ํ”Œ๋ ˆ์ด์–ด ๋ชจ๋“œ์—์„œ๋Š” X)
  • ์ฐธ๊ณ 
    • ๋„คํŠธ์›Œํฌ์— ์—ฐ๊ฒฐ๋œ Peer๋Š” ํ•˜๋‚˜์˜ NetworkRunner ์ธ์Šคํ„ด์Šค ์‚ฌ์šฉ
    • ๊ฒŒ์ž„ ์„ธ์…˜์—์„œ ์—ฐ๊ฒฐ ํ•ด์ œ or ์—ฐ๊ฒฐ ์‹คํŒจ ์‹œ ํ•ด๋‹น NetworkRunner๋Š” ํŒŒ๊ดด ํ•„์š”
    • ์ƒˆ๋กœ์šด ๊ฒŒ์ž„ ์„ธ์…˜์„ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ์ƒˆ๋กœ์šด NetworkRunner ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

์ฃผ์š” ํ”„๋กœํผํ‹ฐ/๋ฉ”์„œ๋“œ

1. ํ”„๋กœํผํ‹ฐ

  • Config[get] Returns the NetworkProjectConfig reference.
  • Instances[get] A list of all NetworkRunners.
  • DeltaTime[get] Returns the fixed tick time interval. Derived from the SimulationConfig.TickRate.
  • IsClient[get] / IsServer[get] / IsPlayer[get] / IsSinglePlayer[get] / IsSharedModeMasterClient[get]
  • IsRunning[get] Returns if this Fusion.Simulation is valid and running.
  • LocalPlayer[get] Returns a PlayerRef for the local simulation. For a dedicated server PlayerRef.IsValid will equal false. PlayerRefs are assigned in order from 0 to MaxPlayers-1 and are re-used as players join and leave. The only caveat is that the server player (if one exists), always gets the last index no matter how many clients are connected.
  • Tick[get] The tick associated with the current state of networked objects, or the current simulation tick being processed (when evaluated during FixedUpdateNetwork).
  • State[get] The current state of the runner, if it's Starting, Running, Shutdown.
  • DeltaTime[get] Returns the fixed tick time interval. Derived from the SimulationConfig.TickRate.

2. ๋ฉ”์„œ๋“œ

  • Despawn (NetworkObject networkObject, bool allowPredicted=false) Destroys a NetworkObject.
  • Disconnect (PlayerRef player) Disconnect a player from the server.
  • Exists (NetworkId id) Returns if the Fusion.Simulation contains a NetworkObject with given id in the current State SimulationSnapshot.
  • Exists (NetworkObject obj) Returns if the Fusion.Simulation contains a reference to a NetworkObject in the current State SimulationSnapshot.
  • FindObject (NetworkId oref) Get the NetworkObject instance for this NetworkRunner from a NetworkId.
  • GetPlayerObject (PlayerRef player) Gets the network object associated with a specific player.
  • GetPlayerUserId (PlayerRef player=default) Gets Player's UserID.
  • HasAnyActiveConnections ()
  • IsPlayerActive (PlayerRef player) / IsPlayerValid (PlayerRef player)
  • SendRpc (SimulationMessage message {, out RpcSendResult info}) Sends RPC message. Not meant to be used directly, ILWeaver calls this.
  • SetPlayerObject (PlayerRef player, NetworkObject networkObject) Sets the network object associated with this player.
  • Shutdown (bool destroyGameObject=true, ShutdownReason shutdownReason=ShutdownReason.Ok, bool forceShutdownProcedure=false)
  • Spawn (GameObject prefab, Vector3? position=null, Quaternion? rotation=null, PlayerRef? inputAuthority=null, OnBeforeSpawned onBeforeSpawned=null, NetworkObjectPredictionKey? predictionKey=null, bool syncPhysics=true) Attempts to network instantiate a NetworkObject using a GameObject. The supplied GameObject must have a NetworkObject component.
  • StartGame (StartGameArgs args) Starts the local Fusion Runner and takes care of all major setup necessary.
  • TryFindBehaviour (NetworkBehaviourId bref, out NetworkBehaviour behaviour) Get the NetworkBehaviour instance for this NetworkRunner from a NetworkBehaviourId.

์ƒ์„ธ

Runner ์ง€์› ์ปดํฌ๋„ŒํŠธ

  • NetworkRunner ์ปดํฌ๋„ŒํŠธ ์‹œ์ž‘ ์‹œ, ๋ชจ๋“  ์ž์‹ SimulationBehaviour ์ปดํฌ๋„ŒํŠธ ๋“ฑ๋ก
    โ†’ FixedUpdateNetwork(), Render() ์ฝœ๋ฐฑ
  • INetworkRunnerCallback ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ชจ๋“  ์ž์‹ ์ปดํฌ๋„ŒํŠธ ์ฝœ๋ฐฑ ๋“ฑ๋ก

1. ๋‚ด์žฅ Fusion Runner ์ปดํฌ๋„ŒํŠธ

  • HitBoxManager ๋Ÿฐํƒ€์ž„์— ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ / ํžˆํŠธ๋ฐ•์Šค์™€ ํžˆํŠธ๋ฐ•์Šค ํžˆ์Šคํ† ๋ฆฌ ๊ด€๋ฆฌ
  • RunnerLagCompensationGizmos ์ง€์—ฐ ๋ณด์ƒ ๊ธฐ์ฆˆ๋ชจ ํ™œ์„ฑํ™”
  • RunnerAOIGizmos ๊ด€์‹ฌ์˜์—ญ(Area Of Interest) ๊ธฐ์ฆˆ๋ชจ ํ™œ์„ฑํ™”
  • RunnerEnableVisibility
    • ๋‹ค์ค‘ Peer ๋ชจ๋“œ์—์„œ Runner ๊ฐ€์‹œ์„ฑ ํ™œ์„ฑํ™”
    • ๋ชจ๋“  Scene ๋ฐ ์Šคํฐ๋œ ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ๊ฐ€์‹œ์„ฑ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ๋“ฑ๋ก
    • Runner Visibility Controls ์ฐฝ ์ž‘๋™์— ํ•„์š”

์‚ฌ์šฉ์ž ์ •์˜ SimulationBehaviour Runner ์ปดํฌ๋„ŒํŠธ

NetworkRunner ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ์— ์ถ”๊ฐ€(๋‘˜ ์ค‘ ํ•˜๋‚˜)

  • SimulationBehaviour ์ƒ์†๋ฐ›๋Š” ์ปดํฌ๋„ŒํŠธ
  • INetworkRunnerCallbacks ๊ตฌํ˜„ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ
    โ†’ NetworkRunner.StartGame() ํ˜ธ์ถœ ์‹œ NetworkRunner ์ธ์Šคํ„ด์Šค์— ์ž๋™ ๋“ฑ๋ก ํ›„, ์ ์ ˆํ•œ ์‹œ์ ์— ๊ด€๋ จ ์ฝœ๋ฐฑ ํ˜ธ์ถœ

Player / PlayerRef ๊ตฌ์กฐ์ฒด

  • [์ „์šฉ ์„œ๋ฒ„/๊ณต์œ  ๋ชจ๋“œ ๊ฒŒ์ž„ ์„œ๋ฒ„ ์ œ์™ธ] ๋ชจ๋“  Peer๋Š” ์ž…๋ ฅ์„ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์šฉ์ž๋กœ ๊ฐ„์ฃผ
  • NetworkRunner์—๋Š” ๊ด€๋ จ๋œ PlayerRef ๊ตฌ์กฐ์ฒด ๊ฐ’ ๋ณด์œ 
    • ๋กœ์ปฌ(ํด๋ผ์ด์–ธํŠธ) PlayerRef = Runner.LocalPlayer
    • ์„œ๋ฒ„ PlayerRef.None
  • PlayerRef
    • ๋„คํŠธ์›Œํฌ ์˜ค๋ธŒ์ ํŠธ์— ๋Œ€ํ•œ Peer์˜ ์ž…๋ ฅ/์ƒํƒœ ๊ถŒํ•œ ๋ณด์œ  ์—ฌ๋ถ€
    • RPC(Remote Procedure Calls)์˜ Peer์„ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ

  • Peer ์—ฐ๊ฒฐ ๋ฐ ์ „์†ก ์ฒ˜๋ฆฌ
  • ๋งค์น˜๋ฉ”์ดํ‚น ์„œ๋ฒ„, ๋ฃธ ์„œ๋ฒ„, ๊ฒŒ์ž„ ์„œ๋ฒ„์— ๋Œ€ํ•œ ์—ฐ๊ฒฐ ๊ด€๋ฆฌ

Tick ๊ด€๋ฆฌ

  • Tick simulation, re-simulation ๊ฒฐ์ •
  • Tick simulation ์‹œ FixedUpdateNetwork(), simulation ๊ด€๋ จ๋œ ์ด๋ฒคํŠธ ํ˜ธ์ถœ

๋„คํŠธ์›Œํฌ ์˜ค๋ธŒ์ ํŠธ ๊ด€๋ฆฌ

๋„คํŠธ์›Œํฌ ์˜ค๋ธŒ์ ํŠธ Lifecycle(spawn, despawn etc), replication, Scene management, all networking, ๊ด€๋ จ ์ฝœ๋ฐฑ ๊ด€๋ฆฌ

Phycis Scene, ๊ด€์‹ฌ (์˜์—ญ) ๊ด€๋ฆฌ

์ด๋ฒคํŠธ ํ•จ์ˆ˜

๋ชจ๋“  NetworkBehaviour ์ด๋ฒคํŠธ์™€ ํƒ€์ด๋ฐ ์„ธ๊ทธ๋จผํŠธ๋Š” NetworkRunner ์ปดํฌ๋„ŒํŠธ์—์„œ ์‹œ์ž‘

  • SimulationBehabiour ๊ฐ€์ƒ ์ด๋ฒคํŠธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜
    • FixedUpdateNetwork()
    • Render()
  • NetworkBehaviour ๊ฐ€์ƒ ์ด๋ฒคํŠธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜
    • Spawned()
    • Despawned()
  • ์ถ”๊ฐ€์ ์œผ๋กœ ์ด๋ฒคํŠธ ์ฝœ๋ฐฑ์„ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด ์ฃผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค
    • IAfterSpawned
    • IBeforeAllTicks
    • IAfterAllTicks
    • IBeforeTick
    • IAfterTick
    • IPlayerJoined
    • IPlayerLeft