Introduction to PUN syntax
review
This time, we mainly focus on implementing multiplayer games with specific codes.
introduce
Synchronize
As for how to achieve synchronization, this involves the synchronization of pun, which mainly requires 3 steps.
First you need to add the props to the component. Let's take roles as an example.
First of all, we need to add Photon components to the characters. The first is the PhotonView component, and the second is the transform component. Use local must be unchecked.
Then, if the character has animation, you need to add the following component. At the same time, it should be noted that the condition control for the trigger needs to be placed in the last place. At the same time, all conditions need to be modified to the above attributes.
Then put the character in the resource directory of photon, and pun will automatically read it, which is convenient for generation and destruction.
Other packages can be set up under this resource path for easy management.
basic use
The following two methods are mainly used to directly connect to pun to start the game, without lobby matching
public override void OnConnectedToMaster()
{
base.OnConnectedToMaster();
Debug.Log("Lianjie");
//创建房间-----------------后期需要改
PhotonNetwork.JoinOrCreateRoom("Room", new Photon.Realtime.RoomOptions()
{
MaxPlayers = 4
}, default);
}
public override void OnJoinedRoom()
{
base.OnJoinedRoom();
OnCountdownTimerIsExpired();
}
some other methods
//加入名为"Room"的房间
PhotonNetwork.JoinRoom("Room");
//如果没有开放的游戏就会失败。错误回调: OnPhotonJoinRoomFailed,可以进行逻辑的编写。
//尝试加入任何随机游戏:
PhotonNetwork.JoinRandomRoom();
//如果没有开放的游戏就会失败。错误回调: OnPhotonRandomJoinFailed
//创建名为"room1"的房间。
PhotonNetwork.CreateRoom("room1");
//如果名为"room1"的房间已存在就会失败并调用:OnPhotonCreateRoomFailed,可以在这个回调进行重写
hall match
There are many callback functions in pun, through which some special cases can be matched.
The following is mainly for the game description.
public override void OnDisable()
{
base.OnDisable();
CountdownTimer.OnCountdownTimerHasExpired -= OnCountdownTimerIsExpired;
}
public override void OnLeftRoom()
{
PhotonNetwork.Disconnect();
}
public override void OnMasterClientSwitched(Player newMasterClient)//*****************************************
{
if (PhotonNetwork.LocalPlayer.ActorNumber == newMasterClient.ActorNumber)
{
//StartCoroutine(SpawnPlatforms());
}
}
public override void OnPlayerLeftRoom(Player otherPlayer)
{
CheckEndOfGame();
}
Implement the callback judgment when some players leave the room.
Judging that players have loaded the scene
private bool CheckAllPlayerLoadedLevel()//检查是否都加载了场景
{
foreach (Photon.Realtime.Player p in PhotonNetwork.PlayerList)
{
object playerLoadedLevel;
if (p.CustomProperties.TryGetValue(Const.PLAYER_LOADED_LEVEL, out playerLoadedLevel))
{
if ((bool)playerLoadedLevel)
{
continue;
}
}
return false;
}
return true;
}
Judgment loading game
public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)//判断游戏结束(通过生命数)||全员加载进来(然后开始倒计时)
{
if (changedProps.ContainsKey(Const.PLAYER_LIVES))
{
CheckEndOfGame();
return;
}
if (!PhotonNetwork.IsMasterClient) //不是主机不用管
{
return;
}
if (!isGameStart && changedProps.ContainsKey(Const.PLAYER_LOADED_LEVEL))//有人加载进来了
{
if (CheckAllPlayerLoadedLevel())//判断是不是都加载进来了
{
CountdownTimer.SetStartTime();
}
else
{
InfoText.text = "等待其他玩家...";
}
}
}
Load the next game and perform initialization
Hashtable props = new Hashtable//下一关生命,设置下一关的属性
{
{
Const.PLAYER_LIVES, 1},
{
Const.WIN_COUNT, 0},
{
Const.PLAYER_LOADED_LEVEL, false}
};
PhotonNetwork.LocalPlayer.SetCustomProperties(props);
if (PhotonNetwork.LocalPlayer.GetPlayerNumber() == winner.GetPlayerNumber())
{
Hashtable hashtable = new Hashtable
{
{
Const.WIN_COUNT, 1}
};
PhotonNetwork.LocalPlayer.SetCustomProperties(hashtable);
}
Summarize
Through this study, it mainly introduces some callback functions of pun and some initialization operations of the game.