Unity-Photon Pun2个人总结

进入房间前的配置

1、使用设定好的Setting

Unity-Photon Pun2个人总结_第1张图片

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

2、MonoBehaviour改为MonoBehaviourPunCallbacks

public class NetworkLauncher : MonoBehaviourPunCallbacks{}

这样我们才可以获得Photon服务端的一些反馈资料

3、连接到Photon服务器

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

4、加入或创建房间

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

5、加入或者创建房间后做场景跳转

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

完整代码

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);
    }
}

 输入名字,进入游戏大厅

Unity-Photon Pun2个人总结_第2张图片

 进入大厅,设置房间名后进入房间,可创建房间或者加入房间

Unity-Photon Pun2个人总结_第3张图片

进入房间后的配置

1、实例化生成玩家对象

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

 预制体需要放在PhotonUnityNetworking/Resources包中

Unity-Photon Pun2个人总结_第4张图片

 2、限定玩家操作,防止当前对象操作另一玩家对象

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

3、Photon数据同步和状态同步

(1)需要同步的物体,要挂上PhotonView组件

Unity-Photon Pun2个人总结_第5张图片

 (2)Photon为我们提供了位置同步Animator的同步,直接挂上去即可完成公布

 Unity-Photon Pun2个人总结_第6张图片

 (3)自定义数据同步

i.需要继承IpunObservable

ii.需要实现自定义接口

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

你可能感兴趣的:(unity,游戏引擎)