public static UITextureBlurTool GetInstance() { if (m_Instance == null) { m_Instance = new UITextureBlurTool(); } return(m_Instance); }
/// <summary> /// 背景模糊; /// </summary> public void F_BlurScreenShot() { if (null != m_BlurTexture) { m_BlurTexture.gameObject.SetActive(false); } if (UICamera != null) { float scale = 0.4f; //比例越小,速度越快,但越模糊 Vector2 v2Screen = new Vector2(Screen.width, Screen.height); float UIScreenWidth = 1280; float UIScreenHeight = UIScreenWidth * v2Screen.y / v2Screen.x; v2Screen.x = UIScreenWidth; v2Screen.y = UIScreenHeight; float width = v2Screen.x * scale; float height = v2Screen.y * scale; RenderTexture rt = new RenderTexture((int)width, (int)height, 24); if (null == SceneCamera) { if (SceneCameraObj == null) { SceneCameraObj = GameObject.Find("WorldCamera"); } if (null != SceneCameraObj) { SceneCamera = SceneCameraObj.GetComponent <Camera>(); } } if (null != SceneCamera) { SceneCamera.targetTexture = rt; SceneCamera.Render(); SceneCamera.targetTexture = null; } UICamera.targetTexture = rt; UICamera.Render(); UICamera.targetTexture = null; RenderTexture.active = rt; Texture2D thumb2d = new Texture2D((int)width, (int)height, TextureFormat.RGB24, false); thumb2d.ReadPixels(new Rect(0, 0, width, height), 0, 0); //Color32[] thumbPixels = thumb2d.GetPixels32(); //thumb2d.SetPixels32(thumbPixels); thumb2d.Apply(); if (m_BlurTexture == null) { GameObject go = new GameObject("ScreenBG"); go.layer = LayerMask.NameToLayer("UI"); go.transform.parent = m_WinListGo.transform; go.transform.localPosition = Vector3.zero; go.transform.localScale = Vector3.one; m_BlurTexture = go.AddComponent <RawImage>(); m_BlurTexture.color = new Color32(84, 84, 84, 255); Canvas canvas = TransformUtil.Add <Canvas>(go); canvas.overrideSorting = true; TransformUtil.Add <GraphicRaycaster>(go); m_BlurTexture.gameObject.SetActive(false); } //处理图片; m_BlurTexture.texture = UITextureBlurTool.GetInstance().F_HandleImage(thumb2d); m_BlurTexture.gameObject.SetActive(true); //Color color = m_BlurTexture.color; //color.a = 0; //m_BlurTexture.color = color; GameObject.Destroy(thumb2d); } else { Loger.Error("CatureScreenShot Fail, UICamera is NULL!"); } #endregion }