少年修仙传客户端代码仓库
lcy
2024-12-16 a39c35fc6449430cd02bccb681c4a0a880e46cd9
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class UIParamCopyTool : MonoBehaviour
{
    private List<UIObject> copyObjects = new List<UIObject>();
    public List<UITarget> copyTargets = new List<UITarget>();
    public void Add()
    {
        var _obejct = new UIObject();
        _obejct.type = UIType.None;
        copyObjects.Add(_obejct);
    }
 
    public UIObject this[int _index]
    {
        get
        {
            if (_index < copyObjects.Count && _index >= 0)
            {
                return copyObjects[_index];
            }
            return null;
        }
    }
 
    public void Remove(int _index)
    {
        var _object = copyObjects[_index];
        copyObjects.RemoveAt(_index);
        _object = null;
    }
 
    public int Count
    {
        get
        {
            return copyObjects.Count;
        }
    }
 
    public void FindTarget(string _parentName)
    {
        copyTargets.Clear();
        FindTarget(transform, _parentName);
    }
 
    public void Paste()
    {
        foreach (var _object in copyObjects)
        {
            foreach (var _target in copyTargets)
            {
                _target.Paste(_object);
            }
        }
    }
 
    public void FindTarget(Transform _parent, string _parentName)
    {
        if(_parent == null|| _parent.childCount == 0)
        {
            return;
        }
        foreach (Transform _tran in _parent)
        {
            if (_tran.name == _parentName)
            {
                var _target = new UITarget()
                {
                    Object = _tran.gameObject,
                    ok = true,
                };
                _target.Init();
                copyTargets.Add(_target);
                continue;
            }
            FindTarget(_tran, _parentName);
        }
    }
 
    public class UIObject
    {
        public Graphic Object;
        public UIType type;
        public string name = string.Empty;
    }
 
    public class UITarget
    {
        public GameObject Object;
        public bool ok = true;
 
        public List<Image> images = new List<Image>();
        public List<Text> texts = new List<Text>();
 
        public void Init()
        {
            Object.GetComponentsInChildren<Image>(true, images);
            Object.GetComponentsInChildren<Text>(true, texts);
        }
 
        public void Paste(UIObject _object)
        {
            switch (_object.type)
            {
                case UIType.None:
                    break;
                case UIType.Image:
                    PasteImage(_object);
                    break;
                case UIType.Text:
                    PasteText(_object);
                    break;
            }
        }
 
        public void PasteRectTransform(RectTransform _target, RectTransform _paste)
        {
            _target.pivot = _paste.pivot;
            _target.anchorMax = _paste.anchorMax;
            _target.anchorMin = _paste.anchorMin;
            _target.sizeDelta = _paste.sizeDelta;
            _target.localPosition = _paste.localPosition;
        }
 
        public void PasteImage(UIObject _object)
        {
            var _imageArray = images.FindAll((x) =>
            {
                return x.name == _object.name;
            });
            if (_imageArray == null)
            {
                return;
            }
            for (int i = 0; i < _imageArray.Count; i++)
            {
                var _image = _imageArray[i];
                var _paste = _object.Object as Image;
                _image.sprite = _paste.sprite;
                _image.color = _paste.color;
                _image.raycastTarget = _paste.raycastTarget;
                PasteRectTransform(_image.rectTransform, _paste.rectTransform);
            }
        }
 
        public void PasteText(UIObject _object)
        {
            var _textArray = texts.FindAll((x) =>
            {
                return x.name == _object.name;
            });
            if (_textArray == null)
            {
                return;
            }
            for (int i = 0; i < _textArray.Count; i++)
            {
                var _text = _textArray[i];
                var _paste = _object.Object as Text;
                _text.color = _paste.color;
                _text.fontSize = _paste.fontSize;
                _text.font = _paste.font;
                _text.raycastTarget = _paste.raycastTarget;
                _text.alignment = _paste.alignment;
                _text.horizontalOverflow = _paste.horizontalOverflow;
                _text.verticalOverflow = _paste.verticalOverflow;
                PasteRectTransform(_text.rectTransform, _paste.rectTransform);
            }
            
        }
    }
 
    public enum UIType
    {
        None,
        Image,
        Text,
    }
}
 
#if UNITY_EDITOR
[CustomEditor(typeof(UIParamCopyTool))]
public class UIParamCopyToolEditor : Editor
{
    private Vector2 scrollposition;
    private Vector2 scrollposition1;
    private string targetName = string.Empty;
 
    public override void OnInspectorGUI()
    {
        var _target = target as UIParamCopyTool;
        if (GUILayout.Button("+"))
        {
            _target.Add();
        }
        scrollposition = GUILayout.BeginScrollView(scrollposition);
        for (int i = 0; i < _target.Count; i++)
        {
            GUILayout.BeginHorizontal();
            EditorGUILayout.EnumPopup(_target[i].type);
            if (_target[i].Object == null)
            {
                _target[i].Object = (Graphic)EditorGUILayout.ObjectField("Component:", _target[i].Object, typeof(Graphic), true);
            }
            else if (_target[i].Object is Image)
            {
                _target[i].name = _target[i].Object.name;
                _target[i].type = UIParamCopyTool.UIType.Image;
                _target[i].Object = (Image)EditorGUILayout.ObjectField(_target[i].name, _target[i].Object, typeof(Image), true);
            }
            else if (_target[i].Object is Text)
            {
                _target[i].name = _target[i].Object.name;
                _target[i].type = UIParamCopyTool.UIType.Text;
                _target[i].Object = (Text)EditorGUILayout.ObjectField(_target[i].name, _target[i].Object, typeof(Text), true);
            }
            if (GUILayout.Button("-"))
            {
                _target.Remove(i);
                i--;
            }
            GUILayout.EndHorizontal();
        }
        GUILayout.EndScrollView();
        GUILayout.BeginHorizontal();
        targetName = EditorGUILayout.TextField("目标名:", targetName);
        if (GUILayout.Button("Find"))
        {
            if (targetName != string.Empty)
            {
                _target.FindTarget(targetName);
            }
        }
        GUILayout.EndHorizontal();
        scrollposition1 = GUILayout.BeginScrollView(scrollposition1);
        for (int i = 0; i < _target.copyTargets.Count; i++)
        {
            GUILayout.BeginHorizontal();
            var _copyTarget = _target.copyTargets[i];
            _copyTarget.ok = EditorGUILayout.Toggle("是否替换", _copyTarget.ok, GUILayout.Width(40));
            EditorGUILayout.LabelField(StringUtility.Contact("Image", _copyTarget.images.Count));
            EditorGUILayout.LabelField(StringUtility.Contact("Text", _copyTarget.texts.Count));
            GUILayout.EndHorizontal();
        }
        GUILayout.EndScrollView();
        if (GUILayout.Button("Paste"))
        {
            _target.Paste();
        }
    }
}
#endif