Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 使用PUN同步运行时导入的对象_Unity3d_Multiplayer_Photon - Fatal编程技术网

Unity3d 使用PUN同步运行时导入的对象

Unity3d 使用PUN同步运行时导入的对象,unity3d,multiplayer,photon,Unity3d,Multiplayer,Photon,在我们关于Unity的多人项目中,每个用户在运行时导入模型,然后加入room,然后将运行时导入的对象配置为网络对象。问题是,当主机离开时,导入的模型会被破坏。以下是我的代码片段: private void Start() { pView = GetComponent<PhotonView>(); pView.ownershipTransfer = OwnershipOption.Takeover; pView.Observed

在我们关于Unity的多人项目中,每个用户在运行时导入模型,然后加入room,然后将运行时导入的对象配置为网络对象。问题是,当主机离开时,导入的模型会被破坏。以下是我的代码片段:

 private void Start()
    {
        pView = GetComponent<PhotonView>();
        pView.ownershipTransfer = OwnershipOption.Takeover;
        pView.ObservedComponents = new List<Component>();

        photonTransformView = transform.GetComponent<PhotonTransformView>();
        pView.ObservedComponents.Add(photonTransformView);
        pView.synchronization = ViewSynchronization.UnreliableOnChange;

        photonTransformView.m_PositionModel.SynchronizeEnabled = true;
        photonTransformView.m_RotationModel.SynchronizeEnabled = true;

        if (GetComponent<OVRGrabbable>() != null)
            transform.GetComponent<OVRGrabbable>().runtimeObj = this;
        partInfo = transform.GetComponent<PartInfo>();
        if (partInfo)
            partInfo.dynamicDync = this;

        if (PhotonNetwork.isMasterClient)
        {
            int objId = PhotonNetwork.AllocateViewID();
            // pView.viewID = objId;
            //TakeOwnership(PhotonNetwork.player);

            MSetPhotonViewId(PhotonNetwork.player.ID, gameObject.name, objId);
        }
    }

    public void MSetPhotonViewId(int id, string objName, int objId)
    {
        //pView.RPC("SetOwnerForObject", PhotonTargets.Others, id);
        object[] content = new object[3];
        content[0] = id;
        content[1] = objName;
        content[2] = objId;
        PhotonNetwork.RaiseEvent(ownerId, content, true, new RaiseEventOptions() { Receivers = ReceiverGroup.All, CachingOption = EventCaching.AddToRoomCache });
    }

    private void OnEvent(byte eventcode, object content, int senderid)
    {
        if (eventcode == ownerId)
        {
            object[] data = (object[])content;
            if (gameObject.name == (string)data[1])
            {
                if (!pView)
                    StartCoroutine(MGetPhotonViewId(content, senderid));
                else
                {
                    pView.viewID = (int)data[2];
                    //Debug.Log("transfering ownership of: " + gameObject.name + " to: " + ((int)data[0]).ToString());
                    pView.TransferOwnership((int)data[0]);
                }
            }
        }
    }

    IEnumerator MGetPhotonViewId(object content, int senderid)
    {
        while (!pView)
        {
            yield return null;
        }

        object[] data = (object[])content;
        if (gameObject.name == (string)data[1])
        {
            pView.viewID = (int)data[2];
            //Debug.Log("transfering ownership of: " + gameObject.name + " to: " + ((int)data[0]).ToString());
            pView.TransferOwnership((int)data[0]);
        }
    }
private void Start()
{
pView=GetComponent();
pView.ownershipTransfer=所有权。接管;
pView.ObservedComponents=新列表();
photonTransformView=transform.GetComponent();
pView.ObservedComponents.Add(photonTransformView);
pView.synchronization=ViewSynchronization.change;
photonTransformView.m_PositionModel.SynchronizeEnabled=真;
photonTransformView.m_RotationModel.SynchronizeEnabled=真;
如果(GetComponent()!=null)
transform.GetComponent().runtimeObj=this;
partInfo=transform.GetComponent();
如果(partInfo)
partInfo.dynamicDync=this;
if(PhotonNetwork.isMasterClient)
{
int objId=PhotonNetwork.AllocateViewID();
//pView.viewID=objId;
//收购所有权(PhotonNetwork.player);
MSetPhotonViewId(PhotonNetwork.player.ID、gameObject.name、objId);
}
}
public void MSetPhotonViewId(int-id、字符串objName、int-objId)
{
//RPC(“SetOwnerForObject”,PhotonTargets.Others,id);
对象[]内容=新对象[3];
内容[0]=id;
内容[1]=对象名;
内容[2]=对象;
PhotonNetwork.RaiseEvent(ownerId,content,true,new RaiseEventOptions(){Receivers=ReceiverGroup.All,CachingOption=EventCaching.AddToRoomCache});
}
私有void OnEvent(字节事件代码、对象内容、int senderid)
{
if(eventcode==ownerId)
{
对象[]数据=(对象[])内容;
如果(gameObject.name==(字符串)数据[1])
{
如果(!pView)
start例程(MGetPhotonViewId(content,senderid));
其他的
{
pView.viewID=(int)数据[2];
//Log(“将:“+gameObject.name+”的所有权转移到:”+((int)数据[0]).ToString());
pView.TransferOwnership((int)数据[0]);
}
}
}
}
IEnumerator MGetPhotonViewId(对象内容,int senderid)
{
而(!pView)
{
收益返回空;
}
对象[]数据=(对象[])内容;
如果(gameObject.name==(字符串)数据[1])
{
pView.viewID=(int)数据[2];
//Log(“将:“+gameObject.name+”的所有权转移到:”+((int)数据[0]).ToString());
pView.TransferOwnership((int)数据[0]);
}
}

当主机离开时,如何避免对象在其他系统上被破坏?

当玩家退出游戏时,通常他/她的行为与新玩家无关。为了避免加入时的拥塞,默认情况下,光子服务器会自动清理玩家缓存的事件,这将永远为玩家留下空间

如果要手动清理文件室的事件缓存,可以使用RoomOptions.CleanupCacheOnLeave设置为false创建文件室


.

你好,Tobias,感谢您分享解决方案。我已通过禁用“自动清除UPPLAYEROBJECTS”解决了这个问题。但因此,我面临着这个问题: