Unity问题与解决方案:Photon同步数据的四种方式

2023-10-25 17:48

本文主要是介绍Unity问题与解决方案:Photon同步数据的四种方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

从我目前get到的点钟,大概是有三个方法,第一个方法是使用PhotonNetwork.RPC方法,该方法接收方法名,发送对象(PhotonPlayer或PhotonTarget)和方法参数,并调用声明[PUNRPC]的相关方法,但根据源码:

//<summary>
/// Internal to send an RPC on given PhotonView. Do not call this directly but use: PhotonView.RPC!
/// </summary>
internal static void RPC(PhotonView view, string methodName, PhotonTargets target, bool encrypt, params object[] parameters)

很明显该方法并不被推荐适用(Do not call this directly but use: PhotonView.RPC!

第二个方法则是按照PhotonView同步的方式,模仿PhotonTransformView等写一个自己的脚本,如果要仿照的话,PhotonRigidbodyView的实现较为简单,可以参考,来看一下原码:

using System;
using UnityEngine;
using Object = System.Object;/// <summary>
/// This class helps you to synchronize the velocities of a physics RigidBody.
/// Note that only the velocities are synchronized and because Unitys physics
/// engine is not deterministic (ie. the results aren't always the same on all
/// computers) - the actual positions of the objects may go out of sync. If you
/// want to have the position of this object the same on all clients, you should
/// also add a PhotonTransformView to synchronize the position.
/// Simply add the component to your GameObject and make sure that
/// the PhotonRigidbodyView is added to the list of observed components
/// </summary>
[RequireComponent(typeof(PhotonView))]
[RequireComponent(typeof(Rigidbody))]
[AddComponentMenu("Photon Networking/Photon Rigidbody View")]
public class PhotonRigidbodyView : MonoBehaviour, IPunObservable
{[SerializeField]bool m_SynchronizeVelocity = true;[SerializeField]bool m_SynchronizeAngularVelocity = true;Rigidbody m_Body;void Awake(){this.m_Body = GetComponent<Rigidbody>();}public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){if (stream.isWriting == true){if (this.m_SynchronizeVelocity == true){stream.SendNext(this.m_Body.velocity);}if (this.m_SynchronizeAngularVelocity == true){stream.SendNext(this.m_Body.angularVelocity);}}else{if (this.m_SynchronizeVelocity == true){this.m_Body.velocity = (Vector3)stream.ReceiveNext();}if (this.m_SynchronizeAngularVelocity == true){this.m_Body.angularVelocity = (Vector3)stream.ReceiveNext();}}}
}

从中不难看到,实现的关键是实现IPunObservable接口和接口对应的方法,该实现通过判断stream的读写状态来执行SendNextReceiveNext,要注意发送和接收的数据流和接收逻辑必须是一一对应的,否则会出现值无法对应的情况。

另外一个特别需要注意的问题!如果是基础数据类型,是可以直接SendNext和ReceiveNext的,但是如果是自定义类型,需要在Photon中进行注册,相关文档在PhotonUnity的Document中有,可以去看一下链接,这里列出序列化的代码实例:

如对于我自定义的一个增益属性类Plugin

public class Camp
{public int camp;
}

有两种注册方式(其实差别不大,只是其中一个使用了Photon提供给我们的流)来进行注册,这里只展示第一种

即扩展该类,添加序列化和反序列化的静态方法:

public class Camp
{public int camp;public static byte[] SerializableClass(object plugin)//参数必须是object{Campp = (Camp) plugin;int intlen = Marshal.SizeOf(typeof(int));/** 创建字节序列*/byte[] bytes = new byte[intlen];/** 序列化*/int index = 0;Protocol.Serialize(p.camp, bytes, ref index);return bytes;}public static object DeserializeClass(byte[] bytes){Plugin plugin = new Plugin();int index = 0;Protocol.Deserialize(out plugin.camp, bytes, ref index);return plugin;}
}

随后在任意位置注册一次即可:

PhotonPeer.RegisterType(typeof(Plugin), (byte) 'a',SerializableClass,DeserializeClass);

注意关于第二个是类似占位符的形式,已经注册过的byte不能再次注册,以及被官方注册过的字节有:W/V/Q/P这四种,分别代表Vector3,Vector2,QuaternionPhotonPlayer

然后关于一些其他的字节之间相互转化的方法可以参考一下极客学院的关于Photon的一个公开课,链接,这里就不详细展开了


然后我基于build模式和最近rx系列的思路写了一个传输类,能简化一下stream传输的逻辑,也附在这吧


public class PhotonTranser
{private PhotonStream stream;public static PhotonTranser create(PhotonStream stream){return new PhotonTranser(stream);}private PhotonTranser(PhotonStream stream){this.stream = stream;}public PhotonTranser sendnext(object value,bool realSend){if (realSend &&stream.isWriting) {stream.SendNext(value);}return this;}public PhotonTranser sendnext(object value){if (stream.isWriting){stream.SendNext(value); }return this;}public PhotonTranser receive(Func<object, object> value,bool realRecv){if (realRecv &&!stream.isWriting){value(stream.ReceiveNext());}return this;}public PhotonTranser receive(Func<object, object> value){if (!stream.isWriting){value(stream.ReceiveNext());    }return this;}public void finish(){}}

按照该类的实现,PhotonRigidbodyView中的OnPhotonSerializeView实现就可以改为:

PhotonTranser.create(stream).sendnext(this.m_Body.velocity, this.m_SynchronizeVelocity).sendnext(this.m_Body.angularVelocity,this.m_SynchronizeAngularVelocity).receive(new Func<object, object>(delegate(object o){this.m_Body.velocity = (Vector3) o;return null;}), this.m_SynchronizeVelocity).receive(new Func<object, object>(delegate(object o){this.m_Body.angularVelocity = (Vector3) o;return null;}),this.m_SynchronizeAngularVelocity  ).finish();

感觉这样下来会就比较直观,不过由于C#半路出家..一些语法不是很了解,不清楚有没有更简单的实现方式,比如lambda等(猜的),有人有思路可以提供一下

第三种方法有条件,就是只能在用PhotonNetwork.Instantiate实例化对象的时候传递,具体的方式可以参考一下我的这一篇博客:

Photon实例化(PhotonNetwork.Instantiate参数详解)

这篇关于Unity问题与解决方案:Photon同步数据的四种方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/284190

相关文章

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi