unity 通过使用 photon networking Pun 实现 HTC Vive VR的多人联网。进阶版 《一》

——pun的配置,以及实现头盔以及两个手柄的同步。

下载完pun并导入后如图所示


有这些东西,pun的介绍还是很清楚的,有很多demo可以学习,而且文档自带。很容易上手。

首先,就是服务器的配置了。就是图中箭头所指向的。打开它,


可以看到很多设置,很显然一般最重要的都在最上面,也就是hosting 主机。因为我将photon的服务器装到了自己的主机上,所以我连的就是我自己的photon服务器。主机有很多选项,主要是欧洲,美洲的。(听说国内也已经有了)(其实也是因为国外的经常掉线我才捣鼓着把服务器装到自己电脑上的。如果你想了解这一部分的知识的话,那你就告诉我)如果你是链接自己服务器的话IP自然不用说,端口号要你看是Tcp连接方式还是Udp连接方式。

配置好后就是连接服务器了。这里讲一下他的连接模式。(感觉这个用词也不太好)。pun和现在主流的网络游戏一样都是基于大厅,房间的模式。就像DOTA,LOL这样,我们进入大厅后,只能说是连接到服务器,还要看我们进入哪些房间,房间有多少人,房间的一些规则。这些。当然也可以随机加入房间。(怎么说呢,我的描述我觉得还是有点问题,但是玩过这类游戏的话,肯定很明白,明白意思就好)。

下面代码

[csharp]  view plain  copy
  1. public class ConnectAndJoinRandom : Photon.MonoBehaviour  
  2. {  
  3.     /// <summary>Connect automatically? If false you can set this to true later on or call ConnectUsingSettings in your own scripts.</summary>  
  4.     public bool AutoConnect = true;  
  5.   
  6.     public byte Version = 1;  
  7.   
  8.     /// <summary>if we don't want to connect in Start(), we have to "remember" if we called ConnectUsingSettings()</summary>  
  9.     private bool ConnectInUpdate = true;  
  10.      
  11.   
  12.        
  13.   
  14.     public void Start()  
  15.     {  
  16.         //Debug.Log("当前是否连接" + PhotonNetwork.connected);  
  17.         PhotonNetwork.autoJoinLobby = false;    // we join randomly. always. no need to join a lobby to get the list of rooms.  
  18.         if (PhotonNetwork.connected)  
  19.         {  
  20.             PhotonNetwork.JoinRandomRoom();  
  21.         }  
  22.     }  
  23.   
  24.     public virtual void Update()  
  25.     {  
  26.         if (ConnectInUpdate && AutoConnect && !PhotonNetwork.connected)  
  27.         {  
  28.             Debug.Log("Update() was called by Unity. Scene  is loaded. Let's connect to the Photon Master Server. Calling: PhotonNetwork.ConnectUsingSettings();");  
  29.   
  30.             ConnectInUpdate = false;  
  31.             PhotonNetwork.ConnectUsingSettings(Version + "." + SceneManagerHelper.ActiveSceneBuildIndex);  
  32.         }  
  33.   
  34.          
  35.     }  
  36.   
  37.   
  38.     // below, we implement some callbacks of PUN  
  39.     // you can find PUN's callbacks in the class PunBehaviour or in enum PhotonNetworkingMessage  
  40.   
  41.   
  42.     public virtual void OnConnectedToMaster()  
  43.     {  
  44.         Debug.Log("OnConnectedToMaster() was called by PUN. Now this client is connected and could join a room. Calling: PhotonNetwork.JoinRandomRoom();");  
  45.         PhotonNetwork.JoinRandomRoom();  
  46.   
  47.     }  
  48.   
  49.     public virtual void OnJoinedLobby()  
  50.     {  
  51.         Debug.Log("OnJoinedLobby(). This client is connected and does get a room-list, which gets stored as PhotonNetwork.GetRoomList(). This script now calls: PhotonNetwork.JoinRandomRoom();");  
  52.         PhotonNetwork.JoinRandomRoom();  
  53.     }  
  54.   
  55.     public virtual void OnPhotonRandomJoinFailed()  
  56.     {  
  57.         Debug.Log("OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);");  
  58.         PhotonNetwork.CreateRoom(nullnew RoomOptions() { MaxPlayers = 4 }, null);  
  59.     }  
  60.   
  61.     // the following methods are implemented to give you some context. re-implement them as needed.  
  62.   
  63.     public virtual void OnFailedToConnectToPhoton(DisconnectCause cause)  
  64.     {  
  65.         Debug.LogError("Cause: " + cause);  
  66.     }  
  67.   
  68.     public void OnJoinedRoom()  
  69.     {  
  70.         Debug.Log("OnJoinedRoom() called by PUN. Now this client is in a room. From here on, your game would be running. For reference, all callbacks are listed in enum: PhotonNetworkingMessage");  
  71.     }  
  72. }  
上面这是一段自动加入房间的代码。
勾选上autoconnect 挂到空物体下,在初始化的时候就可以链接到服务器,当然我还是建议,在没有很熟悉的情况下,在他的demo上面进行添加修改,这样不会出错。


建议使用这个demo。有脚本实时显示着链接状态,非常好用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值