PUN Unity - 手动实例化时在视图中缺少有效的 InstantiationId

PUN Unity - Missing a valid InstantiationId on view when manually instantiated

我正在 Unity 中制作一个程序地牢,我正在使用 PUN。我正在尝试生成在场景中具有光子视图的项目。墙壁和一切都是由种子完成的,它只是可交互的项目。您可以想象我不想为每个项目都链接到我的资源文件夹,因为我有很多项目。所以我正在尝试本页底部所述的手动实例化 https://doc.photonengine.com/zh-tw/pun/current/manuals-and-demos/instantiation 。问题是当我试图销毁物品时出现此错误:

Failed to 'network-remove' GameObject because it is missing a valid InstantiationId on view: View (0)106 on MoneyPileBig(Clone) (scene). Not Destroying GameObject or PhotonViews!
UnityEngine.Debug:LogError(Object)
NetworkingPeer:RemoveInstantiatedGO(GameObject, Boolean) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:3336)
PhotonNetwork:Destroy(GameObject) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2744)
NetworkDestroyer:RPCNetworkDestroy() (at Assets/Scripts/Photon/NetworkDestroyer.cs:17)
System.Reflection.MethodBase:Invoke(Object, Object[])
NetworkingPeer:ExecuteRpc(Hashtable, PhotonPlayer) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2891)
NetworkingPeer:OnEvent(EventData) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2484)
ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:157)

这是我的代码,减去了列表中的内容,因为那是相当多的代码,而且不相关。

private List<PhotonInstantiatingInfo> toPhotonInstantiate = new List<PhotonInstantiatingInfo>();

private void CustomInstantiate(GameObject _prefab, Vector2 _position, Quaternion _rotation, Transform _parent)
{
    if (_prefab.GetComponent<PhotonView>() != null)
    {
        toPhotonInstantiate.Add(new PhotonInstantiatingInfo(_prefab, _position, _rotation, _parent));
    }
    else
    {
        Instantiate(_prefab, _position, _rotation, _parent);
    }
}

private class PhotonInstantiatingInfo
{
    public GameObject Prefab { get; private set; }
    public Transform Parent { get; private set; }
    public Vector2 Position { get; private set; }
    public Quaternion Rotation { get; private set; }

    public PhotonInstantiatingInfo(GameObject _prefab, Vector2 _position, Quaternion _rotation, Transform _parent)
    {
        Prefab = _prefab;
        Parent = _parent;
        Position = _position;
        Rotation = _rotation;
    }
}

private void InstantiatePhotonObjects()
{
    if (!PhotonNetwork.isMasterClient) { return; }
    for (int i = 0; i < toPhotonInstantiate.Count; i++)
    {
        int newViewId = PhotonNetwork.AllocateSceneViewID();
        myPhotonView.RPC("RPCPhotonInstantiate", PhotonTargets.AllBufferedViaServer, i, newViewId);
    }
}

[PunRPC]
private void RPCPhotonInstantiate(int _index, int _viewId)
{
    PhotonInstantiatingInfo instantiationInfo = toPhotonInstantiate[_index];
    GameObject newPlayer = Instantiate(instantiationInfo.Prefab, instantiationInfo.Position, instantiationInfo.Rotation) as GameObject;
    PhotonView[] nViews = newPlayer.GetComponentsInChildren<PhotonView>();
    nViews[0].viewID = _viewId;
}

我该如何解决这个问题?

我通过使用 RPC 手动销毁对象来修复它。因此,首先我会让 RPC 执行 PhotonNetworkDestroy(如果它是主服务器),但现在我不执行 PhototnNetworkDestroy,但我执行正常的销毁并且我不检查它是否是主服务器。