宽高比不正确的导入图像统一编辑器脚本
Aspect ratio incorrect imported image unity editor script
我编写了一个脚本,可以确保在将图像导入 unity 时图像的纵横比保持不变。
这是脚本:
public class PostprocessImages : AssetPostprocessor
{
void OnPostprocessTexture(Texture2D texture)
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.npotScale = TextureImporterNPOTScale.None;
textureImporter.mipmapEnabled = false;
}
}
脚本位于 Assets 的 Editor 文件夹中。
但是当我导入图像时,我得到以下结果:
这绝对不是我要找的。
它应该是这样的:
但是当我进入导入图像的检查器并更改随机设置并点击“应用纹理”时,确实会恢复正确的纵横比。以及我在代码中修改的设置。当我检查图像时是正确的。但是图像没有正确的纵横比..
见下图:
我将如何使图像具有正确的宽高比,从一开始(当我导入它时)。因为我不想手动操作,因为我必须导入大量图像。
如果有什么不清楚的地方,请告诉我,以便我澄清。
我没有这个问题,但可以想到可能的修复方法。
首先要做的是使用OnPreprocessTexture
函数代替OnPostprocessTexture
。这将在导入之前修改纹理。
public class PostprocessImages : AssetPostprocessor
{
void OnPreprocessTexture()
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.npotScale = TextureImporterNPOTScale.None;
textureImporter.mipmapEnabled = false;
}
}
如果这不起作用,请在对 Texture
进行更改后调用 TextureImporter.SaveAndReimport()
,以便 Unity 将在进行更改后重新导入它。这可能会解决问题。值得注意的是,您需要一种方法来确保 TextureImporter.SaveAndReimport()
被调用一次,因为调用 TextureImporter.SaveAndReimport()
会触发 OnPreprocessTexture()
。如果不实施确定这一点的方法,您将进入永无止境的纹理导入的无限循环。在下面的示例中,我使用 static
List
来实现:
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();
}
}
我编写了一个脚本,可以确保在将图像导入 unity 时图像的纵横比保持不变。
这是脚本:
public class PostprocessImages : AssetPostprocessor
{
void OnPostprocessTexture(Texture2D texture)
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.npotScale = TextureImporterNPOTScale.None;
textureImporter.mipmapEnabled = false;
}
}
脚本位于 Assets 的 Editor 文件夹中。 但是当我导入图像时,我得到以下结果:
这绝对不是我要找的。
它应该是这样的:
但是当我进入导入图像的检查器并更改随机设置并点击“应用纹理”时,确实会恢复正确的纵横比。以及我在代码中修改的设置。当我检查图像时是正确的。但是图像没有正确的纵横比..
见下图:
我将如何使图像具有正确的宽高比,从一开始(当我导入它时)。因为我不想手动操作,因为我必须导入大量图像。
如果有什么不清楚的地方,请告诉我,以便我澄清。
我没有这个问题,但可以想到可能的修复方法。
首先要做的是使用OnPreprocessTexture
函数代替OnPostprocessTexture
。这将在导入之前修改纹理。
public class PostprocessImages : AssetPostprocessor
{
void OnPreprocessTexture()
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.npotScale = TextureImporterNPOTScale.None;
textureImporter.mipmapEnabled = false;
}
}
如果这不起作用,请在对 Texture
进行更改后调用 TextureImporter.SaveAndReimport()
,以便 Unity 将在进行更改后重新导入它。这可能会解决问题。值得注意的是,您需要一种方法来确保 TextureImporter.SaveAndReimport()
被调用一次,因为调用 TextureImporter.SaveAndReimport()
会触发 OnPreprocessTexture()
。如果不实施确定这一点的方法,您将进入永无止境的纹理导入的无限循环。在下面的示例中,我使用 static
List
来实现:
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();
}
}