unity设置后的大小

原始大小

使用 System.Drawing.Image.FromFile获取原始图片,还需要 System.Drawing的DLL库文件,放到Plugins文件夹下面,代码如下:
using System.Collections.Generic;
using UnityEngine;
using System.Drawing;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine.UI;
public class DefaultSize
{
// 选中任意张图片单机右键
[MenuItem("Assets/获取size")]
static void GetSize()
{
UnityEngine.Object[] files = Selection.objects;
bool isHave = false;
for (int i = 0; i < files.Length; i++)
{
if (files[i] is Texture2D)
{
Texture2D tex = files[i] as Texture2D;
string filePath = AssetDatabase.GetAssetPath(tex);
// 第一种方式
var cIma = System.Drawing.Image.FromFile(filePath);
Debug.LogError("原始大小:" + cIma.Width + " " + cIma.Height);
Debug.LogError("当前大小:" + tex.width + " " + tex.height);
// 第二种方式
TextureImporter textureImporter = AssetImporter.GetAtPath(filePath) as TextureImporter;
int width = 0;
int height = 0;
GetTextureOriginalSize(textureImporter, out width, out height);
Debug.LogError(width + ":" + height);
}
}
}
public static void GetTextureOriginalSize(TextureImporter ti, out int width, out int height)
{
if (ti == null)
{
width = 0;
height = 0;
return;
}
object[] args = new object[2] { 0, 0 };
MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(ti, args);
width = (int)args[0];
height = (int)args[1];
}
}
网友评论