/// <summary> /// Initial Identity, will be called when spawned. If object id == 0, it will generate new object id /// </summary> /// <param name="manager"></param> /// <param name="isSceneObject"></param> /// <param name="objectId"></param> /// <param name="connectionId"></param> internal void Initial(LiteNetLibGameManager manager, bool isSceneObject, uint objectId = 0, long connectionId = -1) { Manager = manager; ObjectId = objectId; ConnectionId = connectionId; UpdateHighestObjectId(objectId); IsDestroyed = false; IsSpawned = true; IsSceneObject = isSceneObject; if (!IsSceneObject) { AssignSceneObjectId(); } if (!IsSetupBehaviours) { // Setup behaviours index, we will use this as reference for network functions // NOTE: Maximum network behaviour for a identity is 255 (included children) Behaviours = GetComponentsInChildren <LiteNetLibBehaviour>(); SyncBehaviours.Clear(); byte loopCounter; for (loopCounter = 0; loopCounter < Behaviours.Length; ++loopCounter) { Behaviours[loopCounter].Setup(loopCounter); if (Behaviours[loopCounter].CanSyncBehaviour()) { SyncBehaviours.Add(Behaviours[loopCounter]); } } IsSetupBehaviours = true; } // If this is host, hide it then will be showned when initialize subscribings if (IsServer && IsClient) { OnServerSubscribingRemoved(); } Manager.Assets.SpawnedObjects.Add(ObjectId, this); if (IsServer && ConnectionId >= 0) { Player.SpawnedObjects.Add(ObjectId, this); Player.Subscribe(ObjectId); } Manager.InterestManager.NotifyNewObject(this); }
public void UpdateSubscribings(HashSet <uint> newSubscribings) { if (!IsServer || ConnectionId < 0 || !Player.IsReady) { // This is not player's networked object return; } // Always add controlled network object to subscribe it LiteNetLibIdentity tempIdentity; newSubscribings.Add(ObjectId); foreach (uint oldSubscribing in Subscribings) { if (oldSubscribing == ObjectId) { continue; } if (!newSubscribings.Contains(oldSubscribing)) { Player.Unsubscribe(oldSubscribing); if (Manager.LogDebug) { Logging.Log(LogTag, $"Player: {ConnectionId} unsubscribe object ID: {oldSubscribing}."); } } } Subscribings.Clear(); foreach (uint newSubscribing in newSubscribings) { if (!Manager.Assets.TryGetSpawnedObject(newSubscribing, out tempIdentity) || tempIdentity.IsDestroyed) { continue; } Subscribings.Add(newSubscribing); Player.Subscribe(newSubscribing); if (Manager.LogDebug) { Logging.Log(LogTag, $"Player: {ConnectionId} subscribe object ID: {newSubscribing}."); } } }
public void AddSubscribing(uint subscribing) { Subscribings.Add(subscribing); Player.Subscribe(subscribing); }