C# 纵横比不正确导入的图像统一编辑器脚本
我已经编写了一个脚本,当我将图像导入unity时,应该确保图像的纵横比保持不变。C# 纵横比不正确导入的图像统一编辑器脚本,c#,image,unity3d,textures,aspect-ratio,C#,Image,Unity3d,Textures,Aspect Ratio,我已经编写了一个脚本,当我将图像导入unity时,应该确保图像的纵横比保持不变。 以下是脚本: public class PostprocessImages : AssetPostprocessor { void OnPostprocessTexture(Texture2D texture) { TextureImporter textureImporter = (TextureImporter)assetImporter; textureImpo
以下是脚本:
public class PostprocessImages : AssetPostprocessor
{
void OnPostprocessTexture(Texture2D texture)
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.npotScale = TextureImporterNPOTScale.None;
textureImporter.mipmapEnabled = false;
}
}
脚本位于资源中的编辑器文件夹中。
但是,当我导入图像时,会得到以下结果:
这绝对不是我想要的。应该是这样的: 但是,当我只是转到导入图像的检查器,更改一个随机设置并点击apply时,纹理确实得到了正确的纵横比。以及我在代码中修改的设置。当我检查图像时,它们是正确的。但是图像的纵横比不正确。
见下图: 我如何使图像具有正确的纵横比,从getgo开始(当我导入它时)。因为我不想手动操作,因为我必须导入成百上千的图像
如果有不清楚的地方,请告诉我,以便我能澄清 我没有这个问题,但可以想出可能的解决办法 首先要做的是使用
OnPreprocessTexture
函数,而不是onpostprocesseTexture
。这将在导入纹理之前修改纹理
public class PostprocessImages : AssetPostprocessor
{
void OnPreprocessTexture()
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.npotScale = TextureImporterNPOTScale.None;
textureImporter.mipmapEnabled = false;
}
}
如果这不起作用,请在对
纹理
进行更改后调用TextureImport.SaveAndReimport()
,以便Unity在进行更改后将其重新导入。这可能会解决这个问题。值得注意的是,您需要一种方法来确保调用一次TextureImport.SaveAndReimport()
,因为调用TextureImport.SaveAndReimport()
将触发OnPrepreprocessTexture()
。如果不实现一种方法来确定这一点,您将进入一个无限循环的永无止境的纹理导入。在下面的示例中,我使用了一个静态
列表
来实现:
static List<TextureImporter> importedTex = new List<TextureImporter>();
void OnPostprocessTexture(Texture2D texture)
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
/*Make sure that SaveAndReimport is called once ONLY foe each TextureImporter
There would be infinite loop if this is not done
*/
if (!importedTex.Contains(textureImporter))
{
textureImporter.npotScale = TextureImporterNPOTScale.None;
importedTex.Add(textureImporter);
textureImporter.SaveAndReimport();
}
}
static List importedTex=new List();
void on后处理纹理(Texture2D纹理)
{
TextureImporter TextureImporter=(TextureImporter)assetImporter;
/*确保仅为每个TextureImport调用一次SaveAndImport
如果不这样做,将有无限循环
*/
如果(!importedTex.Contains(textureImporter))
{
textureImporter.npotScale=TextureImporterNPOTScale.None;
导入文本添加(textureImporter);
TextureImport.SaveAndReimport();
}
}
不寻常。如果你做资产->重新导入会发生什么?是的,这解决了问题,还是有点奇怪。你试过这个吗?