少年修仙传客户端代码仓库
leonard Wu
2018-08-02 a38909a99dd4108c110611a3575122fe77915a99
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
namespace Snxxz.UI
{
    [ExecuteInEditMode]
    public class AdaptiveLayout : MonoBehaviour
    {
        [SerializeField] HorizonOrVertical m_HorizonOrVerticle;
        [SerializeField] CanvasGroup m_CanvasGroup;
        [SerializeField] List<AdaptiveElement> m_AdapElements;
        [SerializeField] bool m_AutoIncludeChild=false;
        [NonSerialized] RectTransform m_Rect;
        public RectTransform rectTransform
        {
            get
            {
                if (m_Rect == null)
                {
                    m_Rect = GetComponent<RectTransform>();
                }
                return m_Rect;
            }
        }
        public float Size
        {
            get
            {
                return m_HorizonOrVerticle == HorizonOrVertical.Horizon ? rectTransform.sizeDelta.x : rectTransform.sizeDelta.y;
            }
        }
 
        private void OnEnable()
        {
            if (m_AdapElements == null)
            {
                enabled = false;
                return;
            }
            if (m_CanvasGroup != null)
            {
                m_CanvasGroup.alpha = 0;
                m_CanvasGroup.interactable = true;
                StartCoroutine(Co_DelayShow());
            }
        }
 
        private void TrackerRect(RectTransform _rect)
        {
            var _pivot = _rect.pivot;
            var _anchorMax = _rect.anchorMax;
            var _anchorMin = _rect.anchorMin;
            if (m_HorizonOrVerticle == HorizonOrVertical.Horizon)
            {
                _pivot.x = 0; _pivot.y = 0.5f;
                _anchorMax.x = 0; _anchorMax.y = 0.5f;
                _anchorMin.x = 0; _anchorMin.y = 0.5f;
            }
            else
            {
                _pivot.x = 0.5f; _pivot.y = 1f;
                _anchorMax.x = 0.5f; _anchorMax.y = 1;
                _anchorMin.x = 0.5f; _anchorMin.y = 1;
            }
            if (_rect.pivot != _pivot || _rect.anchorMax != _anchorMax || _rect.anchorMin != _anchorMin)
            {
                _rect.pivot = _pivot;
                _rect.anchorMax = _anchorMax;
                _rect.anchorMin = _anchorMin;
            }
        }
 
        private void CalculateLayout()
        {
            var _offset = 0.0f;
            var _horizon = m_HorizonOrVerticle == HorizonOrVertical.Horizon;
            for (int i = 0; i < m_AdapElements.Count; i++)
            {
                if (m_AdapElements[i].rect == null || !m_AdapElements[i].rect.gameObject.activeInHierarchy)
                {
                    continue;
                }
                _offset += _horizon ? m_AdapElements[i].padding.left : m_AdapElements[i].padding.top;
                var _pos = m_AdapElements[i].rect.localPosition;
                if (_horizon)
                {
                    _pos.x = _offset;
                }
                else
                {
                    _pos.y = -_offset;
                }
                if (m_AdapElements[i].rect.localPosition != _pos)
                {
                    m_AdapElements[i].rect.localPosition = _pos;
                }
                _offset += _horizon ? m_AdapElements[i].rect.sizeDelta.x : m_AdapElements[i].rect.sizeDelta.y;
                _offset += _horizon ? m_AdapElements[i].padding.right : m_AdapElements[i].padding.bottom;
            }
            var _sizeDelta = rectTransform.sizeDelta;
            if (_horizon)
            {
                _sizeDelta.x = _offset;
            }
            else
            {
                _sizeDelta.y = _offset;
            }
            if (rectTransform.sizeDelta != _sizeDelta)
            {
                rectTransform.sizeDelta = _sizeDelta;
            }
        }
 
        private void LateUpdate()
        {
            if (m_AdapElements == null)
            {
                return;
            }
            TrackerRect(rectTransform);
            for (int i = 0; i < m_AdapElements.Count; i++)
            {
                if (m_AdapElements[i].rect == null)
                {
                    continue;
                }
                TrackerRect(m_AdapElements[i].rect);
            }
            CalculateLayout();
        }
 
        private void OnTransformChildrenChanged()
        {
            if (m_AutoIncludeChild)
            {
                AutoIncludeChild();
            }
        }
 
        private void AutoIncludeChild()
        {
            var _index = 0;
            foreach (Transform _child in transform)
            {
                if (m_AdapElements == null)
                {
                    m_AdapElements = new List<AdaptiveElement>();
                }
                var _rect = m_AdapElements.Find((x) =>
                 {
                     return x.rect == _child;
                 });
                var _rectOffset= _child.GetComponent<AdaptiveRectOffset>();
                if (_rect == null)
                {
                    m_AdapElements.Insert(_index, new AdaptiveElement(_child as RectTransform, _rectOffset));
                }
                _index++;
            }
            if (m_AdapElements != null)
            {
                m_AdapElements.RemoveAll((x) =>
                {
                    return x.rect == null;
                });
            }
            LateUpdate();
        }
 
        IEnumerator Co_DelayShow()
        {
            yield return WaitingForSecondConst.WaitMS100;
            if (m_CanvasGroup != null)
            {
                m_CanvasGroup.alpha = 1;
            }
        }
 
        public enum HorizonOrVertical
        {
            Horizon,
            Vertical,
        }
        [Serializable]
        public class AdaptiveElement
        {
            [SerializeField] RectTransform m_Rect;
            public RectTransform rect
            {
                get
                {
                    return m_Rect;
                }
            }
            [SerializeField] RectOffset m_Padding;
            public RectOffset padding
            {
                get
                {
                    return m_Padding;
                }
            }
 
            public AdaptiveElement(RectTransform _rect, AdaptiveRectOffset _offset)
            {
                m_Rect = _rect;
                m_Padding = _offset == null ? new RectOffset() : _offset.rectOffset;
            }
        }
 
    }
}