Photon Unity InvalidCastException问题求助:指定的转换无效
解决Photon同步中的InvalidCastException问题
这个错误一直卡着我,没法推进更新。它只触发一次,之后所有功能又恢复正常。一开始以为是别的问题,但禁用SlimeSync脚本后,虽然运行正常但数据完全不同步了。我刚接触多人游戏开发,实在搞不懂,求帮忙解决。
同步脚本代码
using System; using System.Collections; using System.Collections.Generic; using Photon.Pun; using UnityEngine; public class SlimeSync : MonoBehaviourPunCallbacks, IPunObservable { #region IPunObservable implementation private SpriteRenderer spriteRenderer; private PlayerControlledSlime slime; private SlimeCustomization slimeCustomization; private void Start(){ spriteRenderer = GetComponent<SpriteRenderer>(); slime = GetComponent<PlayerControlledSlime>(); slimeCustomization = Camera.main.GetComponentInChildren<SlimeCustomization>(); Camera.main.gameObject.GetComponentInChildren<Canvas>().GetComponent<PlayerInfo>().AssignPlayer(gameObject); } public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if (stream == null || spriteRenderer == null || slime == null || slimeCustomization == null) return; if (stream.IsWriting) { // We own this player: send the others our data stream.SendNext(spriteRenderer.flipX); stream.SendNext(slime.Slime); stream.SendNext(slime.Hat); stream.SendNext(slime.Animation); } else { // Network player, receive data spriteRenderer.flipX = Convert.ToBoolean(stream.ReceiveNext()); int Slime = Convert.ToInt32(stream.ReceiveNext()); slime.AssignSlime(slimeCustomization.slimeObjects[Slime], Slime); int Hat = Convert.ToInt32(stream.ReceiveNext()); slime.AssignHat(slimeCustomization.hatObjects[Hat], Hat); int Animation = Convert.ToInt32(stream.ReceiveNext()); switch (Animation){ case 0: spriteRenderer.sprite = slime.Normal; break; case 1: spriteRenderer.sprite = slime.Squash; break; } } } #endregion }
完整错误信息
InvalidCastException: 指定的转换无效。 Photon.Pun.PhotonTransformView.OnPhotonSerializeView (Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/Views/PhotonTransformView.cs:154) Photon.Pun.PhotonView.DeserializeComponent (UnityEngine.Component component, Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:544) Photon.Pun.PhotonView.DeserializeView (Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:534) Photon.Pun.PhotonNetwork.OnSerializeRead (System.Object[] data, Photon.Realtime.Player sender, System.Int32 networkTime, System.Int16 correctPrefix) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1866) Photon.Pun.PhotonNetwork.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:2237) Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3568) ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PeerBase.cs:904) ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/EnetPeer.cs:595) ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PhotonPeer.cs:1779) Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:236) Rethrow as AggregateException: Caught 1 exception(s) in methods called by DispatchIncomingCommands(). Rethrowing first only (see above). Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:254) Photon.Pun.PhotonHandler.FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:156)
问题定位与修复方案
核心原因
错误栈显示异常来自PhotonTransformView的序列化方法,不是你的SlimeSync直接抛出,但根源是PhotonView的观测组件顺序错误:Photon会按组件列表顺序调用OnPhotonSerializeView,如果自定义的SlimeSync先写入数据,会打乱PhotonTransformView预期的数据流顺序,导致它读取到错误类型的数据,触发转换异常。
具体修复步骤
调整PhotonView的观测组件顺序
- 选中挂载PhotonView的Slime对象,打开Inspector面板的PhotonView组件
- 在「Observed Components」列表中,把
PhotonTransformView移到SlimeSync前面,确保官方组件先处理同步数据
优化组件初始化逻辑
把Start中的相机相关组件初始化延迟一帧,避免Camera.main未加载完成导致的空值问题:private void Awake(){ spriteRenderer = GetComponent<SpriteRenderer>(); slime = GetComponent<PlayerControlledSlime>(); StartCoroutine(InitCameraComponents()); } private IEnumerator InitCameraComponents() { yield return null; slimeCustomization = Camera.main.GetComponentInChildren<SlimeCustomization>(); Camera.main.GetComponentInChildren<Canvas>().GetComponent<PlayerInfo>().AssignPlayer(gameObject); }替换Convert为强类型转换
反序列化时直接用强转代替Convert,减少不必要的类型转换风险:else { spriteRenderer.flipX = (bool)stream.ReceiveNext(); int Slime = (int)stream.ReceiveNext(); slime.AssignSlime(slimeCustomization.slimeObjects[Slime], Slime); int Hat = (int)stream.ReceiveNext(); slime.AssignHat(slimeCustomization.hatObjects[Hat], Hat); int Animation = (int)stream.ReceiveNext(); switch (Animation){ case 0: spriteRenderer.sprite = slime.Normal; break; case 1: spriteRenderer.sprite = slime.Squash; break; } }验证PhotonTransformView配置
- 检查PhotonTransformView的同步选项,只开启需要同步的属性(如位置、旋转),减少冗余数据
- 确认插值模式设置符合游戏需求,避免和自定义同步逻辑冲突
为什么只触发一次?
第一次同步时,组件初始化顺序或数据流顺序错误导致转换失败;后续同步时,组件已完成初始化,Photon内部也自动恢复了数据同步顺序,所以不再报错,但潜在问题仍可能导致偶发同步异常,必须修复。
内容的提问来源于stack exchange,提问作者Turnrp
相关产品推荐
相关产品推荐

