客戶端傳遞數據之服務器,服務器完成邏輯判斷,並將結果返回給客戶端
html
遊戲邏輯在客戶端處理,photon服務器只負責客戶端之間數據驗證和傳遞
web
在PUN的客戶端Client中,包含一個MasterClient,咱們使用MasterClient來處理網絡遊戲邏輯算法
Photon中生成玩家對象須要調用下面方法實例化。服務器
//生成玩家對象
void InstancePlayer() {
playerCustomProperties = PhotonNetwork.player.CustomProperties;
if (playerCustomProperties["Team"].ToString().Equals("Team1"))
{
localPlayer = PhotonNetwork.Instantiate("MyPlayer",
TeamOneSpTrans[(int)playerCustomProperties["TeamNum"]].position, Quaternion.identity, 0);
}
//若是玩家眷於隊伍2,生成RobotPlayer對象
else if (PhotonNetwork.player.CustomProperties["Team"].ToString().Equals("Team2")) {
localPlayer = PhotonNetwork.Instantiate("MyPlayer",
TeamTwoSpTrans[(int)playerCustomProperties["TeamNum"]].position, Quaternion.identity, 0);
}
localPlayer.name = PhotonNetwork.player.NickName;
}
PhotonTransformView組件內置幾種插值算法,能夠使用內置插值方法實現玩家的平滑「移動。網絡
使用該方法時需自行處理位置和旋轉的插值。ide
private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
if (stream.isWriting)
{
//We own this player: send the others our data
...
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
...
PlayerPosition = (Vector3)stream.ReceiveNext();
PlayerRotation = (Quaternion)stream.ReceiveNext();
}
}
Animation動畫:
如player有針對於其動畫的控制腳本,經過Update函數中的playerState來判斷動畫表現,這裏咱們傳遞playerState來實現動畫同步函數
private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
if (stream.isWriting)
{
//We own this player: send the others our data
...
stream.SendNext(player.playerState);
}
else
{
...
player.playerState = (PlayAnimation)stream.ReceiveNext(); ;
}
}
Animator動畫:
animator組件中經過各個參數來控制動畫的分支走向,這裏咱們同步其參數來實現動畫同步動畫
private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
if (stream.isWriting)
{
//We own this player: send the others our data
...
stream.SendNext(ani.GetFloat("RunSpeed"));
stream.SendNext(ani.GetBool("isJump"));
}
else
{
...
ani.SetFloat("RunSpeed",(string)stream.ReceiveNext());
ani.SetBool("isJump",(bool)stream.ReceiveNext());
}
}