当前位置:网站首页>Adding rendererdata of URP with script

Adding rendererdata of URP with script

2022-06-10 18:06:00 Edward. W

I have encountered some strange problems recently .

stay Unity In the rendering pipeline of , Need to give URP Of RenderPipeline Asset Add a little more RenderererData, This is also custom Render Feature And the way of rendering . The specific implementation method is also very simple , Get ready RendererData after , Just... Again Unity Of UI Click the plus sign on the . As shown in the figure below :

But in fact, there are always such needs . This time, you need to dynamically add this... Through scripts Renderer List. We go through URP You can see , This variable is not accessible , There is no corresponding method .

It makes sense that such an interface should be provided by URP Expose yourself , But it's not . But since it is a serializable variable , Probability can be operated in a legal way , But I'm not going to let you visit , So we can only add it by reflection . 

Go straight to the code :

var proInfo = typeof(UniversalRenderPipelineAsset).GetField("m_RendererDataList",
                    BindingFlags.NonPublic | BindingFlags.Instance);

if (proInfo != null)
{
     rendererDataList = (ScriptableRendererData[])proInfo.GetValue(UniversalRenderPipeline.asset);
                
      var newList = new ScriptableRendererData[rendererDataList.Length+1];
      for (int i = 0; i < rendererDataList.Length; i++)
      {
            
            newList[i] = rendererDataList[i];
            newList[rendererDataList.Length] = newRendererData;
      }
      proInfo.SetValue(GraphicsSettings.currentRenderPipeline, newList);
}

First, get the corresponding through reflection Field, Then force the conversion to the corresponding rendererDataList, At this point, you can get the whole list .

But there's a problem , If we directly modify List, In fact, it will not take effect , Because the rendererDataList Just one of our variables , The location pointed to is the real list , If we go straight :

rendererDataList = new ScriptableRendererData[10];

In this case, it is equivalent to a new new A variable , It can't be changed PipeLine Asset The list inside . therefore , Here you also need to use SetValue Method to put the new value back into effect .

therefore , The method in the code is new One ScriptableRendererData Array , First, insert the contents of the obtained list , And then put the customized RendererData To the end . Reuse SetValue Method to update the corresponding asset. 

Measured at Editor and Runtime Can take effect .

原网站

版权声明
本文为[Edward. W]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101724385965.html