Skip to content

Commit

Permalink
refactor: adding sample code for custom character spawning
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Apr 30, 2022
1 parent 37f8e4f commit a6384f5
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Assets/Mirage/Samples~/SpawnCustomPlayer.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Mirage;
using UnityEngine;

namespace Example.CustomCharacter
{
[NetworkMessage]
public struct CreateMMOCharacterMessage
{
public Race race;
public string name;
public Color hairColor;
public Color eyeColor;
}

public enum Race
{
Human,
Elvish,
Dwarvish,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions Assets/Mirage/Samples~/SpawnCustomPlayer/CustomCharacterSpawner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.ComponentModel;
using Mirage;
using UnityEngine;

namespace Example.CustomCharacter
{
public class CustomCharacterSpawner : MonoBehaviour
{
[Header("References")]
public NetworkClient Client;
public NetworkServer Server;
public ClientObjectManager ClientObjectManager;
public ServerObjectManager ServerObjectManager;

[Header("Prefabs")]
// different prefabs based on the Race the player picks
public CustomCharacter HumanPrefab;
public CustomCharacter ElvishPrefab;
public CustomCharacter DwarvishPrefab;

public void Start()
{
Client.Started.AddListener(OnClientStarted);
Client.Authenticated.AddListener(OnClientAuthenticated);
Server.Started.AddListener(OnServerStarted);
}

void OnClientStarted()
{
// make sure all prefabs are Register so mirage can spawn the character for this client and for other players
ClientObjectManager.RegisterPrefab(HumanPrefab.Identity);
ClientObjectManager.RegisterPrefab(ElvishPrefab.Identity);
ClientObjectManager.RegisterPrefab(DwarvishPrefab.Identity);
}

// you can send the message here if you already know
// everything about the character at the time of player
// or at a later time when the user submits his preferences
void OnClientAuthenticated(INetworkPlayer player)
{
var mmoCharacter = new CreateMMOCharacterMessage
{
// populate the message with your data
name = "player name",
race = Race.Human,
eyeColor = Color.red,
hairColor = Color.black,
};
player.Send(mmoCharacter);
}

void OnServerStarted()
{
// wait for client to send us an AddPlayerMessage
Server.MessageHandler.RegisterHandler<CreateMMOCharacterMessage>(OnCreateCharacter);
}

void OnCreateCharacter(INetworkPlayer player, CreateMMOCharacterMessage msg)
{
CustomCharacter prefab = GetPrefab(msg);

// create your character object
// use the data in msg to configure it
CustomCharacter character = Instantiate(prefab);

// set syncVars before telling mirage to spawn character
// this will cause them to be sent to client in the spawn message
character.PlayerName = msg.name;
character.hairColor = msg.hairColor;
character.eyeColor = msg.eyeColor;

// spawn it as the character object
ServerObjectManager.AddCharacter(player, character.Identity);
}

CustomCharacter GetPrefab(CreateMMOCharacterMessage msg)
{
// get prefab based on race
CustomCharacter prefab;
switch (msg.race)
{
case Race.Human: prefab = HumanPrefab; break;
case Race.Elvish: prefab = ElvishPrefab; break;
case Race.Dwarvish: prefab = DwarvishPrefab; break;
// default case to check that client sent valid race.
// the only reason it should be invalid is if the client's code was modified by an attacker
// throw will cause the client to be kicked
default: throw new InvalidEnumArgumentException("Invalid race given");
}

return prefab;
}
}
public class CustomCharacter : NetworkBehaviour
{
[SyncVar] public string PlayerName;
[SyncVar] public Color hairColor;
[SyncVar] public Color eyeColor;

private void Awake()
{
Identity.OnStartClient.AddListener(OnStartClient);

}

private void OnStartClient()
{
// use name and color syncvars to modify renderer settings
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Assets/Mirage/Samples~/SpawnCustomPlayer/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This Sample contains full scripts for the Custom Character Spawning guide.

See the guide here:
https://miragenet.github.io/Mirage/Articles/Guides/GameObjects/SpawnPlayerCustom.html

0 comments on commit a6384f5

Please sign in to comment.