PhotonFusion 使用-Session

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}");
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值