hch
2025-06-19 35f08360f1e07c7a8301f7b2031c0779e2998fb5
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
 
 
public static class UIUtility
{
 
    public static GameObject CreateWidget(string _sourceName, string _name)
    {
        var prefab = ResManager.Instance.LoadAsset<GameObject>("UIComp", _sourceName);
        if (prefab == null)
        {
            return null;
        }
 
        var instance = GameObject.Instantiate(prefab);
        instance.name = string.IsNullOrEmpty(_name) ? _sourceName : _name;
        return instance;
    }
 
    public static T GetComponent<T>(this Transform transform, string path) where T : Component
    {
        if (transform == null || string.IsNullOrEmpty(path))
        {
            return null;
        }
 
        var _child = transform.Find(path);
        if (_child == null)
        {
            return null;
        }
        else
        {
            return _child.GetComponent<T>();
        }
    }
 
    public static T GetComponent<T>(this Component component, string path) where T : Component
    {
        if (component == null || string.IsNullOrEmpty(path))
        {
            return null;
        }
 
        var _child = component.transform.Find(path);
        if (_child == null)
        {
            return null;
        }
        else
        {
            return _child.GetComponent<T>();
        }
    }
 
    public static T GetComponent<T>(this GameObject gameObject, string path) where T : Component
    {
        if (gameObject == null || string.IsNullOrEmpty(path))
        {
            return null;
        }
 
        var _child = gameObject.transform.Find(path);
        if (_child == null)
        {
            return null;
        }
        else
        {
            return _child.GetComponent<T>();
        }
    }
 
    public static Vector2 GetMaxWorldPosition(this RectTransform _rectTransform)
    {
        Vector3[] cornors = new Vector3[4];
        _rectTransform.GetWorldCorners(cornors);
        return cornors[2];
    }
 
    public static Vector2 GetMinWorldPosition(this RectTransform _rectTransform)
    {
        Vector3[] cornors = new Vector3[4];
        _rectTransform.GetWorldCorners(cornors);
        return cornors[0];
    }
 
    public static Vector2 GetMaxReferencePosition(this RectTransform _rectTransform, Transform _reference)
    {
        Vector2 max;
        var offsetY = (1 - _rectTransform.pivot.y) * _rectTransform.rect.height;
        var offsetX = (1 - _rectTransform.pivot.x) * _rectTransform.rect.width;
        max = _rectTransform.TransformPoint(offsetX, offsetY, 0);
        max = _reference.InverseTransformVector(max);
        return max;
    }
 
    public static Vector2 GetMinReferencePosition(this RectTransform _rectTransform, Transform _reference)
    {
        Vector2 min;
        var offsetY = -_rectTransform.pivot.y * _rectTransform.rect.height;
        var offsetX = -_rectTransform.pivot.x * _rectTransform.rect.width;
        min = _rectTransform.TransformPoint(offsetX, offsetY, 0);
        min = _reference.InverseTransformVector(min);
 
        return min;
    }
 
    public static int RayCrossingCount(Vector2 p, List<Vector3> vertices)
    {
        var crossNum = 0;
        for (int i = 0, count = vertices.Count; i < count; i++)
        {
            var v1 = vertices[i];
            var v2 = vertices[(i + 1) % count];
 
            if (((v1.y <= p.y) && (v2.y > p.y))
                || ((v1.y > p.y) && (v2.y <= p.y)))
            {
                if (p.x < v1.x + (p.y - v1.y) / (v2.y - v1.y) * (v2.x - v1.x))
                {
                    crossNum += 1;
                }
            }
        }
 
        return crossNum;
    }
 
    public static UIVertex PackageUIVertex(Vector3 _position, Vector2 _uv0, Color _color)
    {
        var vertex = new UIVertex();
        vertex.position = _position;
        vertex.uv0 = _uv0;
        vertex.color = _color;
 
        return vertex;
    }
 
    public static UIVertex PackageUIVertexUV1(Vector3 _position, Vector2 _uv1, Color _color)
    {
        var vertex = new UIVertex();
        vertex.position = _position;
        vertex.uv1 = _uv1;
        vertex.color = _color;
 
        return vertex;
    }
 
    public static Vector3 ClampWorldPosition(RectTransform _area, PointerEventData _data)
    {
        if (_area == null || _data == null)
        {
            return Vector3.zero;
        }
        else
        {
            var worldMousePos = Vector3.zero;
            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(_area, _data.position, _data.pressEventCamera, out worldMousePos))
            {
                return worldMousePos;
            }
            else
            {
                return _data.pointerCurrentRaycast.worldPosition;
            }
        }
    }
 
    public static bool RectTransformContain(RectTransform _area, RectTransform _test)
    {
        if (_area == null || _test == null)
        {
            return false;
        }
 
        var worldcornersA = new Vector3[4];
        _area.GetWorldCorners(worldcornersA);
        var worldcornersB = new Vector3[4];
        _test.GetWorldCorners(worldcornersB);
 
        var a = new Quadrangle(worldcornersA[0].x, worldcornersA[0].y, worldcornersA[2].x, worldcornersA[2].y);
        var b = new Quadrangle(worldcornersB[0].x, worldcornersB[0].y, worldcornersB[2].x, worldcornersB[2].y);
 
        if (a.minX > b.maxX || a.maxX < b.minX || a.minY > b.maxY || a.maxY < b.minY)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
 
    public static bool IsPointerOverGameObject()
    {
        if (EventSystem.current == null)
        {
            return false;
        }
 
        var eventData = new PointerEventData(EventSystem.current);
        eventData.pressPosition = Input.mousePosition;
        eventData.position = Input.mousePosition;
 
        var list = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventData, list);
 
        return list.Count > 0;
    }
 
    struct Quadrangle
    {
        public float minX;
        public float minY;
        public float maxX;
        public float maxY;
 
        public Quadrangle(float _minX, float _minY, float _maxX, float _maxY)
        {
            this.minX = _minX;
            this.minY = _minY;
            this.maxX = _maxX;
            this.maxY = _maxY;
        }
    }
 
 
    public static Vector2 GetEdge(List<UIVertex> _vertexs, int _axis)
    {
 
        if (_vertexs == null || _vertexs.Count == 0)
        {
            Debug.Log("非法顶点数据");
            return Vector2.zero;
        }
 
        if (_axis != 0 && _axis != 1)
        {
            Debug.Log("非法轴");
            return Vector2.zero;
        }
 
        var count = _vertexs.Count;
        var min = _vertexs[0].position[_axis];
        var max = _vertexs[0].position[_axis];
 
        for (int i = 1; i < count; i++)
        {
            if (_vertexs[i].position[_axis] < min)
            {
                min = _vertexs[i].position[_axis];
            }
            else if (_vertexs[i].position[_axis] > max)
            {
                max = _vertexs[i].position[_axis];
            }
        }
 
        return new Vector2(min, max);
    }
 
    public static void AddQuad(VertexHelper vertexHelper, Vector3[] quadPositions, Color32 color, Vector2[] quadUVs)
    {
        if (quadPositions == null || quadPositions.Length < 4)
        {
            Debug.Log("非法顶点数组");
            return;
        }
 
        if (quadUVs == null || quadUVs.Length < 4)
        {
            Debug.Log("非法UV数组");
            return;
        }
 
        var currentVertCount = vertexHelper.currentVertCount;
        for (var i = 0; i < 4; i++)
        {
            vertexHelper.AddVert(quadPositions[i], color, quadUVs[i]);
        }
        vertexHelper.AddTriangle(currentVertCount, currentVertCount + 1, currentVertCount + 2);
        vertexHelper.AddTriangle(currentVertCount + 2, currentVertCount + 3, currentVertCount);
    }
 
    public static void AddTriangle(VertexHelper vertexHelper, Vector3[] positions, Color32 color, Vector2[] uvs)
    {
        if (positions == null || positions.Length < 3)
        {
            Debug.Log("非法顶点数组");
            return;
        }
 
        if (uvs == null || uvs.Length < 3)
        {
            Debug.Log("非法UV数组");
            return;
        }
 
        var currentVertCount = vertexHelper.currentVertCount;
        for (var i = 0; i < 3; i++)
        {
            vertexHelper.AddVert(positions[i], color, uvs[i]);
        }
        vertexHelper.AddTriangle(currentVertCount, currentVertCount + 1, currentVertCount + 2);
    }
 
    // public static string GetUIElementRelativePath(UIRoot _root, Transform _transform)
    // {
    //     List<Transform> parents = new List<Transform>() { _transform };
    //     GetParents(_transform, ref parents);
 
    //     if (parents.Contains(_root.transform))
    //     {
    //         parents.Remove(_root.transform);
    //     }
 
    //     var names = new string[parents.Count];
    //     for (int i = 0; i < names.Length; i++)
    //     {
    //         names[i] = parents[i].gameObject.name;
    //     }
 
    //     return string.Join("/", names);
    // }
 
    public static void GetParents(Transform _transform, ref List<Transform> _parents)
    {
        if (_transform == null || _parents == null)
        {
            return;
        }
 
        if (_transform.parent != null)
        {
            _parents.Insert(0, _transform.parent);
 
            GetParents(_transform.parent, ref _parents);
        }
    }
 
}