HowToReplicatePosition.md
This document explains how to synchronize the position of GameObjects using the Diarkis plugin.
Table of Contents
1. Project Setup
First, create a new Unity project.
Next, import DiarkisUnityPlugin.unitypackage
(for details, refer to HowToUsePlugin.md).
Finally, set the connection information for Diarkis on the DiarkisNetworkManager
prefab and place it in the scene.
This completes the setup.
2. Adding Assets
Here, we will explain the steps to synchronize a character using Unity's standard assets. For example, we will use "Third Person Character Controller" available in Unity's Asset Store. You can replace this with any asset you are using as needed.
Download and import Third Person Character Controller
from the Package Manager.
Click the Import button in the Unity Package Import window.
For sample work, create a Sample folder under Assets > Diarkis
and create the following three folders:
Use the background data associated with Third Person Character Controller
. Copy the file from Assets > StarterAssets > ThirdPersonController > Scenes > Playground
to Assets > Diarkis > Sample > Scenes
, and rename it to DiarkisSampleScene
.
2. Automatic Room Creation
To synchronize positions, you need to join some room (Room
).
To simplify things, we will automatically create a room using Room.JoinRandom
when the scene is played.
Sample code for this document is available in the HowToReplicatePostion_SampleCode
folder. You can overwrite and use the provided Scripts
folder in Assets > Diarkis > Sample > Scripts
.
Create an empty GameObject by right-clicking and selecting
Create Empty
, then rename it toSceneManager
.Drag and drop
DiarakiSampleGameManager.cs
fromDiarkis > Sample > Scripts > Game
to attach it.
Similarly, create another object named
CoreEvent
.Drag and drop
DiarkisCoreEventCallback.cs
fromDiarkis > Runtime > Extension > MonoBehaviour > Callbacks
to attach it.Specify
DiarakiSampleGameManager.OnNetworkConnect
ofSceneManager
toOn Network Connect
.
Similarly, create another object named
RoomEvent
.Drag and drop
DiarkisRoomRepManager.cs
fromDiarkis > Runtime > Extension > MonoBehaviour > Replication > Room
to attach it.Specify the various callbacks (OnRoomCreate, OnRoomJoin, OnRoomLeave, OnRoomMemberJoin, OnRoomMemberLeave).
Select
SceneManager
.Attach
RoomEvent
toRoom Rep Manager
.
DiarkisRoomRepManager
is a class that inherits from DiarkisRoomEventCallback
and not only handles Room
events but also comes with various useful replication features.
3. Creating the Character Prefab
We will now create the character that the player will control.
Copy
PlayerArmature
fromAssets > StarterAssets > ThirdPersonController > Prefabs
toAssets > Diarkis > Sample > Prefabs
, and rename it toDiarkisPlayerArmature
.
To synchronize a character's position, inherit DiarkisGameObject
and correctly implement the following three functions:
Function Name | Role |
---|---|
HasPositionSyncData | Checks if there is position synchronization data to send for the local character |
GetPositionSyncData | Retrieves the position synchronization data for the local character |
SetPositionSyncData | Sets the position synchronization data for the remote character |
4. Setting the Spawn
Specify the location where the character will be spawned. Create
PlayerSpawnPoint
and set the spawnPosition
.
Attach
DiarkisPlayerArmature
toPlayerPrefab
ofRoomEvent
.Attach
PlayerSpawnPoint
toAutoSpawnTransform
ofRoomEvent
.
Delete
PlayerArmature
from theDiarkisSampleScene
.
By pressing Play, you can now synchronize the positions of remote characters from other clients.
最終更新