/// <summary>
        /// (Re-)render the preview image if we need to.
        /// Doing it in Update avoids the game 'flickering' in the background that can occur if rendering occurs at random times.
        /// Called by Unity every frame.
        /// </summary>
        public override void Update()
        {
            base.Update();

            // Don't do anything if a render hasn't been signalled.
            if (!queueRender)
            {
                return;
            }

            // Clear flag.
            queueRender = false;

            // Don't do anything if there's no prefab to render.
            if (renderPrefab == null)
            {
                return;
            }

            // If the selected building has colour variations, temporarily set the colour to the default for rendering.
            if (renderPrefab.m_useColorVariations)
            {
                Color originalColor = renderPrefab.m_material.color;
                renderPrefab.m_material.color = renderPrefab.m_color0;
                previewRender.Render(false);
                renderPrefab.m_material.color = originalColor;
            }
            else
            {
                // No temporary colour change needed.
                previewRender.Render(false);
            }
        }
        /// <summary>
        /// Render the preview image.
        /// </summary>
        private void RenderPreview()
        {
            if (renderPrefab == null)
            {
                return;
            }

            // If the selected building has colour variations, temporarily set the colour to the default for rendering.
            if (renderPrefab.m_useColorVariations)
            {
                Color originalColor = renderPrefab.m_material.color;
                renderPrefab.m_material.color = renderPrefab.m_color0;
                previewRender.Render(false);
                renderPrefab.m_material.color = originalColor;
            }
            else
            {
                // No temporary colour change needed.
                previewRender.Render(false);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates building thumbnail images (normal, focused, hovered, pressed and disabled) for the given building prefab.
        /// Thumbnails are no longer applied to the m_Thumbnail and m_Atlas fields of the prefab, but to the BuildingData record.
        /// </summary>
        /// <param name="prefab">The BuildingInfo prefab to generate thumbnails for</param>
        /// <param name="name">The display name of the prefab.</param>
        internal void CreateThumbnail(BuildingData building)
        {
            // Reset zoom.
            renderer.Zoom = 4f;

            // Don't do anything with null prefabs.
            if (building?.prefab == null)
            {
                return;
            }

            // Set mesh and material for render.
            if (!renderer.SetTarget(building.prefab))
            {
                // Something went wrong - this isn't a valid rendering target; exit.
                Logging.Message("no thumbnail generated for null mesh ", building.prefab.name);
                return;
            }

            // If the selected building has colour variations, temporarily set the colour to the default for rendering.
            if (building.prefab.m_useColorVariations)
            {
                Color originalColor = building.prefab.m_material.color;
                building.prefab.m_material.color = building.prefab.m_color0;
                renderer.Render(true);
                building.prefab.m_material.color = originalColor;
            }
            else
            {
                // No temporary colour change needed.
                renderer.Render(true);
            }

            // Back up game's current active texture.
            RenderTexture gameActiveTexture = RenderTexture.active;

            // Convert the render to a 2D texture.
            Texture2D thumbnailTexture = new Texture2D(renderer.Texture.width, renderer.Texture.height);

            RenderTexture.active = renderer.Texture;
            thumbnailTexture.ReadPixels(new Rect(0f, 0f, (float)renderer.Texture.width, (float)renderer.Texture.height), 0, 0);
            thumbnailTexture.Apply();

            // Temporary texture for resizing render to thumbnail size (109 x 100).
            RenderTexture resizingTexture = RenderTexture.GetTemporary(109, 100);

            // Resize 2D texture (to 109 x 100) using trilinear filtering.
            resizingTexture.filterMode  = FilterMode.Trilinear;
            thumbnailTexture.filterMode = FilterMode.Trilinear;

            // Resize.
            Graphics.Blit(thumbnailTexture, resizingTexture);
            thumbnailTexture.Resize(109, 100);
            thumbnailTexture.ReadPixels(new Rect(0, 0, 109, 100), 0, 0);
            thumbnailTexture.Apply();

            // Release temporary texture.
            RenderTexture.ReleaseTemporary(resizingTexture);

            // Restore game's current active texture.
            RenderTexture.active = gameActiveTexture;

            // Thumbnail texture name is the same as the building's displayed name.
            thumbnailTexture.name = building.DisplayName;

            // Create new texture atlas with thumnails.
            UITextureAtlas thumbnailAtlas = ScriptableObject.CreateInstance <UITextureAtlas>();

            thumbnailAtlas.name                 = "RICOThumbnails_" + building.DisplayName;
            thumbnailAtlas.material             = UnityEngine.Object.Instantiate <Material>(UIView.GetAView().defaultAtlas.material);
            thumbnailAtlas.material.mainTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
            AddTexturesToAtlas(thumbnailAtlas, GenerateThumbnailVariants(thumbnailTexture));

            // Add atlas to our building data record.
            building.thumbnailAtlas = thumbnailAtlas;
        }
        /// <summary>
        /// Generates building thumbnail images (normal, focused, hovered, pressed and disabled) for the given building prefab.
        /// Thumbnails are applied to the m_Thumbnail and m_Atlas fields of the prefab.
        /// </summary>
        /// <param name="prefab">The BuildingInfo prefab to generate thumbnails for</param>
        /// <param name="name">The display name of the prefab.</param>
        internal static void CreateThumbnail(BuildingData building)
        {
            // Create the renderer if it hasn't already been set up.
            if (thumbnailRenderer == null)
            {
                // Use a unique GameObject name to help find it with ModTools.
                thumbnailRenderer = new GameObject("RICORevisitedThumbnailRenderer").AddComponent <UIPreviewRenderer>();

                // Size and setting for thumbnail images: 109 x 100, doubled for anti-aliasing.
                thumbnailRenderer.Size           = new Vector2(109, 100) * 2f;
                thumbnailRenderer.CameraRotation = 210f;
            }

            // Reset zoom.
            thumbnailRenderer.Zoom = 4f;

            // Don't do anything with null prefabs or prefabs without buttons.
            if (building == null || building.buildingButton == null)
            {
                return;
            }

            // Set mesh and material for render.
            thumbnailRenderer.SetTarget(building.prefab);

            if (thumbnailRenderer.Mesh == null)
            {
                // If the prefab itself has no mesh, see if there's any sub-buildings to render instead (e.g. Boston Residence Garage).
                if (building.prefab.m_subBuildings.Count() > 0)
                {
                    // Use first sub-building as render target; set mesh and material.
                    thumbnailRenderer.Mesh     = building.prefab.m_subBuildings[0].m_buildingInfo.m_mesh;
                    thumbnailRenderer.material = building.prefab.m_subBuildings[0].m_buildingInfo.m_material;
                }
            }

            // If we still haven't gotten a mesh after the above, then something's not right; exit.
            if (thumbnailRenderer.Mesh == null)
            {
                Debugging.Message("no thumbnail generated for null mesh " + building.prefab.name);
                return;
            }

            // If the selected building has colour variations, temporarily set the colour to the default for rendering.
            if (building.prefab.m_useColorVariations)
            {
                Color originalColor = building.prefab.m_material.color;
                building.prefab.m_material.color = building.prefab.m_color0;
                thumbnailRenderer.Render(true);
                building.prefab.m_material.color = originalColor;
            }
            else
            {
                // No temporary colour change needed.
                thumbnailRenderer.Render(true);
            }

            // Back up game's current active texture.
            RenderTexture gameActiveTexture = RenderTexture.active;

            // Convert the render to a 2D texture.
            Texture2D thumbnailTexture = new Texture2D(thumbnailRenderer.Texture.width, thumbnailRenderer.Texture.height);

            RenderTexture.active = thumbnailRenderer.Texture;
            thumbnailTexture.ReadPixels(new Rect(0f, 0f, (float)thumbnailRenderer.Texture.width, (float)thumbnailRenderer.Texture.height), 0, 0);
            thumbnailTexture.Apply();

            // Temporary texture for resizing render to thumbnail size (109 x 100).
            RenderTexture resizingTexture = RenderTexture.GetTemporary(109, 100);

            // Resize 2D texture (to 109 x 100) using trilinear filtering.
            resizingTexture.filterMode  = FilterMode.Trilinear;
            thumbnailTexture.filterMode = FilterMode.Trilinear;

            // Resize.
            Graphics.Blit(thumbnailTexture, resizingTexture);
            thumbnailTexture.Resize(109, 100);
            thumbnailTexture.ReadPixels(new Rect(0, 0, 109, 100), 0, 0);
            thumbnailTexture.Apply();

            // Release temporary texture.
            RenderTexture.ReleaseTemporary(resizingTexture);

            // Restore game's current active texture.
            RenderTexture.active = gameActiveTexture;

            // Thumbnail texture name is the same as the building's displayed name.
            thumbnailTexture.name = building.displayName;

            // Create new texture atlas with thumnails.
            UITextureAtlas thumbnailAtlas = ScriptableObject.CreateInstance <UITextureAtlas>();

            thumbnailAtlas.name                 = "RICOThumbnails_" + building.displayName;
            thumbnailAtlas.material             = UnityEngine.Object.Instantiate <Material>(UIView.GetAView().defaultAtlas.material);
            thumbnailAtlas.material.mainTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
            AddTexturesToAtlas(thumbnailAtlas, GenerateThumbnailVariants(thumbnailTexture));

            // Add atlas to building button.
            building.buildingButton.atlas          = thumbnailAtlas;
            building.buildingButton.normalFgSprite = thumbnailTexture.name;

            // Variants.
            building.buildingButton.focusedFgSprite  = thumbnailTexture.name + "Focused";
            building.buildingButton.hoveredFgSprite  = thumbnailTexture.name + "Hovered";
            building.buildingButton.pressedFgSprite  = thumbnailTexture.name + "Pressed";
            building.buildingButton.disabledFgSprite = thumbnailTexture.name + "Disabled";
        }