由于在urp下,打包后传统的相机事件有些无法正确执行,这时候我们需要在urp管线中的特定时机进行处理一些事件,需要创建继承ScriptableRenderPass和ScriptableRendererFeature的脚本,示例如下:
PluginEventPass:
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class PluginEventPass : ScriptableRenderPass
{
private readonly System.IntPtr _pluginCallback;
private readonly int _eventID;
public PluginEventPass(System.IntPtr callback, int eventID)
{
_pluginCallback = callback;
_eventID = eventID;
renderPassEvent = RenderPassEvent.AfterRenderingSkybox; // 插件事件时机
}
//这里就是你要执行的事件
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
#if UNITY_EDITOR
if (!Application.isPlaying)
{
return;
}
#endif
GL.IssuePluginEvent(_pluginCallback, _eventID);
}
}
PluginEventFeature:
using UnityEngine;
using UnityEngine.Rendering.Universal;
public class PluginEventFeature : ScriptableRendererFeature
{
[SerializeField] int eventID = 1;
private PluginEventPass _pluginPass;
public override void Create()
{
var ptr = DllManager.GetRenderEventFunc();//这里是你的具体事件
_pluginPass = new PluginEventPass(ptr, eventID);
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(_pluginPass);
}
}
然后需要在asset目录下的Settings目录下找到,项目用的管线设置文件例如:
点击add,增加我们上面的设置。这里需要注意,要与Project Settings下的打包设置一致,不然打包后不生效: