PhotonFusion 发布以来备受关注,之前用PUN做的游戏在升级的时候发现本地被限制了,需要很繁琐的备案,诸如此类的麻烦问题,加之新的fusion的使用宣传比之前的pun realtime两个版本效率上都要高,因此将游戏重新改版。客观说,之前的PUN 1.0与PUN 2.0升级换代后的变化不太大,API变化少,例子基本都没变,但Fusion不太一样,变化太大。重新记录一些片段,有需要的可以交流。
1.官方不建议做大厅的游戏房间列表,用即时的游戏匹配来代替,我觉得官方说的比较中肯。也否定了之前的做法,因为维护游戏房间列表对于服务器来说压力太大,玩家选择房间也很难找。选择Matchmaker
2.房间在记录自定义属性时,要注意尽量少用,必要用的时候名称一定要简短。具体使用方法如下:
// Some predefined types used as values for the Game Session Properties
public enum GameType : int {
FreeForAll,
Team,
Timed
}
public enum GameMap : int {
Forest,
City,
Desert
}
// Utility method to start a Host using a defined GameMap and GameType
public async Task StartHost(NetworkRunner runner, GameMap gameMap, GameType gameType) {
var customProps = new Dictionary<string, SessionProperty>();
customProps["map"] = (int)gameMap;
customProps["type"] = (int)gameType;
var result = await runner.StartGame(new StartGameArgs() {
GameMode = GameMode.Host,
SessionProperties = customProps,
});
if (result.Ok) {
// all good
} else {
Debug.LogError($"Failed to Start: {result.ShutdownReason}");
}
}
上面是创建游戏房间,自定义房间属性。下面是加入房间代码。
public async Task StartClient(NetworkRunner runner, GameType gameType) {
var customProps = new Dictionary<string, SessionProperty>() {
{ "type", (int)gameType }
};
var result = await runner.StartGame(new StartGameArgs() {
GameMode = GameMode.Client,
SessionProperties = customProps,
});
if (result.Ok) {
// all good
} else {
Debug.LogError($"Failed to Start: {result.ShutdownReason}");
}
}