少年修仙传客户端代码仓库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using Snxxz.UI;
using System;
 
public class CameraUtility
{
 
    static List<Blur> blurs = new List<Blur>();
 
    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);
    }
 
    /// <summary>
    /// 将世界坐标转换为UI的坐标
    /// </summary>
    /// <param name="_camera"></param>
    /// <param name="_position"></param>
    /// <returns></returns>
    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<Texture2D> screenShotCutTexturePool = new Stack<Texture2D>();
 
    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<Texture2D> callBack)
    {
        SnxxzGame.Instance.StartCoroutine(Co_DelayScreenShotCut(callBack));
    }
 
    static IEnumerator Co_DelayScreenShotCut(Action<Texture2D> callBack)
    {
        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();
        callBack(texture2d);
        callBack = null;
    }
 
    public static void StopShotCut(Texture2D tex)
    {
        RecycleTexture2D(tex);
    }
 
}