Unity开发 Photon Pun 多人游戏组件 | 您所在的位置:网站首页 › syllepsis和pun的区别 › Unity开发 Photon Pun 多人游戏组件 |
大致原理:每个客户端把自己的 相关数据(比如位置信息)传到服务器;再由服务器同步到每一个客户端里; 组件官方网站: https://www.photonengine.com/zh-CN/Photon 大致步骤1、在官网创建账户; 2、在官网 创建应用: Create new application 3、【可选项】申请国内的站点服务 因为默认使用的是 国外的服务器,国内访问可能会慢一点; 所以可以在中国区的官网申请中国光子云; 光子引擎连接: https://vibrantlink.com/ #1 申请光子云 #2 修改配置 这里将放到 4 - #2里面讲解; 因为有光子云 和没有 光子云有些许区别; 4、创建Unity项目,搭建基本的框架: #1 导入 photon pun2- 插件;在 assets store里面搜索下载导入; ps: photon pun2+ 是pro版本,要付费, - 是免费版本; 刚导入可能会有这个窗口,我们跳过,在 #2里进行详细配置; #2 修改配置文件: 导入 photon之后,对配置文件进行 些许配置 1、Photon -》 PhotonUnityNetworking -》 Resources: 找到 PhotonServerSettings; 直接搜这个文件也可以;2、 申请了中国光子云的设置方法: a. 找到脚本 LoadBalancingClient.cs ; 修改 NameServerHost, 由 ns.exitgames.com 改为 ns.photonengine.cn;![]() ![]() #3 写脚本,搭建 框架 小demo: 1、创建 player 的预制体: a. 新建Cube; 添加一个脚本 : PlayerCubeController; 简单移动逻辑 using Photon.Pun; using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed = 2; public float rotateSpeed = 40; private PhotonView pv; // Start is called before the first frame update void Start() { pv = GetComponent(); } // Update is called once per frame void Update() { if(!pv.IsMine || !PhotonNetwork.IsConnected)return; //让 玩家控制 自己的角色 float hValue = Input.GetAxisRaw("Horizontal"); float vValue = Input.GetAxisRaw("Vertical"); Vector3 moveValue = new Vector3(hValue*moveSpeed*Time.deltaTime,vValue*moveSpeed*Time.deltaTime,0); this.transform.Translate(moveValue,Space.World); if(Input.GetKey(KeyCode.R)){ Vector3 rotateValue = new Vector3(0, rotateSpeed*Time.deltaTime, 0); this.transform.Rotate(rotateValue); } } }b.在 cube上 添加一个 photon view组件; 这个组件是 告诉服务器 我要同步那些信息,下面举例同步 位置信息 transform; 两处修改: 1、 Observerable Serach : 改为Manual; 2、 把Transform组件拖到 Observed Components里面; c. 将cube 设为预制体,但是要放到指定文件夹: Photon -> PhotonUnityNetworking -> Resources: 文件夹下面; 2、设置 联网 信息: a. 创建空物体 PhotonManager, 挂在一个新建脚本 PhotonManager: using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun; // 引入命名空间 using Photon.Realtime; public class PhotonManager : MonoBehaviourPunCallbacks // 修改继承对象 { public GameObject playerCube; public Transform posOne; public Transform posTwo; // Start is called before the first frame update void Start() { PhotonNetwork.ConnectUsingSettings(); //通过我们之前配置的文件 连接服务器; Debug.Log("连接上服务器"); } //监听 函数, 连接上服务器 执行 public override void OnConnectedToMaster(){ base.OnConnectedToMaster(); //创建 或者 进入房间 RoomOptions roomOptions = new RoomOptions(); roomOptions.MaxPlayers = 20; PhotonNetwork.JoinOrCreateRoom("Room0820",roomOptions, TypedLobby.Default); } public override void OnJoinedRoom(){ base.OnJoinedRoom(); Debug.Log("连接上房间"); //创建 玩家: if (playerCube == null || posOne == null || posTwo == null)return; if (PhotonNetwork.LocalPlayer.IsMasterClient) { //本身的玩家 是 主机 PhotonNetwork.Instantiate(playerCube.name, posOne.position, playerCube.transform.rotation);// 服务器 帮我们创建玩家; name对应于resources 文件夹下的prefab } else { PhotonNetwork.Instantiate(playerCube.name, posTwo.position, playerCube.transform.rotation); } } } |
CopyRight 2018-2019 实验室设备网 版权所有 |