示例#1
0
        public void OnEntityCreated(string entityType, SpatialOSEntity entity, EntityGameObjectLinker linker)
        {
            var spatialOSPosition = entity.GetComponent <Position.Component>();

            var prefabName = 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);
        }
示例#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);
        }