Hi I am making an iOS and android game where when you pause the game it blurs everything apart from the ui, I have been searching everywhere but I do not know how to blur the scene, I also only need it to blur once not every frame as that would be very taxing. Is there a way to make a ui image blur what is behind it( I am using c#) the help would be much appreciated, thanks.
So this far I’ve made it this way: the main camera has Blur effect from Standard Assets attached to it, which is switched off.
This script makes the work, but I’m not sure about it’s efficiency. Hope it will help you to start looking in the right direction.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CreateBlurBackground : MonoBehaviour
{
[SerializeField]
private Image background;
private Texture2D CreateTexture()
{
RenderTexture renderTexture = new RenderTexture(Screen.width, Screen.height, 16);
RenderTexture.active = renderTexture;
Camera.main.targetTexture = renderTexture;
UnityStandardAssets.ImageEffects.Blur blur = Camera.main.gameObject.GetComponent<UnityStandardAssets.ImageEffects.Blur>();
if(blur != null)
{
blur.enabled = true;
Camera.main.Render();
blur.enabled = false;
}
else
Camera.main.Render();
Camera.main.targetTexture = null;
Texture2D texture2D = new Texture2D(Screen.width, Screen.height);
texture2D.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture2D.Apply();
RenderTexture.active = null;
return texture2D;
}
public void Pause()
{
background.gameObject.SetActive(true);
Texture2D texture = CreateTexture();
background.canvasRenderer.SetTexture(texture);
}
public void Resume()
{
background.gameObject.SetActive(false);
}
}
Feel free to ask in comments below this answer if you have any questions.