public void OnEntityCreated(SpatialOSEntity entity, EntityGameObjectLinker linker)
        {
            if (!entity.TryGetComponent <Metadata.Component>(out var metadata) ||
                !entity.TryGetComponent <Position.Component>(out var spatialOSPosition))
            {
                return;
            }

            var prefabName = metadata.EntityType;
            var position   = spatialOSPosition.Coords.ToUnityVector() + workerOrigin;

            if (!cachedPrefabs.TryGetValue(prefabName, out var prefab))
            {
                var workerSpecificPath = Path.Combine("Prefabs", workerType, prefabName);
                var commonPath         = Path.Combine("Prefabs", "Common", prefabName);

                prefab = Resources.Load <GameObject>(workerSpecificPath) ?? Resources.Load <GameObject>(commonPath);
                cachedPrefabs[prefabName] = prefab;
            }

            if (prefab == null)
            {
                return;
            }

            var gameObject = Object.Instantiate(prefab, position, Quaternion.identity);

            gameObject.name = $"{prefab.name}(SpatialOS: {entity.SpatialOSEntityId}, Worker: {workerType})";

            entityIdToGameObject.Add(entity.SpatialOSEntityId, gameObject);
            linker.LinkGameObjectToSpatialOSEntity(entity.SpatialOSEntityId, gameObject, componentsToAdd);
        }
Exemple #2
0
        public GameObject OnEntityCreated(SpatialOSEntity entity)
        {
            if (!entity.HasComponent <Metadata.Component>())
            {
                return(null);
            }

            var prefabName = entity.GetComponent <Metadata.Component>().EntityType;

            if (!entity.HasComponent <Position.Component>())
            {
                cachedPrefabs[prefabName] = null;
                return(null);
            }

            var spatialOSPosition = entity.GetComponent <Position.Component>();
            var position          = new Vector3((float)spatialOSPosition.Coords.X, (float)spatialOSPosition.Coords.Y, (float)spatialOSPosition.Coords.Z) +
                                    workerOrigin;
            var workerSpecificPath = Path.Combine("Prefabs", workerType, prefabName);
            var commonPath         = Path.Combine("Prefabs", "Common", prefabName);

            if (!cachedPrefabs.TryGetValue(prefabName, out var prefab))
            {
                prefab = Resources.Load <GameObject>(workerSpecificPath);
                if (prefab == null)
                {
                    prefab = Resources.Load <GameObject>(commonPath);
                }

                cachedPrefabs[prefabName] = prefab;
            }

            if (prefab == null)
            {
                return(null);
            }

            var gameObject = Object.Instantiate(prefab, position, Quaternion.identity);

            gameObject.name = $"{prefab.name}(SpatialOS: {entity.SpatialOSEntityId}, Worker: {workerType})";
            return(gameObject);
        }