OnWillRenderObject()
Called for each camera if the object is visible and not a UI element.
Read time 1 minuteLast updated 2 days ago
Definition
- Type: Method
- Namespace: UnityEngine
public void OnWillRenderObject()
Remarks
OnWillRenderObjectOnWillRenderObjectOnWillRenderObject- Per-camera material or shader adjustments, for example setting shader keywords or per-camera uniforms.
- Per-camera logic that doesn't affect the object's transform or visibility.
- Custom rendering logic, like setting up camera-dependent effects.
OnWillRenderObject- Modifying the object's position or transform. This is because the culling process (which decides if an object is visible) has already occurred. If you move the object here, it may already be included in the render list for this frame, and the scene may not update as expected. You could see visual artifacts, popping, or inconsistent rendering results.
- Changing the camera. You can read from Camera.current, but changing camera settings here is generally not recommended. If you need to change camera settings before culling, use MonoBehaviour.OnPreCull() or MonoBehaviour.OnPreRender() instead.
Camera.current will be set to the camera that will render the object.
Note: has no effect when called from a UI element.
OnWillRenderObjectAdditional Resources: MonoBehaviour.OnPreCull() or MonoBehaviour.OnPreRender()
Examples
using UnityEngine;using System.Collections;public class ExampleScript : MonoBehaviour{ public Renderer rend; private float timePass = 0.0f; void Start() { rend = GetComponent<Renderer>(); } void OnWillRenderObject() { timePass += Time.deltaTime; if (timePass > 1.0f) { timePass = 0.0f; print(gameObject.name + " is being rendered by " + Camera.current.name + " at " + Time.time); } }}