Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 如何限制整个项目的纹理大小_Unity3d - Fatal编程技术网

Unity3d 如何限制整个项目的纹理大小

Unity3d 如何限制整个项目的纹理大小,unity3d,Unity3d,我正在使用Unity3d开发游戏 项目中存在许多纹理 如何限制整个项目的纹理大小?如果您希望在默认情况下限制导入纹理的大小,除了在导入后编辑最大大小之外,没有其他直接方法。默认值始终为1024 但是,您可以编写一个自定义项,该自定义项(尽管名称不同)还具有一个可用于设置导入设置的方法。一个非常简单的例子如下: 使用UnityEngine; 使用UnityEditor; 公共类DefaultTextureSizer:AssetPostprocessor { void纹理() { TextureIm

我正在使用Unity3d开发游戏

项目中存在许多纹理


如何限制整个项目的纹理大小?

如果您希望在默认情况下限制导入纹理的大小,除了在导入后编辑最大大小之外,没有其他直接方法。默认值始终为1024

但是,您可以编写一个自定义项,该自定义项(尽管名称不同)还具有一个可用于设置导入设置的方法。一个非常简单的例子如下:

使用UnityEngine;
使用UnityEditor;
公共类DefaultTextureSizer:AssetPostprocessor
{
void纹理()
{
TextureImporter tex_importer=作为TextureImporter的assetImporter;
tex_importer.maxTextureSize=512;//默认值以外的其他一些最大大小
}
}

这将确保所有导入的纹理的最大大小(在本例中)为512。

如果使用
on postprocesseTexture
可以读取每个纹理的实际大小并设置最大大小

当我想要降低低端手机构建的纹理分辨率时,我使用以下代码:

    public void OnPostprocessTexture(Texture2D texture)
    {
        if (!TextureResizingEnabled)
            return;

        TextureImporter textureImporter = assetImporter as TextureImporter;
        TextureImporterFormat format;

        TextureImporterSettings textureImporterSettings = new TextureImporterSettings();
        textureImporter.ReadTextureSettings(textureImporterSettings);

        //grabbing the max texture dimension for use in size calculation
        float size = Mathf.Max(texture.width, texture.height);

        Debug.LogError("original size = " + size + "  orig max size = " + textureImporterSettings.maxTextureSize);

        // Getting the smallest texture size between original texture size, to be resized  by TextureResizingFactor, and maxTextureSize set in asset importer settings:
        size = Mathf.Min(Mathf.Pow(2f, Mathf.Floor(Mathf.Log(size, 2f)) - TextureResizingFactor), textureImporterSettings.maxTextureSize);

        Debug.LogError("chosen   size = " + size);

        // we won't make any changes if the calculate size is lesser than the minimum on Unity dropdown box (32):
        if (size >= 32)
        {
            textureImporterSettings.maxTextureSize = (int)size;
            textureImporter.SetTextureSettings(textureImporterSettings);
        }
    }
它基本上除以最大纹理大小的2,因此如果最大大小等于2048的纹理,它将设置为1024,而同一项目中的512纹理将设置为256。这对于降低内存使用非常有用


在这种情况下使用
onpostprocesseTexture
的缺点是我必须重新导入纹理两次:第一次将更改纹理最大大小,第二次(禁用此代码)将实际应用新的最大大小。这是因为我们在导入纹理后设置了新的大小,但这是唯一的方法,因为在
onprepreprocessTexture

中无法读取纹理大小。我建议为浏览器安装拼写检查插件。还有,已经有标签了。我真不敢相信Unity还没有在整个项目的项目设置中添加一个简单的默认导入设置,很明显,大多数为移动设备开发的人几乎永远不会使用4096纹理,例如,如果他们在某些情况下使用4096纹理,他们可以只指定这几种情况。相反,我们必须等待数小时,才能导入具有巨大纹理的新资源,然后以较小的最大大小重新导入它们,或者我们必须搞乱脚本编写,找出困难的方法。这至少是6年前,第一个线程出现要求这个功能。