Unity-Photon Pun2 personal summary

Configuration before entering the room

1. Use the set Setting

    private void Start()
    {
        PhotonNetwork.ConnectUsingSettings();
    }

2. Change MonoBehaviour to MonoBehaviourPunCallbacks

public class NetworkLauncher : MonoBehaviourPunCallbacks{}

Only in this way can we get some feedback from the Photon server

3. Connect to the Photon server

    public override void OnConnectedToMaster()
    {
        base.OnConnectedToMaster();  //连接Photon服务器
        print("Welcome");
        PhotonNetwork.JoinLobby(default);  //进入游戏大厅
    }

4. Join or create a room

    public void JoinOrCreateButton() 
    {
        RoomOptions options = new RoomOptions
        {
            MaxPlayers = 10  //最大玩家人数
        };
        PhotonNetwork.JoinOrCreateRoom("MyRoom", options, default);
    }

5. Do scene jump after joining or creating a room

    public override void OnJoinedRoom()
    {
        PhotonNetwork.LoadLevel(1);
    }

full code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;

public class NetworkLauncher : MonoBehaviourPunCallbacks
{
    public GameObject loginUI;
    public GameObject nameUI;
    public InputField roomName;
    public InputField playerName;

    public GameObject roomListUI;


    private void Start()
    {
        PhotonNetwork.ConnectUsingSettings();
    }

    public override void OnConnectedToMaster()
    {
        base.OnConnectedToMaster();
        print("Welcome");
        nameUI.SetActive(true);
        PhotonNetwork.JoinLobby(default);
    }

    public void PlayButton() 
    {
        nameUI.SetActive(false);
        //NickName玩家名字
        PhotonNetwork.NickName = playerName.text;
        loginUI.SetActive(true);

        if (PhotonNetwork.InLobby) 
        {
            roomListUI.SetActive(true);
        }
    }

    public void JoinOrCreateButton() 
    {
        if (roomName.text.Length < 2) return;
        loginUI.SetActive(false);

        RoomOptions options = new RoomOptions
        {
            MaxPlayers = 10
        };
        PhotonNetwork.JoinOrCreateRoom(roomName.text, options, default);
    }

    public override void OnJoinedRoom()
    {
        PhotonNetwork.LoadLevel(1);
    }
}

 Enter your name to enter the game lobby

 Enter the lobby, enter the room after setting the room name, you can create a room or join a room

Configuration after entering the room

1. Instantiate and generate player objects

GameObject Player = PhotonNetwork.Instantiate("Player", 
                     Vector.one,Quaternion.identity, 0);

 The prefab needs to be placed in the PhotonUnityNetworking/Resources package

 2. Limit player operations to prevent the current object from operating another player object

    private void Update()
    {
        //如果操作的不是自己的玩家对象,则直接返回true
        if (!photonView.IsMine && PhotonNetwork.IsConnected) return;
    }

3. Photon data synchronization and state synchronization

(1) For objects that need to be synchronized, the PhotonView component must be attached

 (2) Photon provides us with position synchronization and Animator synchronization, and the announcement can be completed directly by hanging it up

 

 (3) Custom data synchronization

i. Need to inherit IpunObservable

ii. Need to implement a custom interface

public class Test : MonoBehaviourPunCallbacks,IPunObservable
{
        public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(IsthereAnyone);
        }
        else 
        {
            IsthereAnyone = (bool)stream.ReceiveNext();
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_46711336/article/details/128249806