加载纹理并更改纹理导入设置。

huangapple go评论58阅读模式
英文:

Load texture at runtime and change texture import settings

问题

以下是代码的中文翻译:

我正在尝试在运行时从StreamingAssets文件夹加载纹理,然后更改它们的导入设置。

由于这将用于修改目的,无法使用资源。

我编写了以下方法,但出于某种原因,纹理导入器始终返回null

private Texture2D LoadTexture(string filePath, bool normal)
{
    Texture2D texture = new Texture2D(2, 2);
    byte[] imageData = System.IO.File.ReadAllBytes(filePath);
    texture.LoadImage(imageData);

    TextureImporter textureImporter = AssetImporter.GetAtPath(filePath) as TextureImporter;

    if (normal)
        textureImporter.textureType = TextureImporterType.NormalMap;
    else
        textureImporter.textureType = TextureImporterType.Default;

    textureImporter.wrapMode = TextureWrapMode.Repeat;
    textureImporter.mipmapEnabled = true;
    AssetDatabase.ImportAsset(filePath);

    return texture;
}

希望这对你有帮助。

英文:

I'm trying to load textures at runtime from StreamingAssets folder and than changing their import settings.

Cant use resources because this will use for modding purposes

I wrote the following method but for some reason the texture importer always returns null.

private Texture2D LoadTexture(string filePath, bool normal)
{
        Texture2D texture = new Texture2D(2, 2);
        byte[] imageData = System.IO.File.ReadAllBytes(filePath);
        texture.LoadImage(imageData);

        TextureImporter textureImporter = AssetImporter.GetAtPath(filePath) as TextureImporter;

        if (normal)
            textureImporter.textureType = TextureImporterType.NormalMap;
        else
            textureImporter.textureType = TextureImporterType.Default;

        textureImporter.wrapMode = TextureWrapMode.Repeat;
        textureImporter.mipmapEnabled = true;
        AssetDatabase.ImportAsset(filePath);

        return texture;
}

答案1

得分: 0

AssetImporter 是一个编辑器类,因此你不能在运行时使用它。要在运行时加载图像,只需选择合适的纹理格式,然后调用 LoadImage。通常,法线贴图使用压缩格式,并处于线性颜色空间。

private Texture2D LoadTexture(string filePath, bool normal)
{
    Texture2D texture;
    
    if (normal)
    {
        texture = new Texture2D(2, 2, TextureFormat.DXT1, mipChain: true, linear: true);
    }
    else
    {
        texture = new Texture2D(2, 2);
    }

    byte[] imageData = System.IO.File.ReadAllBytes(filePath);
    texture.LoadImage(imageData);
    return texture;
}

当然,这不是确定的,这只是一个示例。你可以根据实际的图像填写这些参数。最好将参数更改为 formatlinear 以获得更好的兼容性。

private Texture2D LoadTexture(string filePath, TextureFormat format, bool linear)
{
    Texture2D texture = new Texture2D(2, 2, format, true, linear);
    byte[] imageData = System.IO.File.ReadAllBytes(filePath);
    texture.LoadImage(imageData);
    return texture;
}
英文:

AssetImporter is an editor class so that you cannot use it in runtime. To load an image in runtime you just need to choose a suitable texture format and call LoadImage next. Usually a normal map uses a compressed format and is in linear color space.

private Texture2D LoadTexture(string filePath, bool normal)
{
    Texture2D texture;
    
    if(normal)
    {
        texture = new Texture2D(2, 2, TextureFormat.DXT1, mipChain: true, linear: true);
    }
    else
    {
        texture = new Texture2D(2, 2);
    }

    byte[] imageData = System.IO.File.ReadAllBytes(filePath);
    texture.LoadImage(imageData);
    return texture;
}

Of course, this is not certain, here is just an example. You may fill in these parameters based on your actual image. You'd better change the parameters to format and linear for better compatibility.

private Texture2D LoadTexture(string filePath, TextureFormat format, bool linear)
{
    Texture2D texture = new Texture2D(2, 2, format, true, linear);
    byte[] imageData = System.IO.File.ReadAllBytes(filePath);
    texture.LoadImage(imageData);
    return texture;
}

huangapple
  • 本文由 发表于 2023年4月20日 10:33:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76060109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定