-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: adding sample code for custom character spawning
- Loading branch information
1 parent
37f8e4f
commit a6384f5
Showing
6 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
Assets/Mirage/Samples~/SpawnCustomPlayer/CreateMMOCharacterMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Mirage/Samples~/SpawnCustomPlayer/CreateMMOCharacterMessage.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
111 changes: 111 additions & 0 deletions
111
Assets/Mirage/Samples~/SpawnCustomPlayer/CustomCharacterSpawner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Mirage/Samples~/SpawnCustomPlayer/CustomCharacterSpawner.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |