private IEntityObject InstantiateEntityObject(EntityId entityId, string prefabName)
        {
            // Always ensure that entities are spawned into the same scene that this script was started in.
            // This avoids the following case:
            //  1) Users dynamically loads scene, activates it, spawns their own entities into it.
            //  2) A new entity is spawned by SpatialOS into the new scene.
            //  3) The user destroys the scene, also destroying the SpatialOS-created GameObject.
            //  4) Errors ensue when the user tries to access the destroyed GameObject via the SpatialOS GameObject tracking layer (e.g. Universe.)
            using (new SaveAndRestoreScene(entitySpawningScene))
            {
                var loadedPrefab         = templateProvider.GetEntityTemplate(prefabName);
                var underlyingGameObject = prefabFactory.MakeComponent(loadedPrefab, prefabName);

                var entityObject = new EntityObject(entityId, underlyingGameObject, prefabName,
                                                    interestedComponentUpdaterProvider);

                var entityObjectStorage =
                    underlyingGameObject.GetComponent <EntityObjectStorage>() ??
                    underlyingGameObject.AddComponent <EntityObjectStorage>();

                entityObjectStorage.Initialize(entityObject, spatialCommunicator);

                return(entityObject);
            }
        }
示例#2
0
        private void PrePoolAssets()
        {
            if (assetsToPrePool == null || !assetsToPrePool.Any())
            {
                return;
            }

            foreach (var prefabNameToCount in assetsToPrePool)
            {
                // Prepooling only supports default context at the moment.  Ultimately all this code should be pulled out into user code.
                var requestedCountInPool = prefabNameToCount.Value;
                templateProvider.PrepareTemplate(prefabNameToCount.Key, prefabName =>
                {
                    var prefab = templateProvider.GetEntityTemplate(prefabName);
                    prefabPool.PreparePool(prefab, prefabName, requestedCountInPool);
                },
                                                 exception => Debug.LogErrorFormat("Problem initialising pool for entity {0}: {1}",
                                                                                   prefabNameToCount.Key, exception.Message));
            }
        }