/// <summary>
        /// Assign a randomly chosen Wall and Roof <see cref="Material"/> to a given building.
        /// </summary>
        /// <param name="building">Building to assign <see cref="Material"/>s to.</param>
        /// <param name="index">
        /// Optional index of Wall and Roof <see cref="Material"/> pair to apply. If this value is not
        /// set a random <see cref="Material"/> pair will be used.
        /// </param>
        internal void AssignNineSlicedMaterials(GameObject building, int?index = null)
        {
            // If a specific Material index was given, verify it is a valid index for a Wall and Roof
            // Material pair.
            if (index.HasValue)
            {
                if (index.Value < 0 || index.Value >= WallMaterials.Length)
                {
                    Debug.LogError(
                        ExampleErrors.InvalidArrayIndex(this, WallMaterials, "Wall Materials", index.Value));

                    return;
                }
            }
            else
            {
                // Pick a random Material index to use for both Wall and Roof Materials. Not that the same
                // index will work for both arrays of Materials, as we have already verified that the Wall
                // and Roof Material arrays are the same length.
                index = Random.Range(0, WallMaterials.Length);
            }

            // Replace building MeshRenderer's sharedMaterials array with chosen Materials. Note that this
            // must be done by creating a new array of Materials, rather than altering the entries of this
            // MeshRenderer's sharedMaterials array, as altering the existing array will not actually
            // change the MeshRenderer's Materials.
            MeshRenderer buildingMeshRenderer = building.GetComponent <MeshRenderer>();

            buildingMeshRenderer.sharedMaterials =
                new Material[] { WallMaterials[index.Value], RoofMaterials[index.Value] };
        }
        /// <summary>
        /// Verify given <see cref="Material"/> arrays are valid (not empty nor containing any null
        /// entries, and both arrays of the same length).
        /// </summary>
        private void Awake()
        {
            // Verify that at least one Wall Material and at least one Roof Material has been given.
            if (WallMaterials.Length == 0)
            {
                Debug.LogError(ExampleErrors.EmptyArray(this, WallMaterials, "Wall Materials"));

                return;
            }

            if (RoofMaterials.Length == 0)
            {
                Debug.LogError(ExampleErrors.EmptyArray(this, RoofMaterials, "Roof Materials"));

                return;
            }

            // Verify that the same number of Wall and Roof Materials have been given.
            if (WallMaterials.Length != RoofMaterials.Length)
            {
                Debug.LogErrorFormat(
                    "Incorrect number of Building Roof Materials defined for {0}.{1}: {2} " +
                    "Building Wall Materials were given, but {3} Building Roof Materials were given." +
                    "\n{1} needs the same number of Building Roof Materials as Building Wall " +
                    "Materials, i.e. {2} of each.",
                    name,
                    GetType(),
                    WallMaterials.Length,
                    RoofMaterials.Length);

                return;
            }

            // Verify that no null Materials have been given.
            for (int i = 0; i < WallMaterials.Length; i++)
            {
                if (WallMaterials[i] == null)
                {
                    Debug.LogError(ExampleErrors.NullArrayElement(this, WallMaterials, "Wall Materials", i));

                    return;
                }

                if (RoofMaterials[i] == null)
                {
                    Debug.LogError(ExampleErrors.NullArrayElement(this, RoofMaterials, "Roof Materials", i));

                    return;
                }
            }

            // If have reached this point then have verified that all required parts are present and
            // properly set up.
        }
Exemplo n.º 3
0
        /// <summary>
        /// Use <see cref="CameraController"/>'s OnMove event to detect when the <see cref="Camera"/>
        /// has moved far enough that the Floating Origin needs to be recentered.
        /// </summary>
        private void Awake()
        {
            if (MapsService == null)
            {
                Debug.LogError(ExampleErrors.MissingParameter(
                                   this, MapsService, "Maps Service", "is required for this script to work."));

                return;
            }

            // Verify a Camera Controller has been given.
            if (CameraController == null)
            {
                Debug.LogError(ExampleErrors.MissingParameter(
                                   this, CameraController, "Camera Controller", "to tell when the Camera has moved"));

                return;
            }

            // Verify that a valid Floating Origin range was given, i.e. that given distance was not
            // negative nor zero. Comparison is made to float.Epsilon instead of zero to account for float
            // rounding errors.
            if (FloatingOriginRange <= float.Epsilon)
            {
                Debug.LogError(ExampleErrors.NotGreaterThanZero(
                                   this,
                                   FloatingOriginRange,
                                   "Floating Origin Range",
                                   "to tell how far the Camera should move before the Floating " + "Origin is reset"));

                return;
            }

            // Store the initial position of the Camera on the ground plane.
            FloatingOrigin = GetCameraPositionOnGroundPlane();

            // If no additional GameObjects have been set (to be moved when the world's Floating Origin is
            // recentered), set this array to be just Camera.main's GameObject. This is so that, by
            // default, the scene's Camera is moved when the world is recentered, resulting in a seamless
            // recentering of the world that should be invisible to the user.
            if (AdditionalGameObjects == null)
            {
                AdditionalGameObjects = new[] { Camera.main.gameObject };
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Verify that all required parameters have been correctly defined, returning false if not.
        /// </summary>
        private bool VerifyParameters()
        {
            // TODO(b/149056787): Standardize parameter verification across scripts.
            // Verify that a Ground plane has been given.
            if (Ground == null)
            {
                Debug.LogError(ExampleErrors.MissingParameter(this, Ground, "Ground"));

                return(false);
            }

            // Verify that there is a Camera.main in the scene (i.e. a Camera that is tagged:
            // "MainCamera").
            if (Camera.main == null)
            {
                Debug.LogError(ExampleErrors.NullMainCamera(this));

                return(false);
            }

            // If have reached this point then we have verified all required parameters.
            return(true);
        }