初始设置
创建4个场景:Init,Start,Lobby,Game
在Init场景中添加GameManager,以及NetManager脚本
GameManager
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : NetworkBehaviour
{
public static GameManager Instance;
void Start()
{
Instance = this;
DontDestroyOnLoad(gameObject);
SceneManager.LoadScene(1);
}
public void LoadScene(string sceneName)
{
NetworkManager.SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
}
}
NetworkManager
1.制作玩家预制体,需要加上NetworkObject脚本
2.将玩家预制体添加到NetworkManager的PlayerPrefab中
3.添加一个NetworkPrefabsLists
4.设置为UnityTransport
连接服务器
LobbyCtr的作用是,每有一个玩家进入服务器,在UI滚动列表上加上一个带有玩家名字的Cell
public class LobbyCtr : NetworkBehaviour
{
public Transform canvas;
public Transform content;
public GameObject goCell;
public Toggle togReady;
public Button btnStartGame;
Dictionary<ulong, UICell> dicAllCell;
Dictionary<ulong, PlayerInfo> dicAllPlayerInfos;
public void AddPlayer(PlayerInfo playerInfo)
{
GameObject go = Instantiate(goCell);
go.SetActive(true);
go.transform.SetParent(content, false);
var cell = go.GetComponent<UICell>();
cell.Init(playerInfo);
dicAllCell.Add(playerInfo.id, cell);
dicAllPlayerInfos.Add(playerInfo.id, playerInfo);
}
/// <summary>
/// 服务器客户端都会调用
/// </summary>
public override void OnNetworkSpawn()
{
if (IsServer)
{
NetworkManager.OnClientConnectedCallback += OnClientConnect;
}
Debug.Log("OnNetworkSpawn");
dicAllPlayerInfos = new Dictionary<ulong, PlayerInfo>();
dicAllCell = new Dictionary<ulong, UICell>();
btnStartGame.onClick.AddListener(OnBtnStartGameClick);
togReady.onValueChanged.AddListener(OnReadyChange);
PlayerInfo playerInfo = new PlayerInfo();
playerInfo.id = NetworkManager.LocalClientId;
playerInfo.isready = false;
AddPlayer(playerInfo);
base.OnNetworkSpawn();
}
/// <summary>
/// 服务器在收到其他玩家进入时调用
/// </summary>
/// <param name="obj"></param>
private void OnClientConnect(ulong obj)
{
Debug.Log("NetworkManager_OnClientConnectedCallback");
PlayerInfo playerInfo = new PlayerInfo();
playerInfo.id = obj;
playerInfo.isready = false;
AddPlayer(playerInfo);
UpdateAllPlayerInfos();
}
/// <summary>
/// 服务器通知所有玩家刷新状态
/// </summary>
void UpdateAllPlayerInfos()
{
foreach (var item in dicAllPlayerInfos)
{
UpdatePlayerInfoClientRpc(item.Value);
}
}
/// <summary>
/// 服务器通知客户端刷新状态
/// </summary>
/// <param name="playerInfo"></param>
[ClientRpc]
void UpdatePlayerInfoClientRpc(PlayerInfo playerInfo)
{
Debug.Log("UpdatePlayerInfoClientRpc:" + playerInfo.id);
//服务器自己也会调用,所以判断客户端才执行此方法
if (!IsServer)
{
if (dicAllPlayerInfos.ContainsKey(playerInfo.id))
{
dicAllPlayerInfos[playerInfo.id] = playerInfo;
}
else
{
AddPlayer(playerInfo);
}
UpdatePlayerCells();
}
}
private void UpdatePlayerCells()
{
foreach (var item in dicAllPlayerInfos)
{
dicAllCell[item.Key].SetReady(item.Value.isready);
}
}
/// <summary>
/// 客户端通知服务器刷新状态
/// </summary>
[ServerRpc(RequireOwnership = false)]
void UpdateAllPlayerInfoServerRpc(PlayerInfo playerInfo)
{
dicAllPlayerInfos[playerInfo.id] = playerInfo;
dicAllCell[playerInfo.id].SetReady(playerInfo.isready);
UpdateAllPlayerInfos();
}
private void OnBtnStartGameClick()
{
}
/// <summary>
/// 准备
/// </summary>
private void OnReadyChange(bool arg0)
{
//刷新本地UI
dicAllCell[NetworkManager.LocalClientId].SetReady(arg0);
//更改本地值
UpdatePlayerInfo(NetworkManager.LocalClientId, arg0);
//通知RPC
if (IsServer)
{
//服务器直接通知所有客户端
UpdateAllPlayerInfos();
}
else
{
//通知服务器
UpdateAllPlayerInfoServerRpc(dicAllPlayerInfos[NetworkManager.LocalClientId]);
}
}
/// <summary>
/// 修改本地值
/// </summary>
void UpdatePlayerInfo(ulong id, bool isReady)
{
PlayerInfo playerInfo = dicAllPlayerInfos[id];
playerInfo.isready = isReady;
dicAllPlayerInfos[id] = playerInfo;
}
}
注意要点:
1.LobbyCtr需要继承NetworkBehaviour类
2.实现OnNetworkSpawn方法,OnNetworkSpawn方法在Start方法前执行
3.服务器需要在OnNetworkSpawn方法中添加监听客户端加入的接口
4.客户端加入服务器后,服务器统一转发给所有客户端
5.如果是服务器,直接转发给其他客户端
6.如果是客户端,需要先发给服务器,再由服务器转发给其他客户端
7.服务器调用客户端的方法以ClientRpc结尾,并在方法头加上[ClientRpc]
特性
8.客户端调用服务器的方法以ServerRpc结尾,并在方法头加上[ServerRpc]
特性
PlayerInfo需要继承INetworkSerializable接口序列化
public struct PlayerInfo : INetworkSerializable
{
public ulong id;
public bool isready;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref id);
serializer.SerializeValue(ref isready);
}
}
Variables 和 RPC 消息的区别
- Variables是一定时间同步一次变化
- RPC是每次变化都会同步