某天 策划找到我说,UI变图片变模糊了。因为在用U3D的编辑时候窗口比较小看不出。然后打开正式游戏看,还真的变小。而且就好像被拉伸了一样。这这这,一开始以为是美术给的图上不清楚,在反复找了一下之后,发现原来是Unity在导入图片的时候,会默认把图片的分辨率设置为2的次幂,如一张718*498的图片,会自动变为什么512*512。这张图片在打包出去被资源加载的时候就会被拉伸回718*498(因为那个gameObject图片就是这个大小),所以图片变模糊了。

我们只要把这个NonPowerof2修改为None即可。
因为这时候项目已经开后到后期了,存在这个问题的图片也挺多的,就写了个脚本来统一改了。
PS.要先选中需要的文件or文件夹。
代码如下:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
/// <summary>
/// 这个函数是自动对图片资源Non Power of 2 设置成None等
/// </summary>
public class SetNPOTScale : MonoBehaviour
{
/// <summary>
/// 循环设置选择的图片
/// </summary>
[MenuItem("SetTextureInfo/SetNPOTScaleNone")]
private static void LoopSetTexture()
{
Object[] textures = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
foreach (Texture2D texture in textures)
{
string path = AssetDatabase.GetAssetPath(texture);
TextureImporter texImporter = AssetImporter.GetAtPath(path) as TextureImporter;
texImporter.textureType = TextureImporterType.Default;
texImporter.npotScale = TextureImporterNPOTScale.None;
AssetDatabase.ImportAsset(path);
}
}
Unity在导入非2的次幂分辨率图片时会自动调整为2的次幂,导致图片拉伸模糊。通过设置TextureImporter的NPOTScale为None可以解决此问题。可以使用脚本批量修改已存在的图片资源。
1970

被折叠的 条评论
为什么被折叠?



