// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using System; using System.Collections.Generic; using System.Linq; using UnityEngine; // ReSharper disable MemberCanBePrivate.Global namespace Doozy.Editor.Common.Extensions { /// Extension methods for Sprites public static class SpriteExtensions { /// Convert Sprite to Texture2D /// Target Sprite public static Texture2D ToTexture2D(this Sprite sprite) { if (sprite == null) throw new NullReferenceException(nameof(sprite)); try { if (sprite.rect.width == sprite.texture.width) { sprite.texture.name = sprite.name; return sprite.texture; } var texture = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height); Color[] newColors = sprite.texture.GetPixels ( Mathf.CeilToInt(sprite.textureRect.x), Mathf.CeilToInt(sprite.textureRect.y), Mathf.CeilToInt(sprite.textureRect.width), Mathf.CeilToInt(sprite.textureRect.height) ); texture.SetPixels(newColors); texture.Apply(); texture.name = sprite.name; return texture; } catch { sprite.texture.name = sprite.name; return sprite.texture; } } /// Convert a collection of Sprite to a collection of Texture2D /// Sprite collection public static IEnumerable ToTexture2D(this IEnumerable sprites) { if (sprites == null) throw new NullReferenceException(nameof(sprites)); return sprites.Select(sprite => sprite.ToTexture2D()); } } }