using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic; using vnxbqy.UI; using System; public class CameraUtility { static List blurs = new List(); public static void RegisterBlur(Blur _blur) { if (_blur == null) { return; } if (!blurs.Contains(_blur)) { blurs.Add(_blur); _blur.enabled = false; } } public static void UnRegisterBlur(Blur _blur) { if (blurs.Contains(_blur)) { blurs.Remove(_blur); } } public static void AddCullingMask(Camera _camera, int _layer) { _camera.cullingMask |= (1 << _layer); } public static void RemoveCullingMask(Camera _camera, int _layer) { _camera.cullingMask &= ~(1 << _layer); } /// /// 将世界坐标转换为UI的坐标 /// /// /// /// public static Vector3 ConvertToUIPosition(Camera _camera, Vector3 _position) { if (_camera == null || CameraManager.uiCamera == null) { return Vector3.zero; } var temp = _camera.WorldToViewportPoint(_position); var uiposition = CameraManager.uiCamera.ViewportToWorldPoint(temp); uiposition.z = 0; return uiposition; } public static Vector3 ScreenToUIPosition(Vector3 _screenPosition) { if (CameraManager.uiCamera == null) { return Vector3.zero; } var temp = CameraManager.uiCamera.ScreenToViewportPoint(_screenPosition); var uiposition = CameraManager.uiCamera.ViewportToWorldPoint(temp); uiposition.z = 0; return uiposition; } static Stack screenShotCutTexturePool = new Stack(); static Texture2D RequireTexture2D() { if (screenShotCutTexturePool.Count > 0) { return screenShotCutTexturePool.Pop(); } else { return new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); } } static void RecycleTexture2D(Texture2D _texture) { if (_texture != null) { if (Mathf.Abs(_texture.width - ResolutionUtility.currentResolution.x) > 10 || Mathf.Abs(_texture.height - ResolutionUtility.currentResolution.y) > 10) { //Resources.UnloadAsset(_texture); } else { screenShotCutTexturePool.Push(_texture); } } } public static void ScreenShotCut(RawImage _image, Action _callBack, bool _onlySceneCamera = false, bool blur = true) { if (_image == null) { return; } var screenRect = new Rect(0, 0, ResolutionUtility.currentResolution.x - 1, ResolutionUtility.currentResolution.y - 1); SnxxzGame.Instance.StartCoroutine(Co_ScreenShotCut(_image, _callBack, _onlySceneCamera, blur)); } static WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame(); static IEnumerator Co_ScreenShotCut(RawImage _image, Action _callBack, bool _onlySceneCamera = false, bool blur = true) { if (_onlySceneCamera) { CameraManager.uiCamera.enabled = false; } if (blur) { for (int i = 0; i < blurs.Count; i++) { if (_onlySceneCamera && blurs[i].gameObject == CameraManager.uiCamera.gameObject) { continue; } blurs[i].enabled = true; } } _image.color = _image.color.SetA(0); yield return waitForEndOfFrame; var texture2d = RequireTexture2D(); var screenRect = new Rect(0, 0, ResolutionUtility.currentResolution.x - 1, ResolutionUtility.currentResolution.y - 1); texture2d.ReadPixels(screenRect, 0, 0, false); texture2d.Apply(); _image.texture = texture2d; _image.color = _image.color.SetA(1); for (int i = 0; i < blurs.Count; i++) { blurs[i].enabled = false; } CameraManager.uiCamera.enabled = true; if (_callBack != null) { _callBack(); _callBack = null; } } public static void StopShotCut(RawImage _image) { if (_image.texture != null) { RecycleTexture2D((Texture2D)_image.texture); } } public static void ScreenShotCut(Action callBack) { SnxxzGame.Instance.StartCoroutine(Co_DelayScreenShotCut(callBack)); } static IEnumerator Co_DelayScreenShotCut(Action callBack) { yield return waitForEndOfFrame; try { var texture2d = RequireTexture2D(); var screenRect = new Rect(0, 0, ResolutionUtility.currentResolution.x - 1, ResolutionUtility.currentResolution.y - 1); texture2d.ReadPixels(screenRect, 0, 0, false); texture2d.Apply(); callBack(texture2d); } catch (Exception e) { Debug.LogError("截屏发生错误:" + e); } callBack = null; } public static void StopShotCut(Texture2D tex) { RecycleTexture2D(tex); } }