Merge pull request #503 from Santarh/textureSizeFix

In Editor, resized exporting texture if MasSize setting value is smaller than original image size.
This commit is contained in:
hiroj
2020-08-17 14:28:38 +09:00
committed by GitHub

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using System.Reflection;
using UnityEditor;
#endif
@@ -122,6 +123,29 @@ namespace UniGLTF
var path = UnityPath.FromAsset(texture);
if (path.IsUnderAssetsFolder)
{
var textureImporter = (TextureImporter) AssetImporter.GetAtPath(path.Value);
var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
if (textureImporter != null && getSizeMethod != null)
{
var args = new object[2] {0, 0};
getSizeMethod.Invoke(textureImporter, args);
var originalWidth = (int) args[0];
var originalHeight = (int) args[1];
var originalSize = Mathf.Max(originalWidth, originalHeight);
var requiredMaxSize = textureImporter.maxTextureSize;
// Resized exporting if MaxSize setting value is smaller than original image size.
if (originalSize > requiredMaxSize)
{
return new BytesWithMime
{
Bytes = TextureItem.CopyTexture(texture, GetColorSpace(textureType), null).EncodeToPNG(),
Mime = "image/png",
};
}
}
if (path.Extension == ".png")
{
return new BytesWithMime