[英]Change Color Lookup texture during runtime
我正在嘗試更改 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
盡管Merijn Kersten的回答有效,但有一種更好的方法可以在運行時更改紋理。
在您提供的代碼中,您試圖修改_globalVolume
對象上的ColorLookup
組件,但在運行時修改對象上的值不會自動更新場景中的對象。
您可以在修改其值后對_globalVolume
對象使用SetDirty
方法。 這將告訴 Unity 保存更改並更新場景中的對象。
{
if (index > 0 && ThemeColorLookups.Length > index)
{
if (_globalVolume.profile.TryGet(out ColorLookup cl))
{
cl.texture = new TextureParameter(ThemeColorLookups[index], true);
_globalVolume.SetDirty();
}
}
else
{
if (_globalVolume.profile.TryGet(out ColorLookup cl))
{
cl.texture = new TextureParameter(null, true);
_globalVolume.SetDirty();
}
}
}
private void SetThemeIntensity(int value)
{
if (_globalVolume.profile.TryGet(out ColorLookup cl))
{
cl.contribution = new ClampedFloatParameter(value / 100f, 0, 1, true);
_globalVolume.SetDirty();
}
}
Merijn Kersten的解決方案之所以有效,是因為他直接將Texture
對象分配給texture.value
屬性,這意味着他正在修改屬於ColorLookup
組件的現有TextureParameter
對象。 它將由 Unity 自動更新,因為該對象存在於場景中。
我設法通過直接分配紋理而不是新的紋理參數來解決這個問題,而不是:
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;
}
它不能分配新參數的原因是系統不會響應對未綁定到現有組件的對象所做的更改,這意味着您通過創建新實例所做的任何更改都不會反映在現場。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.