← Files UnityARCHIVED FILE

skills/sprite-editor/scripts/GetTextureSourceImageSize.cs

1.31 KB · Oct 2, 2026 · 00:33 UTC

↓ Download file

using UnityEditor;
using UnityEditor.U2D.Sprites;
using UnityEngine;

namespace Editor
{
    static public partial class SpriteEditorUtility
    {
        /// <summary>
        /// Get the original source image size of a texture, which is different from the texture size when the texture is imported with "Max Size" smaller than the original image size.
        /// This method will try to get the original source image size from TextureImporter, if it fails, it will return the texture size as fallback.
        /// </summary>
        /// <param name="texture"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        static public void GetTextureSourceImageSize(Texture2D texture, out int width, out int height)
        {
            SpriteDataProviderFactories factories = new SpriteDataProviderFactories();
            factories.Init();
            var dataProvider = factories.GetSpriteEditorDataProviderFromObject(texture);
            var textureDataProvider = dataProvider?.GetDataProvider<ITextureDataProvider>();
            if(textureDataProvider != null)
            {
                textureDataProvider.GetTextureActualWidthAndHeight(out width, out height);
                return;
            }

            width = texture.width;
            height = texture.height;
        }
    }
}

SHA-256: ae43201540dc294a03bc5689e701ea8a2dccc4392556fd749762b0248e31ad95