[英]Unity Photon PUN - unable to get IPunObservable call back to operate
我正在尝试了解 Photon Pun 架构。 我无法调用“ IPunObservable
”中的回调OnPhotonSerializeView()
。
我在我的场景中拥有的是
位于层次结构顶部的名为DataHandler
的GameObject
DataHandler
附加了一个PhotonView
和PhotonTransformView
DataHandler
还附加了一个名为DataHandler
的脚本
DataHandler
脚本组件已被拖到 PhotonView 组件中的第一个 observable(因此它显示“ DataHandler (DataHandler)
”作为被观察组件)。
DataHandler 脚本实现IPunObservable
,其中一种方法是
public class DataHandler: MonoBehaviourPunCallbacks, IPunObservable {.... public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { Debug.Log("OnPhotonSerializeView(): " + (stream.IsWriting? "writing": "reading")); }.... }
运行时日志中不显示任何内容。
但是,当作为 Windows EXE 构建并运行时,与一个房间相连,它会出现 NullReferenceExceptions。 日志显示:
NullReferenceException: Object reference not set to an instance of an object
at Photon.Pun.PhotonView.SerializeComponent (UnityEngine.Component component, Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) [0x0001e] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonView.cs:368
at Photon.Pun.PhotonView.SerializeView (Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) [0x00024] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonView.cs:330
at Photon.Pun.PhotonNetwork.OnSerializeWrite (Photon.Pun.PhotonView view) [0x00089] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonNetworkPart.cs:1593
at Photon.Pun.PhotonNetwork.RunViewUpdate () [0x000b3] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonNetworkPart.cs:1522
at Photon.Pun.PhotonHandler.LateUpdate () [0x00042] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonHandler.cs:155
(Filename: C:/Users/nick/Documents/Unity3D/PhotonPunTest/Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs Line: 368)
PhotonView.cs
中第 368 行的代码是
protected internal void SerializeComponent(Component component, PhotonStream stream, PhotonMessageInfo info)
{
IPunObservable observable = component as IPunObservable;
if (observable != null)
{
observable.OnPhotonSerializeView(stream, info);
}
else
{
Debug.LogError("Observed scripts have to implement IPunObservable. "+ component + " does not. It is Type: " + component.GetType(), component.gameObject);
}
}
( Debug.LogError
行是 368)。
看不出有什么问题,尽管我认为component
可能是null
。 该脚本正在实现IPunObservable
。 任何人都可以帮忙吗?
这里的问题是关于本地播放器和主客户端的光子逻辑。 关于谁可以发送的规则在此绑定。
最后,我发现 go 在更高级别的“帮助”组件(PhotonView 等)下方更容易,并使用较低级别的 RaiseEvent() / OnEvent() 机制发送所有内容。
请参阅上一个问题的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.