/// <summary> /// Render and show a preview of a building. /// </summary> /// <param name="building">The building to render</param> public void Show(BuildingData building) { // Update current selection to the new building. currentSelection = building; renderPrefab = (currentSelection == null || currentSelection.name == null) ? null : (PrefabCollection <BuildingInfo> .FindLoaded(currentSelection.name)); // Generate render if there's a selection with a mesh. if (renderPrefab != null && renderPrefab.m_mesh != null) { // Set default values. previewRender.CameraRotation = 210f; previewRender.Zoom = 4f; // Set mesh and material for render. previewRender.SetTarget(renderPrefab); // Set background. previewSprite.texture = previewRender.Texture; noPreviewSprite.isVisible = false; // Render at next update. RenderPreview(); } else { // No valid current selection with a mesh; reset background. previewSprite.texture = null; noPreviewSprite.isVisible = true; } // Hide any empty building names. if (building == null) { buildingName.isVisible = false; buildingLevel.isVisible = false; buildingSize.isVisible = false; } else { // Set and show building name. buildingName.isVisible = true; buildingName.text = currentSelection.DisplayName; UIUtils.TruncateLabel(buildingName, width - 45); buildingName.autoHeight = true; // Set and show building level. buildingLevel.isVisible = true; buildingLevel.text = Translations.Translate("PRR_LEVEL") + " " + Mathf.Min((int)currentSelection.prefab.GetClassLevel() + 1, Util.MaxLevelOf(currentSelection.prefab.GetSubService())); UIUtils.TruncateLabel(buildingLevel, width - 45); buildingLevel.autoHeight = true; // Set and show building size. buildingSize.isVisible = true; buildingSize.text = currentSelection.prefab.GetWidth() + "x" + currentSelection.prefab.GetLength(); UIUtils.TruncateLabel(buildingSize, width - 45); buildingSize.autoHeight = true; } }
/// <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"; }