/// <summary>Connect camera to slider.</summary>
    private void Awake()
    {
        // Make sure the given near and far distances are valid, skipping setup if not.
        if (!VerifyRange())
        {
            return;
        }

        // Get required SliderController for getting input from UI Slider.
        SliderController = GetComponent <SliderController>();

        // Make sure that given start distance is within range of given near and far distances.
        if (StartDistance < NearDistance || StartDistance > FarDistance)
        {
            Debug.LogError(ExampleErrors
                           .OutsideRange(this, StartDistance, "Start Distance", NearDistance, FarDistance));
            StartDistance = Mathf.Clamp(StartDistance, NearDistance, FarDistance);
        }

        // Convert start distance to a percent of near and far distances, and adjust slider to this
        // value.
        float sliderFraction = (StartDistance - NearDistance) / (FarDistance - NearDistance);

        OnSlider(sliderFraction);

        // Set slider's starting value, and only after have done so, connect future changes in slider
        // input to zooming in and out.
        SliderController.SetStartingValue(sliderFraction);
        SliderController.OnChange += OnSlider;
    }
Exemple #2
0
    /// <summary>
    /// Use events to connect <see cref="DynamicMapsService"/> to <see cref="BuildingTexturer"/> so
    /// that all extruded buildings can receive a Nine-Sliced <see cref="Material"/>.
    /// </summary>
    private void Awake()
    {
        // Verify that the given Segment Physics layer is valid (positive and within the range of all
        // available physics layers).
        if (SegmentPhysicsLayer < 0 || SegmentPhysicsLayer > 31)
        {
            Debug.LogError(ExampleErrors.OutsideRange(this, SegmentPhysicsLayer, "Segment Physics Layer",
                                                      0, 31));
            return;
        }

        // Convert Segment Physics Layer index into a Layer Mask for ray-casting into this layer only
        // (i.e. for seeing if any segments are hit by a ray-cast).
        SegmentPhysicsLayerMask = 1 << SegmentPhysicsLayer;

        // Get required Building Texturer component on this GameObject.
        BuildingTexturer buildingTexturer = GetComponent <BuildingTexturer>();

        // Get the required Dynamic Maps Service on this GameObject.
        DynamicMapsService dynamicMapsService = GetComponent <DynamicMapsService>();

        // See if any segment types have been given as types to remove buildings over. We check this so
        // that, if no segments types have been given, unnecessary setup can be avoided.
        bool removeAny = RemoveOverRoads || RemoveOverRailways || RemoveOverFerryLanes;

        // If any segment types have been given as types to remove buildings over, then sign up to event
        // called after each new segment is loaded, so can assign a Collider to it and place it in the
        // Segment Physics Layer. Note that:
        // - DynamicMapsService.MapsService is auto-found on first access (so will not be null).
        // - This event must be set now during Awake, so that when Dynamic Maps Service starts loading
        //   the map during Start, this event will be triggered for all Extruded Structures.
        if (removeAny)
        {
            dynamicMapsService.MapsService.Events.SegmentEvents.DidCreate.AddListener(args => {
                args.GameObject.AddComponent <MeshCollider>();
                args.GameObject.layer = SegmentPhysicsLayer;
            });
        }

        // Sign up to event called after each new building is loaded, so can store it for checking
        // against segments and apply a Nine sliced texture.
        dynamicMapsService.MapsService.Events.ExtrudedStructureEvents.DidCreate.AddListener(args => {
            // Store building so it can be check to be over a segment (after all segments are loaded). We
            // skip this if no segment types have been given as types to remove buildings over.
            if (removeAny)
            {
                StoreBuilding(args.GameObject, args.MapFeature);
            }

            // Assign Nine sliced texture to this building.
            buildingTexturer.AssignNineSlicedMaterials(args.GameObject);
        });

        // When all geometry is loaded, check if any buildings are over any segments, and remove them if
        // so. We skip this if no segment types have been given as types to remove buildings over.
        if (removeAny)
        {
            dynamicMapsService.MapsService.Events.MapEvents.Loaded.AddListener(args =>
                                                                               TryRemoveBuildings());
        }
    }