private IEnumerator DelayDestroyObject(string id) { if (destroyedObjects.Contains(id)) { yield break; } yield return(new WaitForSeconds(0.01f)); if (Objects.ContainsKey(id)) { NetworkMissionObject obj = Objects[id]; Objects.Remove(id); Destroy(obj.gameObject); destroyedObjects.Add(id); } }
private void HandleSyncMission(byte[] data) { NetDataEvent ndata = Utils.FromBytesJSON <NetDataEvent>(data); BaseMission mission = (BaseMission)ndata.Values["mission"].ObjectValue; if (mission.Objects == null) { mission.Objects = new Dictionary <string, BaseMissionObject>(); } if (mission.DynamicObjects == null) { mission.DynamicObjects = new Dictionary <string, BaseMissionObject>(); } Dictionary <string, BaseMissionObject> objects = mission.Objects; MergeDictionary(objects, mission.DynamicObjects); //instantiate objects on client created on server foreach (var o in objects.ToList()) { if (o.Value.Destroyed) { continue; } bool isOwn = o.Value.OwnerID.Equals(NetManager.I.ID); if (!NetworkObjectDispenser.I.Objects.ContainsKey(o.Value.ID)) { GameObject go = SpawnObject(o.Value.ID, o.Value.NameID, o.Value.Position.FromServerVector3(), o.Value.Center.FromServerVector3(), o.Value.Size.FromServerVector3(), o.Value.EulerAngles.FromServerVector3(), isOwn); if (o.Value is PlayerObject) { players.Add(go.GetComponent <Player>()); if (isOwn) { CameraController.I.Target = go; } } } } //sync exist objects foreach (var o in objects.ToList()) { if (NetworkObjectDispenser.I.Objects.ContainsKey(o.Value.ID)) { NetworkMissionObject obj = NetworkObjectDispenser.I.Objects[o.Value.ID]; obj.SyncTransform(o.Value.Position.FromServerVector3(), o.Value.EulerAngles.FromServerVector3()); if (o.Value is IHuman) { (obj as IHumanObject).SyncHealth((o.Value as IHuman).Health, (o.Value as IHuman).MaxHealth); if (o.Value is PlayerObject && o.Value.OwnerID.Equals(NetManager.I.ID)) { GameGUIManager.I.UpdateHealthBar((o.Value as IHuman).Health, (o.Value as IHuman).MaxHealth); } } if (o.Value.Destroyed) { NetworkObjectDispenser.I.DestroyObject(o.Value.ID); } } } }