public void Detach() { NetAssert.NotNull(UnityObject); NetAssert.True(IsAttached); NetAssert.True(NetworkId.Packed != 0UL); if (AutoRemoveChildEntities) { foreach (AscensionEntity child in UnityObject.GetComponentsInChildren(typeof(AscensionEntity), true)) { if (child.IsAttached && (ReferenceEquals(child.entity, this) == false)) { child.transform.parent = null; } } } if (Controller) { RevokeControl(null); } // destroy on all connections var it = Core.connections.GetIterator(); while (it.Next()) { it.val.entityChannel.DestroyOnRemote(this); } // call out to behaviours foreach (IEntityBehaviour eb in Behaviours) { try { if (eb.Invoke && ReferenceEquals(eb.entity, this.UnityObject)) { eb.Detached(); eb.entity = null; } } catch (Exception exn) { NetLog.Error("User code threw exception inside Detach callback"); NetLog.Exception(exn); } } // call out to user try { GlobalEventListenerBase.EntityDetachedInvoke(this.UnityObject); } catch (Exception exn) { NetLog.Error("User code threw exception inside Detach callback"); NetLog.Exception(exn); } // clear out attached flag Flags &= ~EntityFlags.ATTACHED; // remove from entities list if (Core.entitiesFrozen.Contains(this)) { Core.entitiesFrozen.Remove(this); } if (Core.entitiesThawed.Contains(this)) { Core.entitiesThawed.Remove(this); } // clear from unity object UnityObject.entity = null; // log NetLog.Debug("Detached {0}", this); }
public static Entity CreateFor(GameObject instance, PrefabId prefabId, TypeId serializerId, EntityFlags flags) { Entity eo; eo = new Entity(); eo.UnityObject = instance.GetComponent <AscensionEntity>(); eo.UpdateRate = eo.UnityObject.updateRate; eo.AutoFreezeProxyFrames = eo.UnityObject.autoFreezeProxyFrames; eo.AllowFirstReplicationWhenFrozen = eo.UnityObject.allowFirstReplicationWhenFrozen; eo.AutoRemoveChildEntities = eo.UnityObject.autoRemoveChildEntities; eo.PrefabId = prefabId; eo.Flags = flags; if (prefabId.Value == 0) { eo.Flags |= EntityFlags.SCENE_OBJECT; eo.SceneId = eo.UnityObject.SceneGuid; } if (eo.UnityObject.persistThroughSceneLoads) { eo.Flags |= EntityFlags.PERSIST_ON_LOAD; } if (eo.UnityObject.clientPredicted) { eo.Flags |= EntityFlags.CONTROLLER_LOCAL_PREDICTION; } // create serializer eo.Serializer = Factory.NewSerializer(serializerId); eo.Serializer.OnCreated(eo); // done return(eo); }
public void Attach() { NetAssert.NotNull(UnityObject); NetAssert.False(IsAttached); NetAssert.True((NetworkId.Packed == 0UL) || (Source != null)); try { AttachIsRunning = true; // mark as don't destroy on load GameObject.DontDestroyOnLoad(UnityObject.gameObject); // assign network id if (Source == null) { NetworkId = NetworkIdAllocator.Allocate(); } // add to entities list Core.entitiesThawed.AddLast(this); // mark as attached Flags |= EntityFlags.ATTACHED; // call out to behaviours foreach (IEntityBehaviour eb in Behaviours) { try { if (eb.Invoke && ReferenceEquals(eb.entity, this.UnityObject)) { eb.Attached(); } } catch (Exception exn) { NetLog.Error("User code threw exception inside Attached callback"); NetLog.Exception(exn); } } // call out to user try { GlobalEventListenerBase.EntityAttachedInvoke(this.UnityObject); } catch (Exception exn) { NetLog.Error("User code threw exception inside Attached callback"); NetLog.Exception(exn); } // log NetLog.Debug("Attached {0} (Token: {1})", this, AttachToken); } finally { AttachIsRunning = false; } }