https://docs-multiplayer.unity3d.com/docs/tutorials/helloworld/helloworldintro
Creating Network Manager and selecting the Transport#
In this section we will add a Network Manager and add a Transport to our project.
- Right click in the Hierarchy tab of the Main Unity Window.
- Select Create Empty.
- Rename the GameObject NetworkManager.You have now created a new GameObject called NetworkManager.
- TIP
We renamed the GameObject because:- It makes it easier to refer to later.
- There should only be one NetworkManager, this is the object that contains the NetworkManager component. You may get unexpected results if you create more than one NetworkManager.
- Select NetworkManager.
- Click Add Component in the Inspector Tab.
- Select MLAPI from the list shown.
- Select NetworkManager Component from the list displayed.
- Inside the NetworkManager component tab, locate the NetworkTransport field.
- Click "Select Transport".
- Select UnetTransport.
- Save your scene.
Creating an object to spawn for each connected player
Creating an object to spawn for each connected player#
This section adds in a player object and spawns it for each connected player.
- Create a 3D Object->Capsule
- Rename it Player.
- Add a NetworkObject component.
- Click the Assets folder.
- Create a new Folder and call it Prefabs.
- Make Player a prefab by dragging it to Prefabs folder you just created.
- Delete Player from scene.
- TIP
We remove Player, because we will be using the network library to spawn the player. The library cannot track objects that start in the scene. - Select NetworkManager.
- Inside the NetworkManager component tab, locate the NetworkPrefabs field.
- Click + to create a slot.
- Drag this player prefab from above into the new empty slotNOTE
- You may see the following error reported There is no NetworkPrefab Marked as a PlayerPrefab. Once you have completed the above steps you can clear the error.
- IMPORTANT
- When you select the Default Player Prefab , you are telling the library that when a client connects to the game, automatically spawn this prefab as the character for the connecting client. If you do not have any prefab set as Default Player Prefab the game will crash on client connect.
- Create a 3D Object->Plane, centered at (0,0,0).
- Save your scene
Testing Hello World#
Now we will test to see if evereything works as expected.
- Click Play.
- Click Start Host under NetworkManager.
https://docs-multiplayer.unity3d.com/docs/tutorials/helloworld/helloworldtwo
Adding Scripts to Hello World#
This section will add some scripts to Hello World which will contain the new features we will be covering in the tutorial.
- Click the Assets folder.
- Create a new Folder and call it Scripts.
- Create an empty GameObject rename it HelloWorldManager.
- Create a script called HelloWorldManager.
- Add the HelloWorldManager script as a component.
- Open the HelloWorldManager.cs script.
- Edit the HelloWorldManager.cs script to match the following.
https://youtu.be/wdzkZbG2-18?list=TLGG62O2Dyc_tMcyMzA4MjAyMQ
```
using MLAPI;
using UnityEngine;
namespace MultiPlayer
{
public class HelloWorldManager : MonoBehaviour
{
void OnGUI()
{
GUILayout.BeginArea(new Rect(10, 10, 300, 300));
if (!NetworkManager.Singleton.IsClient && !NetworkManager.Singleton.IsServer)
{
StartButtons();
}
else
{
StatusLabels();
SubmitNewPosition();
}
GUILayout.EndArea();
}
static void StartButtons()
{
if (GUILayout.Button("Host")) NetworkManager.Singleton.StartHost();
if (GUILayout.Button("Client")) NetworkManager.Singleton.StartClient();
if (GUILayout.Button("Server")) NetworkManager.Singleton.StartServer();
}
static void StatusLabels()
{
var mode = NetworkManager.Singleton.IsHost ?
"Host" : NetworkManager.Singleton.IsServer ? "Server" : "Client";
GUILayout.Label("Transport: " +
NetworkManager.Singleton.NetworkConfig.NetworkTransport.GetType().Name);
GUILayout.Label("Mode: " + mode);
}
static void SubmitNewPosition()
{
if (GUILayout.Button(NetworkManager.Singleton.IsServer ? "Move" : "Request Position Change"))
{
if (NetworkManager.Singleton.ConnectedClients.TryGetValue(NetworkManager.Singleton.LocalClientId,
out var networkedClient))
{
var player = networkedClient.PlayerObject.GetComponent<HelloWorldPlayer>();
if (player)
{
player.Move();
}
}
}
}
}
}
```
Adding basic movement to the Player object#
This script adds some basic movement to the Hello World player.
- Create a new script HelloWorldPlayer.
- Open the HelloWorldPlayer.cs script.
- Edit the HelloWorldPlayer.cs script to match the following.
- Select the Player prefab.
- Add the script HelloWorldPlayer script as a component.
- HelloWorldPlayer.cs
```
using MLAPI;
using MLAPI.Messaging;
using MLAPI.NetworkVariable;
using UnityEngine;
namespace MultiPlayer
{
public class HelloWorldPlayer : NetworkBehaviour
{
public NetworkVariableVector3 Position = new NetworkVariableVector3(new NetworkVariableSettings
{
WritePermission = NetworkVariablePermission.ServerOnly,
ReadPermission = NetworkVariablePermission.Everyone
});
public override void NetworkStart()
{
Move();
}
public void Move()
{
if (NetworkManager.Singleton.IsServer)
{
var randomPosition = GetRandomPositionOnPlane();
transform.position = randomPosition;
Position.Value = randomPosition;
}
else
{
SubmitPositionRequestServerRpc();
}
}
[ServerRpc]
void SubmitPositionRequestServerRpc(ServerRpcParams rpcParams = default)
{
Position.Value = GetRandomPositionOnPlane();
}
static Vector3 GetRandomPositionOnPlane()
{
return new Vector3(Random.Range(-3f, 3f), 1f, Random.Range(-3f, 3f));
}
void Update()
{
transform.position = Position.Value;
}
}
}
```
https://youtu.be/Ui8fRj-mK1k?list=TLGGdoBS8_t3G8wyMzA4MjAyMQ
----------------------------------------------------------------------------
Whenever you press the GUI button (which is contextual depending on if you are server or a client), you find your local player and simply call Move().
You can now create a build which will demonstrate the concepts outlined above.
TIP
Make sure SampleScene is included in BuildSettings.
One build instance can create a host. Another client can join the host's game. Both are able to press a GUI button to move. Server will move immediately and be replicated on client. Client can request a new position, which will instruct the server to modify that server instance's position NetworkVariable. That client will apply that NetworkVariable position inside of it's Update() method.
https://youtu.be/khZh7lZPzqc?list=TLGGkOWzxzCFh-gyMzA4MjAyMQ
'tech > metaverse' 카테고리의 다른 글
21/08/23 Unity GUI 관련 자료 모음 (0) | 2021.08.23 |
---|---|
21/08/23 OnGui 사용법 (0) | 2021.08.23 |
21/08/23 Install MLAPI (0) | 2021.08.23 |
21/08/22 Unity C# Events Handling (0) | 2021.08.22 |
21/08/22 Unity Official Learning Tutorial (0) | 2021.08.22 |