참고: UNet은 지원이 중단되었으며 향후 Unity에서 삭제될 예정입니다. 현재 새로운 시스템이 개발 중입니다. 자세한 내용과 다음 단계는 이 블로그 포스트를 참조하십시오. |
상태 동기화는 네트워크로 연결된 게임 오브젝트의 스크립트에 속하는 값(예: 정수, 부동 소수점, 문자열, boolean 값)을 동기화하는 것을 말합니다.
상태 동기화는 서버에서 원격 클라이언트로 수행됩니다. 로컬 클라이언트는 서버와 씬을 공유하므로 데이터가 직렬화되지 않습니다. 하지만 SyncVar 후크는 로컬 클라이언트에서도 호출됩니다.
데이터는 반대 방향, 즉 원격 클라이언트에서 서버로 동기화되지 않습니다. 이렇게 하려면 커맨드를 사용해야 합니다.
SyncVar는 NetworkBehaviour에서 상속받는 스크립트 변수로, 서버에서 클라이언트로 동기화됩니다. 게임 오브젝트가 스폰되거나 새로운 플레이어가 진행 중인 게임에 참여하는 경우 현재 볼 수 있는 네트워크 오브젝트의 모든 SyncVar 최신 상태를 전달받습니다. 동기화할 특정 스크립트 변수를 지정하려면 다음과 같이 [SyncVar]
커스텀 속성을 사용하십시오.
class Player : NetworkBehaviour {
[SyncVar]
int health;
public void TakeDamage(int amount)
{
if (!isServer)
return;
health -= amount;
}
}
SyncVar 상태는 OnStartClient()가 호출되기 이전에 클라이언트의 게임 오브젝트에 적용되므로, 오브젝트 상태는 OnStartClient() 내부에서 항상 최신 상태를 유지합니다.
SyncVar는 정수, 문자열, 플로트와 같은 기본 타입이거나 Vector3나 사용자가 정의한 구조체와 같은 Unity 타입일 수 있지만, SyncVar 구조체 업데이트는 구조체 변화에서 점진적으로 이루어지는 것이 아니라 한 번에 진행되도록 전송됩니다. 하나의 NetworkBehaviour 스크립트에는 SyncList를 포함하여 최대 32개의 SyncVar가 있을 수 있습니다(아래의 다음 섹션 참조).
SyncVar 값이 변경되면 서버가 SyncVar 업데이트를 자동으로 전송하므로 변경을 추적하거나 변경에 관한 정보를 직접 전송할 필요가 없습니다.
SyncVar에는 값이 포함되는 반면, SyncList에는 값 리스트가 포함되어 있습니다. SyncList 컨텐츠는 SyncVar 상태와 함께 초기 상태 업데이트에 포함됩니다. SyncList는 자체 콘텐츠를 동기화하는 클래스이므로 SyncList에는 SyncVar 속성이 필요하지 않습니다. 다음의 SyncList를 기본 타입으로 사용할 수 있습니다.
SyncListString
SyncListFloat
SyncListInt
SyncListUInt
SyncListBool
또한 고유 구조체 타입 리스트를 동기화하는 데 사용할 수 있는 SyncListStruct도 제공됩니다. SyncListStruct를 사용하는 경우 사용하려는 구조체 타입에 기본 타입 멤버, 배열 및 일반 Unity 타입이 포함됩니다. 복잡한 클래스나 일반 컨테이너는 포함할 수 없으며, 이 구조체의 public 변수만 직렬화됩니다.
SyncList는 콜백이라는 SyncListChanged 델리게이트를 통해 리스트 콘텐츠가 변경되면 클라이언트에 알릴 수 있도록 합니다. 이 델리게이트는 발생한 작업의 타입과 작업 대상이었던 아이템의 색인으로 호출됩니다.
public class MyScript : NetworkBehaviour
{
public struct Buf
{
public int id;
public string name;
public float timer;
};
public class TestBufs : SyncListStruct<Buf> {}
TestBufs m_bufs = new TestBufs();
void BufChanged(Operation op, int itemIndex)
{
Debug.Log("buf changed:" + op);
}
void Start()
{
m_bufs.Callback = BufChanged;
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.