少年修仙传客户端代码仓库
client_Wu Xijin
2019-06-13 033958214c0b16d7e7b93cc821b018c295251867
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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
[XLua.LuaCallCSharp]
public static class TransformExtension
{
 
    [XLua.LuaCallCSharp]
    public static void SetParentEx(this Transform transform, Transform parent, Vector3 localPosition, Quaternion rotation, Vector3 scale)
    {
 
        if (transform != null && parent != null)
        {
            transform.SetParent(parent);
            transform.localPosition = localPosition;
            transform.localRotation = rotation;
            transform.localScale = scale;
        }
    }
 
    [XLua.LuaCallCSharp]
    public static void SetParentEx(this Transform transform, Transform parent, Vector3 localPosition, Vector3 eulerAngles, Vector3 scale)
    {
        if (transform != null && parent != null)
        {
            transform.SetParent(parent);
            transform.localPosition = localPosition;
            transform.localEulerAngles = eulerAngles;
            transform.localScale = scale;
        }
    }
 
    public static T[] GetComponentsInChildren<T>(this Transform transform, bool includeInactive, bool includeSelf) where T : Component
    {
 
        if (includeSelf)
        {
            return transform.GetComponentsInChildren<T>(includeInactive);
        }
        else
        {
            int childCount = transform.childCount;
            List<T> list = new List<T>();
            T t = null;
            for (int i = 0; i < childCount; i++)
            {
                t = transform.GetComponent<T>();
                if (t != null)
                {
                    list.Add(t);
                }
            }
            return list.ToArray();
        }
 
    }
 
    [XLua.LuaCallCSharp]
    public static Transform GetChildTransformDeeply(this Transform transform, string childName, bool includeSelf = false)
    {
 
        if (includeSelf)
        {
            if (transform.name.Equals(childName))
            {
                return transform;
            }
        }
 
        int _count = transform.childCount;
 
        Transform _tempChild = null;
 
        for (int i = 0; i < _count; ++i)
        {
 
            _tempChild = transform.GetChild(i);
 
            if (_tempChild.name.Equals(childName))
            {
                return _tempChild;
            }
 
            _tempChild = transform.GetChild(i).GetChildTransformDeeply(childName, false);
            if (_tempChild)
            {
                return _tempChild;
            }
        }
 
        return null;
    }
 
    /// <summary>
    /// 以锚四个角的方式进行匹配
    /// 并且将对象设置为父对象
    /// </summary>
    /// <param name="_child"></param>
    /// <param name="_parent"></param>
    [XLua.LuaCallCSharp]
    public static void MatchWhith(this RectTransform _child, RectTransform _parent)
    {
 
        if (_child.parent != _parent)
        {
            _child.SetParent(_parent);
        }
        _child.anchoredPosition3D = Vector3.zero;
        _child.sizeDelta = Vector2.zero;
        _child.anchorMin = Vector2.zero;
        _child.anchorMax = Vector2.one;
        _child.pivot = Vector2.one * 0.5f;
        _child.localRotation = Quaternion.identity;
        _child.localScale = Vector3.one;
    }
 
    [XLua.LuaCallCSharp]
    public static bool ContainWorldPosition(this RectTransform _rectTransform, Vector3 _worldPosition)
    {
        var worldCorners = new Vector3[4];
        _rectTransform.GetWorldCorners(worldCorners);
        if (_worldPosition.x >= worldCorners[0].x && _worldPosition.x <= worldCorners[2].x
            && _worldPosition.y >= worldCorners[0].y && _worldPosition.y <= worldCorners[2].y)
        {
            return true;
        }
 
        return false;
    }
 
    [XLua.LuaCallCSharp]
    public static bool RectTransformContain(this RectTransform _rectTransform, RectTransform _target)
    {
        var targetWorldCorners = new Vector3[4];
        _target.GetWorldCorners(targetWorldCorners);
 
        for (int i = 0; i < targetWorldCorners.Length; i++)
        {
            var position = targetWorldCorners[i];
            if (_rectTransform.ContainWorldPosition(position))
            {
                return true;
            }
        }
 
        return false;
    }
 
    [XLua.LuaCallCSharp]
    public static void SetPosition(this Transform transform, float x, float y, float z, bool isLocal = false)
    {
        if (transform == null)
        {
            return;
        }
 
        if (isLocal)
        {
            transform.localPosition = new Vector3(x, y, z);
        }
        else
        {
            transform.position = new Vector3(x, y, z);
        }
    }
 
    public static Transform GetRoot(this Transform transform)
    {
        return transform && transform.parent ? GetRoot(transform.parent) : transform;
    }
 
}