새소식

유니티/Fusion

photon fusion tutorial #3

  • -

https://doc.photonengine.com/ko-kr/fusion/current/tutorials/shared-mode-basics/4-network-properties

 

Fusion 2 4 - Network Properties | Photon Engine

This section shows how to synchronize additional data over the network in addition to the player's position using . Fusion synchronizes the transform

doc.photonengine.com

 

1. 네트워크 사용하여 스크립트 내 변수 변경

기본적으로 NetworkObjectsFusion은 NetworkTransform과 같은 Fusion 기능을 자동으로 동기화해준다.

 

[Networked] 을 가진 변수만 StateAuthority 통해 동기화가 가능

클라이언트가 개체의 네트워크 속성 변수를 변경하는 경우 우선적으로 예측값이 적용되고 그 후 서버를 통한 동기화가 이루어진다.

 

동기화를 원하는 오브젝트는 

[Networked] 키워드를 추가하고,

{get; set;} 프로퍼티를 추가한다.

 

[Networked, OnChangedRender(nameof(ColorChanged))]
public Color NetworkedColor { get; set; }

void ColorChanged()
{
    MeshRenderer.material.color = NetworkedColor;
}

해당 내용은 NetworkedColor 가 변경될때마다 Unity Update에서  nameof에 참조된 함수가 호출된다.

 

 

 

 

2. RPC  네트워크 상 다른 오브젝트의 함수 호출하기

* NetworkBehaviour 에서만 사용가

 

체력 변경 사항을 감지하여 자동으로 동기화시켜주는 역할 및 호출함수 지정

using Fusion;
using UnityEngine;

public class Health : NetworkBehaviour
{
    [Networked, OnChangedRender(nameof(HealthChanged))]
    public float NetworkedHealth { get; set; } = 100;

    void HealthChanged()
    {
        Debug.Log($"Health changed to: {NetworkedHealth}");
    }
}

 

 

RPC를 통한 다른 네트워크 객체와의 소통

RpcSources.All  =>  모든 객체가 호출 가능한 함수

RpcTargets.StateAuthority =>  StateAuthorityRPC 만 수신

해당 구문을 쓰는 이유는 StateAuthority가 Health Networked Property를 업데이트하기 때문에 수행

 

[Rpc(RpcSources.All, RpcTargets.StateAuthority)]
public void DealDamageRpc(float damage)
{
    // The code inside here will run on the client which owns this object (has state and input authority).
    Debug.Log("Received DealDamageRpc on StateAuthority, modifying Networked variable");
    NetworkedHealth -= damage;
}

 

 

 

네트워크상 RayCast

Runner로 부터 현재 씬에서의 레이 캐스트를 통한 대상을 가져올 수 있다.

if (Runner.GetPhysicsScene().Raycast(ray.origin,ray.direction, out var hit))
{
    if (hit.transform.TryGetComponent<Health>(out var health))
    {
        health.DealDamageRpc(Damage);
    }
}

 

 

 

 


[Networked] 를 통한 상태 변경을 감시하는 역할로 사용하면 쉽게 상태 변경에 대해 변경이 가능

[RPC] 를 통해 상태 변경을 지정하면 대부분의 상태 변경의 요구 조건을 만족할 수 있다.

 

Peer

 

계층적 구조의 프로토콜을 사용하는 통신망의 동일 프로토콜 계층에서 대등한 지위로 동작하는 기능 단위 또는 장치

'유니티 > Fusion' 카테고리의 다른 글

Fusion 기본 정보  (1) 2024.06.06
[Fusion] Asteroids.SharedSimple 뜯어보기  (0) 2024.06.06
Photon Fusion Tutorial #2  (0) 2024.06.04
Fusion Shared Tutorial #1  (0) 2024.06.04
Photon Fusion Pun2  (0) 2024.06.04
Contents

아핫

땡큐하다