【问题标题】:Change Color Lookup texture during runtime在运行时更改颜色查找纹理
【发布时间】:2022-12-14 23:42:28
【问题描述】:

我正在尝试更改 URP 中后处理堆栈的颜色查找模块的纹理和贡献。

最初我尝试像这样简单地修改值:

private void SetTheme(int index)
{
    if (index > 0 && ThemeColorLookups.Length > index)
    {
        if (_globalVolume.profile.TryGet(out ColorLookup cl))
        {
            cl.texture = new TextureParameter(ThemeColorLookups[index], true);
        }
    }
    else
    {
        if (_globalVolume.profile.TryGet(out ColorLookup cl))
        {
            cl.texture = new TextureParameter(null, true);
        }
    }
}

private void SetThemeIntensity(int value)
{
    if (_globalVolume.profile.TryGet(out ColorLookup cl))
    {
        cl.contribution = new ClampedFloatParameter(value / 100f, 0, 1, true);
    }
}

这确实在编辑器中检查音量时更改了值,但是在游戏或场景视图中没有反映任何更改。

我还尝试用一个新实例完全交换 Color Lookup 实例,这或多或少导致了与前一种方法相同的行为。

private int _currentThemeIndex;
private float _currentThemeIntensity;

private void SetTheme(int index)
{
    if (index > 0 && ThemeColorLookups.Length > index)
    {
        _globalVolume.profile.Remove<ColorLookup>();

        var cl = _globalVolume.profile.Add<ColorLookup>();
        cl.contribution = new ClampedFloatParameter(_currentThemeIntensity, 0, 1, true);
        cl.texture = new TextureParameter(ThemeColorLookups[index], true);

        _currentThemeIndex = index;
    }
    else
    {
        _currentThemeIndex = 0;
        _globalVolume.profile.Remove<ColorLookup>();
    }
}

private void SetThemeIntensity(int value)
{
    _currentThemeIntensity = value / 100f;

    if (_currentThemeIndex == 0) { return; }

    _globalVolume.profile.Remove<ColorLookup>();

    var cl = _globalVolume.profile.Add<ColorLookup>();
    cl.contribution = new ClampedFloatParameter(value/100f, 0, 1, true);
    cl.texture = new TextureParameter(ThemeColorLookups[_currentThemeIndex], true);
}

为什么变化没有反映在时间上?如果我在运行时手动修改值,则会显示正确的纹理和贡献,但是对代码执行“相同”只会产生编辑器更改。

值得注意的是,在执行这段代码后,每当您拖动 UI 滑块时都会发生这种情况,即使我尝试通过编辑器手动修改值,也没有任何反应(显然检查器更新除外)。所以在我重放场景之前,它基本上会变砖。此时我可以再次手动修改这些值,但在我的情况下这是不可取的。我想通过代码完全控制 2 个暴露的属性。

Unity 版本 - 2021.2.19f1 使用 URP

【问题讨论】:

    标签: c# unity3d graphics post-processing urp


    【解决方案1】:

    我设法通过直接分配纹理而不是新的纹理参数来解决这个问题,而不是:

    if (volume.profile.TryGet(out ColorLookup cl))
    {
         cl.texture.value = new TextureParameter(texture, true);
    }
    

    试试这个:

    if (volume.profile.TryGet(out ColorLookup cl))
    {
        cl.texture.value = texture;
    }
    

    我认为它不适用于分配新参数的原因是因为系统不响应在那里所做的更改,因为您创建了一个参数的新实例而不是将更改应用于具有(我假设)侦听器的现有参数附加到它。

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2017-12-31
      • 2021-09-18
      • 2020-05-04
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多