unity 通过使用 photon networking Pun 实现 HTC Vive VR的多人联网。进阶版 《一》
——pun的配置,以及实现头盔以及两个手柄的同步。
下载完pun并导入后如图所示
有这些东西,pun的介绍还是很清楚的,有很多demo可以学习,而且文档自带。很容易上手。
首先,就是服务器的配置了。就是图中箭头所指向的。打开它,
可以看到很多设置,很显然一般最重要的都在最上面,也就是hosting 主机。因为我将photon的服务器装到了自己的主机上,所以我连的就是我自己的photon服务器。主机有很多选项,主要是欧洲,美洲的。(听说国内也已经有了)(其实也是因为国外的经常掉线我才捣鼓着把服务器装到自己电脑上的。如果你想了解这一部分的知识的话,那你就告诉我)如果你是链接自己服务器的话IP自然不用说,端口号要你看是Tcp连接方式还是Udp连接方式。
配置好后就是连接服务器了。这里讲一下他的连接模式。(感觉这个用词也不太好)。pun和现在主流的网络游戏一样都是基于大厅,房间的模式。就像DOTA,LOL这样,我们进入大厅后,只能说是连接到服务器,还要看我们进入哪些房间,房间有多少人,房间的一些规则。这些。当然也可以随机加入房间。(怎么说呢,我的描述我觉得还是有点问题,但是玩过这类游戏的话,肯定很明白,明白意思就好)。
下面代码
- public class ConnectAndJoinRandom : Photon.MonoBehaviour
- {
- /// <summary>Connect automatically? If false you can set this to true later on or call ConnectUsingSettings in your own scripts.</summary>
- public bool AutoConnect = true;
- public byte Version = 1;
- /// <summary>if we don't want to connect in Start(), we have to "remember" if we called ConnectUsingSettings()</summary>
- private bool ConnectInUpdate = true;
- public void Start()
- {
- //Debug.Log("当前是否连接" + PhotonNetwork.connected);
- PhotonNetwork.autoJoinLobby = false; // we join randomly. always. no need to join a lobby to get the list of rooms.
- if (PhotonNetwork.connected)
- {
- PhotonNetwork.JoinRandomRoom();
- }
- }
- public virtual void Update()
- {
- if (ConnectInUpdate && AutoConnect && !PhotonNetwork.connected)
- {
- Debug.Log("Update() was called by Unity. Scene is loaded. Let's connect to the Photon Master Server. Calling: PhotonNetwork.ConnectUsingSettings();");
- ConnectInUpdate = false;
- PhotonNetwork.ConnectUsingSettings(Version + "." + SceneManagerHelper.ActiveSceneBuildIndex);
- }
- }
- // below, we implement some callbacks of PUN
- // you can find PUN's callbacks in the class PunBehaviour or in enum PhotonNetworkingMessage
- public virtual void OnConnectedToMaster()
- {
- Debug.Log("OnConnectedToMaster() was called by PUN. Now this client is connected and could join a room. Calling: PhotonNetwork.JoinRandomRoom();");
- PhotonNetwork.JoinRandomRoom();
- }
- public virtual void OnJoinedLobby()
- {
- 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();");
- PhotonNetwork.JoinRandomRoom();
- }
- public virtual void OnPhotonRandomJoinFailed()
- {
- Debug.Log("OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);");
- PhotonNetwork.CreateRoom(null, new RoomOptions() { MaxPlayers = 4 }, null);
- }
- // the following methods are implemented to give you some context. re-implement them as needed.
- public virtual void OnFailedToConnectToPhoton(DisconnectCause cause)
- {
- Debug.LogError("Cause: " + cause);
- }
- public void OnJoinedRoom()
- {
- 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");
- }
- }
勾选上autoconnect 挂到空物体下,在初始化的时候就可以链接到服务器,当然我还是建议,在没有很熟悉的情况下,在他的demo上面进行添加修改,这样不会出错。
建议使用这个demo。有脚本实时显示着链接状态,非常好用。