使用 PUN 同步运行时导入的对象
Posted
技术标签:
【中文标题】使用 PUN 同步运行时导入的对象【英文标题】:Syncing objects imported at runtime using PUN 【发布时间】:2019-03-16 14:28:30 【问题描述】:在我们在 Unity 上的多人游戏项目中,每个用户在运行时导入模型,然后加入房间,然后将运行时导入的对象配置为联网对象。问题是,当主机离开时,导入的模型会被破坏。这是我的代码 sn-p:
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]);
当主机离开时,如何避免对象在其他系统上被破坏?
【问题讨论】:
【参考方案1】:当玩家退出游戏时,通常他/她的行为与新玩家不再相关。为避免加入时出现拥塞,Photon 服务器默认会自动清理已被玩家缓存的事件,这些事件已经永远离开了空间。
如果您想手动清理房间的事件缓存,您可以创建房间并将 RoomOptions.CleanupCacheOnLeave 设置为 false。
See the docs.
【讨论】:
嗨 Tobias,感谢分享解决方案。我通过禁用“autoCleanUpPlayerObjects”解决了问题。但结果我面临这个问题:***.com/questions/55958661/…以上是关于使用 PUN 同步运行时导入的对象的主要内容,如果未能解决你的问题,请参考以下文章