/// <summary> /// Prepares an instantiated GameObject for taking a snapshot by setting its layers. /// </summary> /// <param name="prefab">The instantiated GameObject to prepare.</param> /// <returns>A GameObjectStateSnapshot containing the state of the gameObject prior to modifying its layers.</returns> private GameObjectStateSnapshot PrepareObject(GameObject gameObject) { GameObjectStateSnapshot goss = new GameObjectStateSnapshot(gameObject); SetLayersRecursively(gameObject); return(goss); }
/// <summary> /// Prepares an instantiated GameObject for taking a snapshot by setting its layers and applying the specified position offset, rotation, and scale to it. /// </summary> /// <param name="prefab">The instantiated GameObject to prepare.</param> /// <param name="positionOffset">The position offset relative to the SnapshotCamera to apply to the gameObject.</param> /// <param name="rotation">The rotation to apply to the gameObject.</param> /// <param name="scale">The scale to apply to the gameObject.</param> /// <returns>A GameObjectStateSnapshot containing the state of the gameObject prior to modifying its layers, position, rotation, and scale.</returns> private GameObjectStateSnapshot PrepareObject(GameObject gameObject, Vector3 positionOffset, Quaternion rotation, Vector3 scale) { GameObjectStateSnapshot goss = new GameObjectStateSnapshot(gameObject); gameObject.transform.position = transform.position + positionOffset; gameObject.transform.rotation = rotation; gameObject.transform.localScale = scale; SetLayersRecursively(gameObject); return(goss); }
/// <summary> /// Takes a snapshot of an instantiated GameObject and returns it as a Texture2D. /// </summary> /// <param name="gameObject">The instantiated GameObject to snapshot.</param> /// <param name="backgroundColor">The background color of the snapshot. Can be transparent.</param> /// <param name="positionOffset">The position offset relative to the SnapshotCamera that will be applied to the gameObject while taking the snapshot. Its position will be restored after taking the snapshot.</param> /// <param name="rotation">The rotation that will be applied to the gameObject while taking the snapshot. Its rotation will be restored after taking the snapshot.</param> /// <param name="scale">The scale that will be applied to the gameObject while taking the snapshot. Its scale will be restored after taking the snapshot.</param> /// <param name="width">The width of the snapshot image.</param> /// <param name="height">The height of the snapshot image.</param> /// <returns>A Texture2D containing the captured snapshot.</returns> public Texture2D TakeObjectSnapshot(GameObject gameObject, Color backgroundColor, Vector3 positionOffset, Quaternion rotation, Vector3 scale, int width = 128, int height = 128) { if (gameObject == null) { throw new ArgumentNullException("gameObject"); } else if (gameObject.scene.name == null) { throw new ArgumentException("gameObject parameter must be an instantiated GameObject! If you want to use a prefab directly, use TakePrefabSnapshot instead.", "gameObject"); } // Prepare the gameObject and save its current state so we can restore it later GameObjectStateSnapshot previousState = PrepareObject(gameObject, positionOffset, rotation, scale); // Take a snapshot Texture2D snapshot = TakeSnapshot(backgroundColor, width, height); // Restore the gameObject to its previous state previousState.Restore(); // Return the snapshot return(snapshot); }