tech/metaverse

21/08/23 Your First Networked Game "Hello World"

tech-lover 2021. 8. 23. 10:24

https://docs-multiplayer.unity3d.com/docs/tutorials/helloworld/helloworldintro

 

Your First Networked Game "Hello World" | Unity Multiplayer Networking

Tutorial that explains creating a project, installing the MLAPI package, and creating the basic components for your first networked game.

docs-multiplayer.unity3d.com

https://youtu.be/4Mf81GdEDU8

 

https://youtu.be/NsfwlWaZ0ng

https://youtu.be/ZYEUWzsXEoY

Creating Network Manager and selecting the Transport#

In this section we will add a Network Manager and add a Transport to our project.

  1. Right click in the Hierarchy tab of the Main Unity Window.
  2. Select Create Empty.
  3. Rename the GameObject NetworkManager.You have now created a new GameObject called NetworkManager.
  4. 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.
  5. Select NetworkManager.
  6. Click Add Component in the Inspector Tab.
  7. Select MLAPI from the list shown.
  8. Select NetworkManager Component from the list displayed.
  9. Inside the NetworkManager component tab, locate the NetworkTransport field.
  10. Click "Select Transport".
  11. Select UnetTransport.
  12. Save your scene.

 

Creating an object to spawn for each connected player

https://youtu.be/B_FWb4J1Pxw

Creating an object to spawn for each connected player#

This section adds in a player object and spawns it for each connected player.

  1. Create a 3D Object->Capsule
  2. Rename it Player.
  3. Add a NetworkObject component.
  4. Click the Assets folder.
  5. Create a new Folder and call it Prefabs.
  6. Make Player a prefab by dragging it to Prefabs folder you just created.
  7. Delete Player from scene.
  8. 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.
  9. Select NetworkManager.
  10. Inside the NetworkManager component tab, locate the NetworkPrefabs field.
  11. Click + to create a slot.
  12. Drag this player prefab from above into the new empty slotNOTE
  13. 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.
  14. IMPORTANT
  15. 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.
  16. Create a 3D Object->Plane, centered at (0,0,0).
  17. Save your scene

https://youtu.be/Ee3t0xNF0n8

Testing Hello World#

Now we will test to see if evereything works as expected.

  1. Click Play.
  2. Click Start Host under NetworkManager.

 

 

https://docs-multiplayer.unity3d.com/docs/tutorials/helloworld/helloworldtwo

 

Building on "Hello World" | Unity Multiplayer Networking

Tutorial that explains adding scripts to objects, editor modes (Host Server and Client), basic player movement,Permissions and basic RPC use.

docs-multiplayer.unity3d.com

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.

  1. Click the Assets folder.
  2. Create a new Folder and call it Scripts.
  3. Create an empty GameObject rename it HelloWorldManager.
  4. Create a script called HelloWorldManager.
  5. Add the HelloWorldManager script as a component.
  6. Open the HelloWorldManager.cs script.
  7. 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(1010300300));

            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.

  1. Create a new script HelloWorldPlayer.
  2. Open the HelloWorldPlayer.cs script.
  3. Edit the HelloWorldPlayer.cs script to match the following.
  4. Select the Player prefab.
  5. 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(-3f3f), 1fRandom.Range(-3f3f));

        }

 

        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