Exemple #1
0
        public static void NotifyDownload(PrefabInfo prefab)
        {
            Profiler.Info("Notify user of new prefab: {0}", prefab.name);
            var list = UIView.GetAView().GetComponentsInChildren <ChirpPanel>();

            if (list.Count() > 0)
            {
                var panel = list[0];
                panel.AddMessage(new ChirperMessage(prefab));

                var msgsPanel = panel.GetFieldValue <UIScrollablePanel>("m_Container");
                var msgPanel  = msgsPanel.components[msgsPanel.components.Count() - 1];
                var msgButton = (UILabel)msgPanel.components[1];
                msgButton.clipChildren = false;

                var button = msgButton.AddUIComponent <UIButton>();
                button.height           = 75; button.width = 75;
                button.relativePosition = new Vector3(5, 20);
                button.objectUserData   = prefab;
                button.atlas            = prefab.m_Atlas;
                button.normalFgSprite   = prefab.m_Thumbnail;
                button.focusedFgSprite  = prefab.m_Thumbnail + "Focused";
                button.hoveredFgSprite  = prefab.m_Thumbnail + "Hovered";
                button.pressedFgSprite  = prefab.m_Thumbnail + "Pressed";
                button.disabledFgSprite = prefab.m_Thumbnail + "Disabled";

                if (prefab.m_Thumbnail == null || prefab.m_Thumbnail == "")
                {
                    button.normalFgSprite = "ToolbarIconProps";
                }

                string      localizedTooltip = prefab.GetLocalizedTooltip();
                int         hashCode         = TooltipHelper.GetHashCode(localizedTooltip);
                UIComponent tooltipBox       = GeneratedPanel.GetTooltipBox(hashCode);

                button.tooltip    = localizedTooltip;
                button.tooltipBox = tooltipBox;

                button.eventClick += PrefabButtonClicked;
            }
        }
        void PatchIcons(
            PrefabInfo info,
            Dictionary <string, Package.Asset> thumbnails,
            Dictionary <string, Package.Asset> tooltips,
            Dictionary <string, DateTime> timestamps)
        {
            Texture2D[] textures         = null;
            Texture2D   thumbnailTexture = null;
            Texture2D   tooltipTexture   = null;
            Boolean     haveThumbnail    = false;
            Boolean     haveTooltip      = false;
            string      packageName      = info.name.Split('.')[0];

            if (!timestamps.ContainsKey(packageName))
            {
                // No chance at patching this icon.
                return;
            }

            if (File.Exists(Path.Combine("IAICache", packageName + ".skip")))
            {
                Debug.Log(String.Format("Skipping {0} due to skip file", packageName));
                return;
            }

            UIButton   uiButton   = null;
            GameObject gameObject = GameObject.Find(info.name);

            if (gameObject != null)
            {
                uiButton = gameObject.GetComponent <UIButton>();
            }
            // Check the cache for this item.
            if (LoadFromCache(packageName, timestamps[packageName], ref textures, ref haveThumbnail, ref haveTooltip))
            {
                ++loadCount;
            }
            else
            {
                if (!string.IsNullOrEmpty(info.m_Thumbnail))
                {
                    // This item has a thumbnail, check if it's a generated one.
                    if (uiButton != null && uiButton.atlas != null)
                    {
                        var focusedSpriteInfo = uiButton.atlas[uiButton.focusedFgSprite];
                        if (focusedSpriteInfo != null && focusedSpriteInfo.texture != null)
                        {
                            long nonBlueCount = 0;
                            try
                            {
                                Color32[] focusedPixels = focusedSpriteInfo.texture.GetPixels32();
                                // The default atlas generator creates ugly dark blue focused icons
                                // by removing everything from the red and green channel.  Detect these
                                // by adding up the amount of red and green in the image.
                                foreach (Color32 pixel in focusedPixels)
                                {
                                    if (pixel.a > 32)
                                    {
                                        nonBlueCount += pixel.r + pixel.g;
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                Debug.Log(String.Format("Failed to patch texture for {0}, skipping.", info.name));
                                return;
                            }
                            if (nonBlueCount < 10000)
                            {
                                // This is a generated atlas.  Replace the focused icon by generating
                                // a new atlas.  Include the tooltip if there is one.
                                var thumbnailSpriteInfo = uiButton.atlas[uiButton.normalFgSprite];
                                if (thumbnailSpriteInfo != null && thumbnailSpriteInfo.texture != null)
                                {
                                    thumbnailTexture      = thumbnailSpriteInfo.texture;
                                    thumbnailTexture.name = info.name + "Icon";
                                }
                                var tooltipSpriteInfo = uiButton.atlas["tooltip"];
                                if (tooltipSpriteInfo != null && thumbnailSpriteInfo.texture != null)
                                {
                                    tooltipTexture      = tooltipSpriteInfo.texture;
                                    tooltipTexture.name = info.name;
                                }
                            }
                        }
                    }
                }

                // Create a thumbnail
                if (string.IsNullOrEmpty(info.m_Thumbnail))
                {
                    // Try loading an override image.
                    thumbnailTexture = LoadOverride(packageName, "thumb");

                    if (thumbnailTexture == null && thumbnails.ContainsKey(packageName))
                    {
                        // Use the steam workshop image.
                        thumbnailTexture = thumbnails[packageName].Instantiate <Texture2D>();
                    }

                    if (thumbnailTexture != null)
                    {
                        thumbnailTexture.wrapMode = TextureWrapMode.Clamp;
                        thumbnailTexture.name     = info.name + "Icon";
                        if (thumbnailTexture.width > thumbnailTexture.height)
                        {
                            ScaleTexture(thumbnailTexture, THUMBNAIL_SIZE, (THUMBNAIL_SIZE * thumbnailTexture.height) / thumbnailTexture.width);
                        }
                        else
                        {
                            ScaleTexture(thumbnailTexture, (THUMBNAIL_SIZE * thumbnailTexture.width) / thumbnailTexture.height, THUMBNAIL_SIZE);
                        }
                    }
                }

                // Create a tooltip image based on the steam workshop image.
                if (string.IsNullOrEmpty(info.m_InfoTooltipThumbnail))
                {
                    // Try loading an override image
                    tooltipTexture = LoadOverride(packageName, "tooltip");

                    if (tooltipTexture == null && tooltips.ContainsKey(packageName))
                    {
                        // Use the steam workshop image.
                        tooltipTexture = tooltips[packageName].Instantiate <Texture2D>();
                    }

                    if (tooltipTexture != null)
                    {
                        // The tooltip texture name must match the info name, as that's the key value that's used
                        // (stored on UIButton.m_Tooltip, not by us).
                        tooltipTexture.name = info.name;
                        // Crop and scale the tooltip to TOOLTIP_WIDTH x TOOLTIP_HEIGHT
                        if (((float)tooltipTexture.width / (float)tooltipTexture.height) > (float)TOOLTIP_WIDTH / (float)TOOLTIP_HEIGHT)
                        {
                            // Picture is too wide, scale to TOOLTIP_HEIGHT pixels tall and then crop out the middle
                            ScaleTexture(tooltipTexture, (TOOLTIP_HEIGHT * tooltipTexture.width) / tooltipTexture.height, TOOLTIP_HEIGHT);
                            CropTexture(tooltipTexture, (tooltipTexture.width - TOOLTIP_WIDTH) / 2, 0, TOOLTIP_WIDTH, TOOLTIP_HEIGHT);
                        }
                        else if (((float)tooltipTexture.width / (float)tooltipTexture.height) < (float)TOOLTIP_WIDTH / (float)TOOLTIP_HEIGHT)
                        {
                            // Picture is too tall, scale to TOOLTIP_WIDTH pixels wide and then crop out the middle
                            ScaleTexture(tooltipTexture, TOOLTIP_WIDTH, (TOOLTIP_WIDTH * tooltipTexture.height) / tooltipTexture.width);
                            CropTexture(tooltipTexture, 0, (tooltipTexture.height - TOOLTIP_HEIGHT) / 2, TOOLTIP_WIDTH, TOOLTIP_HEIGHT);
                        }
                        else
                        {
                            // Picture is the right aspect ratio, just scale it
                            ScaleTexture(tooltipTexture, TOOLTIP_WIDTH, TOOLTIP_HEIGHT);
                        }
                    }
                }

                // Build these textures into an atlas.
                if (thumbnailTexture != null)
                {
                    if (tooltipTexture != null)
                    {
                        textures = new Texture2D[] { thumbnailTexture, null, null, null, null, tooltipTexture };
                    }
                    else
                    {
                        textures = new Texture2D[] { thumbnailTexture, null, null, null, null };
                    }
                    GenerateMissingThumbnailVariants(ref textures);
                }
                else if (tooltipTexture != null)
                {
                    textures = new Texture2D[] { tooltipTexture };
                }
                else
                {
                    // Hmm, we've failed to make any textures.  Move on to the next object.
                    return;
                }
                haveThumbnail = thumbnailTexture != null;
                haveTooltip   = tooltipTexture != null;
                SaveToCache(packageName, timestamps[packageName], textures, haveThumbnail, haveTooltip);
            }
            UITextureAtlas atlas = AssetImporterThumbnails.CreateThumbnailAtlas(textures, info.name + "Atlas");

            if (haveThumbnail)
            {
                // Store the thumbnail atlas and icon names on the uiButton for this building.
                if (uiButton != null)
                {
                    uiButton.atlas = atlas;
                    string baseIconName = info.name + "Icon";
                    uiButton.normalFgSprite   = baseIconName;
                    uiButton.focusedFgSprite  = baseIconName + "Focused";
                    uiButton.hoveredFgSprite  = baseIconName + "Hovered";
                    uiButton.pressedFgSprite  = baseIconName + "Pressed";
                    uiButton.disabledFgSprite = baseIconName + "Disabled";
                }
                // Store the thumbnail atlas for this building.
                info.m_Atlas = atlas;
                // Store the name of the thumbnail.
                info.m_Thumbnail = info.name + "Icon";
            }
            if (haveTooltip)
            {
                // Store the tooltip atlas for this building.
                info.m_InfoTooltipAtlas = atlas;
                // This is actually the transition thumbnail, we set it to the default to blank
                // out the tooltip during transitions, which matches the behaviour of the
                // existing icons.
                info.m_InfoTooltipThumbnail = "";
                if (uiButton != null)
                {
                    uiButton.tooltip = info.GetLocalizedTooltip();
                }
            }

            ++patchCount;
        }