- Home /
Disabling/Enabling a GameObject Before Rendering Scene
Right now I'm trying to take a screenshot within my AR game but I don't want the GameObject to be within the screenshot. The only solution I could think of was to disable and enable the gameobject during the screen capture but Unity's execution flow makes enabling/disabling occur after the scene has already been rendered so this causes the object to visibly disappear during the frame. Is there a way to enable/disable the gameobject prior to rendering?
Answer by Hellium · Jul 16, 2018 at 04:56 PM
Here is how I would have done it (following code not tested)
First script, simply responsible for detecting when OnPreRender
and OnPostRender
are called. This script must be attached to a camera :
public class RenderingCallbacksListener : MonoBehaviour
{
private bool hasCamera = false;
private System.Action onPreRender;
private System.Action onPostRender;
void Start ()
{
if( GetComponent<Camera>() != null )
{
hasCamera = true;
}
else
{
Debug.LogError( "This script must be attached to a camera", gameObject );
}
}
/// <summary>
/// Sets up the pre and post render actions.
/// </summary>
/// <param name="onPreRender">The on pre render.</param>
/// <param name="onPostRender">The on post render.</param>
public void SetupPreAndPostRenderActions( System.Action onPreRender, System.Action onPostRender )
{
if ( !hasCamera )
{
Debug.LogError( "This script must be attached to a camera", gameObject );
return;
}
this.onPreRender = onPreRender;
this.onPostRender = onPostRender;
}
/// <summary>
/// OnPostRender is called before the camera this script is attached to starts rendering the scene
/// </summary>
void OnPreRender()
{
if ( onPreRender != null )
onPreRender();
onPreRender = null;
}
/// <summary>
/// OnPostRender is called after the camera this script is attached to finished rendering the scene
/// </summary>
void OnPostRender()
{
if ( onPostRender != null )
onPostRender();
onPostRender = null;
}
}
Then, in another script, call the SetupPreAndPostRenderActions
function and provide the functions you want to be called before and after the rendering.
public RenderingCallbacksListener RenderingCallbacksListener ; // Drag & drop the gameobject with the script attached, in the inspector
void ExampleFunction()
{
GameObject[] gameObjects = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
RenderingCallbacksListener.SetupPreAndPostRenderActions( () => HideGameObjects( gameObjects ), () => ShowGameObjects( gameObjects ) );
}
private void HideGameObjects( GameObject[] gos )
{
for ( int i = 0 ; i < gos.Length ; i++ )
{
gos[i].SetActive( false );
}
}
private void ShowGameObjects( GameObject[] gos )
{
for ( int i = 0 ; i < gos.Length ; i++ )
{
gos[i].SetActive( true );
}
}
Your answer

Follow this Question
Related Questions
How to do time warp effect on game like popular tiktok effect 1 Answer
How to prevent image from rendering to screen but seen by camera 0 Answers
Rendering screenshots on EC2 linux results in 1x1 pixel images 0 Answers
Cloud recognition in Vuforia 0 Answers
Image on Vuforia looks odd on my phone 0 Answers