下面例子通过反射功能调用TextureImporter对象中的GetWidthAndHeight方法,在纹理导入之前就能得到纹理的宽高。
using UnityEditor;
using System.Reflection;
using UnityEngine;
public class SpriteSheetPostprocessor:AssetPostprocessor{
private void OnPreprocessTexture(){
TextureImporter importer=assetImporter as TextureImporter;
object[] parameters=new object[2]{0,0};
//MethodInfo methodInfo=typeof(TextureImporter).GetMethod("GetWidthAndHeight",BindingFlags.NonPublic|BindingFlags.Instance);
MethodInfo methodInfo=importer.GetType().GetMethod("GetWidthAndHeight",BindingFlags.NonPublic|BindingFlags.Instance);
methodInfo.Invoke(importer,parameters);
Debug.LogFormat("width:{0} height:{1}",parameters[0],parameters[1]);
}
}