事件函数,Unity 在相机渲染场景后调用。
在内置渲染管线中,Unity 会在相机渲染场景后立即对附加到与启用 Camera 组件相同的 GameObjects 上的 MonoBehaviour 调用 OnPostRender
。使用 OnPostRender
在渲染循环的这一点执行您自己的代码;例如,如果您在 MonoBehaviour.OnPreRender 中更改了视觉效果,您可以在此处将其更改回来。 OnPostRender
可以是协程。
对于不需要脚本与 Camera 组件位于同一 GameObject 上的类似功能,请参阅 Camera.onPostRender。对于可脚本化渲染管线中的类似功能,请参阅 RenderPipelineManager。
要在 Unity 渲染所有相机和 GUI 后执行代码,请使用 WaitForEndOfFrame 或 CommandBuffer。
using UnityEngine;
// A script that when attached to the camera, makes the resulting // colors inverted. See its effect in play mode. public class ExampleClass : MonoBehaviour { private Material mat;
// Will be called from camera after regular rendering is done. public void OnPostRender() { if (!mat) { // Unity has a built-in shader that is useful for drawing // simple colored things. In this case, we just want to use // a blend mode that inverts destination colors. var shader = Shader.Find("Hidden/Internal-Colored"); mat = new Material(shader); mat.hideFlags = HideFlags.HideAndDontSave; // Set blend mode to invert destination colors. mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusDstColor); mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); // Turn off backface culling, depth writes, depth test. mat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off); mat.SetInt("_ZWrite", 0); mat.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always); }
GL.PushMatrix(); GL.LoadOrtho();
// activate the first shader pass (in this case we know it is the only pass) mat.SetPass(0); // draw a quad over whole screen GL.Begin(GL.QUADS); GL.Vertex3(0, 0, 0); GL.Vertex3(1, 0, 0); GL.Vertex3(1, 1, 0); GL.Vertex3(0, 1, 0); GL.End();
GL.PopMatrix(); } }