FooJiaYin / DogeLife

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Networking API

Reference: https://mirror-networking.gitbook.io/docs/

Setup

Reference: https://mirror-networking.gitbook.io/docs/guides/networkbehaviour

Using ...;
Using ...;
Using Mirror;

// public class Example : MonoBehaviour
public class Example : NetworkBehaviour

Player

  • When a new client is connected to the network, a new player instance is spawned on all connected clients.
  • There will be many player game objects in the scene.
  • In player.cs
    public class player : NetworkBehaviour
    {
        void Example() {
            // Code applied to the local player and all other players
            if (isLocalPlayer) {
                // Code applied to the local player only
            }
        }
    }
  • Since the player is now spawned dynamically, any game object reference (such as ScoreDisplay) should be assigned with code (not dragged from the scene)

Spawning GameObject

Example: SpawnFood() in GameManager.cs

/* When we want to spawn a game object */

// Spawn only at the server
if (!isServer) return;

var newObject = Instantiate(prefab, transform.position, transform.rotation);

// Tell other clients to spawn the game object
NetworkServer.Spawn(newObject);
  • Game object to be synced between all clients must be spawned only at the server and then synced to the clients
  • Game object should spawned from prefab, not GameObject instance from the scene
    • There should not be any game object instance in the scene because the instance will be independent in each client (not synced)
  • Never spawn game object as child to other game object, because NetworkServer.Spawn() takes its local transform as global transform.
    • If there is a need, we will have to write a custom spawn function

Volume

  1. Add Volume.cs to GameManager or any other game object
  2. Access the volume:
    // In GameManager (or any game object that Volume.cs is attached to)
    Volume vol = gameObject.GetComponent<Volume>();
    // In other game object
    Volume vol = FindObjectOfType<Volume>();
    // Example Usage
    Debug.log(vol.volume); // vol.volume is a float

About


Languages

Language:C# 99.2%Language:JavaScript 0.7%Language:HTML 0.1%Language:ShaderLab 0.0%Language:Smalltalk 0.0%