📅  最后修改于: 2022-03-11 14:49:23.297000             🧑  作者: Mango
//You need to create a message handler
public class MMONetworkManager : NetworkManager
{
public override void OnStartServer()
{
base.OnStartServer();
NetworkServer.RegisterHandler(OnCreateCharacter);
}
public override void OnClientConnect(NetworkConnection conn)
{
base.OnClientConnect(conn);
// you can send the message here, or wherever else you want
CreateMMOCharacterMessage characterMessage = new CreateMMOCharacterMessage
{
race = Race.Elvish,
name = "Joe Gaba Gaba",
hairColor = Color.red,
eyeColor = Color.green
};
conn.Send(characterMessage);
}
void OnCreateCharacter(NetworkConnection conn, CreateMMOCharacterMessage message)
{
// playerPrefab is the one assigned in the inspector in Network
// Manager but you can use different prefabs per race for example
GameObject gameobject = Instantiate(playerPrefab);
// Apply data from the message however appropriate for your game
// Typically Player would be a component you write with syncvars or properties
Player player = gameobject.GetComponent();
player.hairColor = message.hairColor;
player.eyeColor = message.eyeColor;
player.name = message.name;
player.race = message.race;
// call this to use this gameobject as the primary controller
NetworkServer.AddPlayerForConnection(conn, gameobject);
}
}