private static void addPrefabForType(StructureMetadata.UsageType type, string path,
                                             Dictionary <StructureMetadata.UsageType, ExtrudedStructureStyle> mapping)
        {
            Object                 prefabModel         = Resources.Load(path);
            GameObject             prefab              = (GameObject)Instantiate(prefabModel, Vector3.zero, Quaternion.identity);
            ExtrudedStructureStyle extrudedPrefabStyle =
                new ExtrudedStructureStyle.Builder {
                Prefab = prefab
            }.Build();

            mapping.Add(type, extrudedPrefabStyle);
        }
Exemple #2
0
        /// <summary>
        /// Check if a building of a given <see cref="StructureMetadata.UsageType"/> should be replaced
        /// with a prefab.
        /// </summary>
        /// <param name="usage"><see cref="StructureMetadata.UsageType"/> of this building.</param>
        private bool ShouldReplaceBuilding(StructureMetadata.UsageType usage)
        {
            switch (usage)
            {
            case StructureMetadata.UsageType.Bar:
                return(ReplaceBars);

            case StructureMetadata.UsageType.Bank:
                return(ReplaceBanks);

            case StructureMetadata.UsageType.Lodging:
                return(ReplaceLodgings);

            case StructureMetadata.UsageType.Cafe:
                return(ReplaceCafes);

            case StructureMetadata.UsageType.Restaurant:
                return(ReplaceRestaurants);

            case StructureMetadata.UsageType.EventVenue:
                return(ReplaceEventVenues);

            case StructureMetadata.UsageType.TouristDestination:
                return(ReplaceTouristDestinations);

            case StructureMetadata.UsageType.Shopping:
                return(ReplaceShops);

            case StructureMetadata.UsageType.School:
                return(ReplaceSchools);

            case StructureMetadata.UsageType.Unspecified:
                return(ReplaceUnspecifieds);

            default:
                Debug.LogErrorFormat(
                    "{0}.{1} encountered an unhandled {2} of '{3}' for building.\n{0} is " +
                    "not yet setup to handle {3} type buildings.",
                    name,
                    GetType(),
                    typeof(StructureMetadata.UsageType),
                    usage);

                return(false);
            }
        }
    /// <summary>
    /// Use <see cref="DynamicMapsService"/> to load geometry, replacing any buildings as needed.
    /// </summary>
    private void Awake()
    {
        // Make sure a prefab has been specified.
        if (Prefab == null)
        {
            Debug.LogError(ExampleErrors.MissingParameter(this, Prefab, "Prefab",
                                                          "to replace specific buildings with"));
            return;
        }

        // Get required DynamicMapsService component on this GameObject.
        DynamicMapsService dynamicMapsService = GetComponent <DynamicMapsService>();

        // See if any options have been set indicating which types of buildings to replace, signing up
        // to WillCreate events if so.
        if (ReplaceBars || ReplaceBanks || ReplaceLodgings || ReplaceCafes || ReplaceRestaurants ||
            ReplaceEventVenues || ReplaceTouristDestinations || ReplaceShops || ReplaceSchools ||
            ReplaceUnspecifieds)
        {
            // Create styles for ExtrudedStructure and ModeledStructure type buildings that are to be
            // replaced with a prefab.
            createStyles();

            // Sign up to events called just before any new building is loaded, so we can check each
            // building's usage type and replace it with prefab if needed. Note that:
            // - DynamicMapsService.MapsService is auto-found on first access (so will not be null).
            // - These events must be set now during Awake, so that when DynamicMapsService starts
            //   loading the map during Start, these event will be triggered for all ExtrudedStructures
            //   and ModeledStructures.
            dynamicMapsService.MapsService.Events.ExtrudedStructureEvents.WillCreate.AddListener(args => {
                StructureMetadata.UsageType usage = args.MapFeature.Metadata.Usage;
                if (ShouldReplaceBuilding(usage))
                {
                    args.Style = extrudedStructureStyles.ContainsKey(usage.ToString()) ? extrudedStructureStyles[usage.ToString()] : extrudedPrefabStyle;
                }
                if (args.MapFeature.Metadata.PlaceId == CustomLocation)
                {
                    args.Style = extrudedStructureStyles["Custom"];
                }
            });
            dynamicMapsService.MapsService.Events.ModeledStructureEvents.WillCreate.AddListener(args => {
                StructureMetadata.UsageType usage = args.MapFeature.Metadata.Usage;
                if (ShouldReplaceBuilding(usage))
                {
                    args.Style = modeledStructureStyles.ContainsKey(usage.ToString()) ? modeledStructureStyles[usage.ToString()] : modeledPrefabStyle;
                }
                if (args.MapFeature.Metadata.PlaceId == CustomLocation)
                {
                    args.Style = modeledStructureStyles["Custom"];
                }
            });
        }

        // See if we should be replacing any suppressed buildings with prefab, signing up to DidCreate
        // event if so.
        if (ReplaceSuppressed)
        {
            // Sign up to event called just after any new building is loaded, so we can check if the
            // building's mesh has been suppressed and should be replaced with a prefab.
            dynamicMapsService.MapsService.Events.ExtrudedStructureEvents.DidCreate.AddListener(
                args => TryReplaceSuppressedBuilding(args.GameObject));
            dynamicMapsService.MapsService.Events.ModeledStructureEvents.DidCreate.AddListener(
                args => TryReplaceSuppressedBuilding(args.GameObject));
        }
    }