public static bool ClampSize(Texture2D p_texture, float p_minSize = 4, float p_maxSize = 512) { bool v_return = false; p_maxSize = Mathf.Max(2, p_maxSize); p_minSize = Mathf.Max(2, p_minSize); if (p_texture != null) { bool v_needApplyMin = p_texture.width < p_minSize || p_texture.height < p_minSize; bool v_needApplyMax = p_texture.width > p_maxSize || p_texture.height > p_maxSize; if (v_needApplyMin || v_needApplyMax) { float v_sizeToApply = v_needApplyMin ? p_minSize : p_maxSize; float v_widthRatio = (float)v_sizeToApply / p_texture.width; float v_heightRatio = (float)v_sizeToApply / p_texture.height; if (v_widthRatio < v_heightRatio) { p_texture.BilinearScale(Mathf.RoundToInt(p_texture.width * v_widthRatio), Mathf.RoundToInt(p_texture.height * v_widthRatio)); } else { p_texture.BilinearScale(Mathf.RoundToInt(p_texture.width * v_heightRatio), Mathf.RoundToInt(p_texture.height * v_heightRatio)); } v_return = true; } } return(v_return); }
static public Texture2D FetchOrMakeIcon(string texPart, Color color, float scale) { string cacheKey = string.Join("*", texPart, color, scale); if (iconCache.ContainsKey(cacheKey)) { return(iconCache[cacheKey]); } // Make it from stratch string largeCacheKey = string.Join("*", texPart, color, 1f); string overlayCacheKey = string.Join("*", texPart, color, scale); Texture2D largeIcon; if (iconCache.ContainsKey(largeCacheKey)) { largeIcon = iconCache[largeCacheKey]; } else { largeIcon = ContentFinder <Texture2D> .Get("UI/Overlays/RimLoot_" + texPart, true).CloneAsReadable(); // Colorize and that's the largeIcon largeIcon.Colorize(color); largeIcon.Apply(); iconCache[largeCacheKey] = largeIcon; } Texture2D overlayIcon = largeIcon.Clone(); // Scale it and that's the overlayIcon overlayIcon.BilinearScale( Mathf.RoundToInt(largeIcon.width * scale), Mathf.RoundToInt(largeIcon.height * scale) ); overlayIcon.Apply(); // Stash it into cache iconCache[overlayCacheKey] = overlayIcon; return(iconCache[cacheKey]); }
/// <summary> Scale the loaded image down if nesscessary. </summary> private Texture2D Resize(Texture2D image, int maxSize) { if (maxSize < 0) { return(image); } // No need to resize. if (maxSize >= image.width && maxSize >= image.height) { return(image); } float scaleFactor = maxSize / (float)Mathf.Max(image.width, image.height); int newWidth = (int)(image.width * scaleFactor); int newHeight = (int)(image.height * scaleFactor); image.BilinearScale(newWidth, newHeight); return(image); }