少年修仙传客户端代码仓库
client_Wu Xijin
2019-04-16 fc714208ff3a320c3bb52bddd5ea3d91f647a206
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
348
349
350
351
352
353
354
355
/******************************************************************************
 * Spine Runtimes Software License v2.5
 *
 * Copyright (c) 2013-2016, Esoteric Software
 * All rights reserved.
 *
 * You are granted a perpetual, non-exclusive, non-sublicensable, and
 * non-transferable license to use, install, execute, and perform the Spine
 * Runtimes software and derivative works solely for personal or internal
 * use. Without the written permission of Esoteric Software (see Section 2 of
 * the Spine Software License Agreement), you may not (a) modify, translate,
 * adapt, or develop new applications using the Spine Runtimes or otherwise
 * create derivative works or improvements of the Spine Runtimes or (b) remove,
 * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
 * or other intellectual property or proprietary rights notices on or in the
 * Software, including any copy thereof. Redistributions in binary or source
 * form must include this license and terms.
 *
 * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
 * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
 
//#define BAKE_ALL_BUTTON
//#define REGION_BAKING_MESH
 
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Spine;
 
namespace Spine.Unity.Editor {
    using Event = UnityEngine.Event;
 
    [CustomEditor(typeof(AtlasAsset)), CanEditMultipleObjects]
    public class AtlasAssetInspector : UnityEditor.Editor {
        SerializedProperty atlasFile, materials;
        AtlasAsset atlasAsset;
 
        GUIContent spriteSlicesLabel;
        GUIContent SpriteSlicesLabel {
            get {
                if (spriteSlicesLabel == null) {
                    spriteSlicesLabel = new GUIContent(
                        "Apply Regions as Texture Sprite Slices",
                        SpineEditorUtilities.Icons.unityIcon,
                        "Adds Sprite slices to atlas texture(s). " +
                        "Updates existing slices if ones with matching names exist. \n\n" +
                        "If your atlas was exported with Premultiply Alpha, " +
                        "your SpriteRenderer should use the generated Spine _Material asset (or any Material with a PMA shader) instead of Sprites-Default.");
                }
                return spriteSlicesLabel; 
            }
        }
 
        static List<AtlasRegion> GetRegions (Atlas atlas) {
            FieldInfo regionsField = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.NonPublic);
            return (List<AtlasRegion>)regionsField.GetValue(atlas);
        }
 
        void OnEnable () {
            SpineEditorUtilities.ConfirmInitialization();
            atlasFile = serializedObject.FindProperty("atlasFile");
            materials = serializedObject.FindProperty("materials");
            materials.isExpanded = true;
            atlasAsset = (AtlasAsset)target;
            #if REGION_BAKING_MESH
            UpdateBakedList();
            #endif
        }
 
        #if REGION_BAKING_MESH
        private List<bool> baked;
        private List<GameObject> bakedObjects;
 
        void UpdateBakedList () {
            AtlasAsset asset = (AtlasAsset)target;
            baked = new List<bool>();
            bakedObjects = new List<GameObject>();
            if (atlasFile.objectReferenceValue != null) {
                List<AtlasRegion> regions = this.Regions;
                string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset);
                string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath);
                string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name);
                for (int i = 0; i < regions.Count; i++) {
                    AtlasRegion region = regions[i];
                    string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(region) + ".prefab").Replace("\\", "/");
                    GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(GameObject));
                    baked.Add(prefab != null);
                    bakedObjects.Add(prefab);
                }
            }
        }
        #endif
 
        override public void OnInspectorGUI () {
            if (serializedObject.isEditingMultipleObjects) {
                DrawDefaultInspector();
                return;
            }
 
            serializedObject.Update();
            atlasAsset = atlasAsset ?? (AtlasAsset)target;
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(atlasFile);
            EditorGUILayout.PropertyField(materials, true);
            if (EditorGUI.EndChangeCheck()) {
                serializedObject.ApplyModifiedProperties();
                atlasAsset.Clear();
                atlasAsset.GetAtlas();
            }
 
            if (materials.arraySize == 0) {
                EditorGUILayout.LabelField(new GUIContent("Error:  Missing materials", SpineEditorUtilities.Icons.warning));
                return;
            }
 
            for (int i = 0; i < materials.arraySize; i++) {
                SerializedProperty prop = materials.GetArrayElementAtIndex(i);
                Material mat = (Material)prop.objectReferenceValue;
                if (mat == null) {
                    EditorGUILayout.LabelField(new GUIContent("Error:  Materials cannot be null", SpineEditorUtilities.Icons.warning));
                    return;
                }
            }
 
            EditorGUILayout.Space();
            if (atlasFile.objectReferenceValue != null) {
                if (SpineInspectorUtility.LargeCenteredButton(SpriteSlicesLabel)) {
                    var atlas = atlasAsset.GetAtlas();
                    foreach (var m in atlasAsset.materials)
                        UpdateSpriteSlices(m.mainTexture, atlas);
                }
            }
 
            #if REGION_BAKING_MESH
            if (atlasFile.objectReferenceValue != null) {
                Atlas atlas = asset.GetAtlas();
                FieldInfo field = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic);
                List<AtlasRegion> regions = (List<AtlasRegion>)field.GetValue(atlas);
                EditorGUILayout.LabelField(new GUIContent("Region Baking", SpineEditorUtilities.Icons.unityIcon));
                EditorGUI.indentLevel++;
                AtlasPage lastPage = null;
                for (int i = 0; i < regions.Count; i++) {
                    if (lastPage != regions[i].page) {
                        if (lastPage != null) {
                            EditorGUILayout.Separator();
                            EditorGUILayout.Separator();
                        }
                        lastPage = regions[i].page;
                        Material mat = ((Material)lastPage.rendererObject);
                        if (mat != null) {
                            GUILayout.BeginHorizontal();
                            {
                                EditorGUI.BeginDisabledGroup(true);
                                EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250));
                                EditorGUI.EndDisabledGroup();
                            }
                            GUILayout.EndHorizontal();
 
                        } else {
                            EditorGUILayout.LabelField(new GUIContent("Page missing material!", SpineEditorUtilities.Icons.warning));
                        }
                    }
                    GUILayout.BeginHorizontal();
                    {
                        //EditorGUILayout.ToggleLeft(baked[i] ? "" : regions[i].name, baked[i]);
                        bool result = baked[i] ? EditorGUILayout.ToggleLeft("", baked[i], GUILayout.Width(24)) : EditorGUILayout.ToggleLeft("    " + regions[i].name, baked[i]);
                        if(baked[i]){
                            EditorGUILayout.ObjectField(bakedObjects[i], typeof(GameObject), false, GUILayout.Width(250));
                        }
                        if (result && !baked[i]) {
                            //bake
                            baked[i] = true;
                            bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]);
                            EditorGUIUtility.PingObject(bakedObjects[i]);
                        } else if (!result && baked[i]) {
                            //unbake
                            bool unbakeResult = EditorUtility.DisplayDialog("Delete Baked Region", "Do you want to delete the prefab for " + regions[i].name, "Yes", "Cancel");
                            switch (unbakeResult) {
                            case true:
                                //delete
                                string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset);
                                string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath);
                                string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name);
                                string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/");
                                AssetDatabase.DeleteAsset(bakedPrefabPath);
                                baked[i] = false;
                                break;
                            case false:
                                //do nothing
                                break;
                            }
                        }
                    }
                    GUILayout.EndHorizontal();
                }
                EditorGUI.indentLevel--;
 
                #if BAKE_ALL_BUTTON
                // Check state
                bool allBaked = true;
                bool allUnbaked = true;
                for (int i = 0; i < regions.Count; i++) {
                    allBaked &= baked[i];
                    allUnbaked &= !baked[i];
                }
 
                if (!allBaked && GUILayout.Button("Bake All")) {
                    for (int i = 0; i < regions.Count; i++) {
                        if (!baked[i]) {
                            baked[i] = true;
                            bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]);
                        }
                    }
 
                } else if (!allUnbaked && GUILayout.Button("Unbake All")) {
                    bool unbakeResult = EditorUtility.DisplayDialog("Delete All Baked Regions", "Are you sure you want to unbake all region prefabs? This cannot be undone.", "Yes", "Cancel");
                    switch (unbakeResult) {
                    case true:
                        //delete
                        for (int i = 0; i < regions.Count; i++) {
                            if (baked[i]) {
                                string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset);
                                string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath);
                                string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name);
                                string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/");
                                AssetDatabase.DeleteAsset(bakedPrefabPath);
                                baked[i] = false;
                            }
                        }
                        break;
                    case false:
                        //do nothing
                        break;
                    }
 
                }
                #endif
                
            }
            #else
            if (atlasFile.objectReferenceValue != null) {
                EditorGUILayout.LabelField("Atlas Regions", EditorStyles.boldLabel);
                int baseIndent = EditorGUI.indentLevel;
 
                var regions = AtlasAssetInspector.GetRegions(atlasAsset.GetAtlas());
                AtlasPage lastPage = null;
                for (int i = 0; i < regions.Count; i++) {
                    if (lastPage != regions[i].page) {
                        if (lastPage != null) {
                            EditorGUILayout.Separator();
                            EditorGUILayout.Separator();
                        }
                        lastPage = regions[i].page;
                        Material mat = ((Material)lastPage.rendererObject);
                        if (mat != null) {
                            EditorGUI.indentLevel = baseIndent;
                            using (new GUILayout.HorizontalScope())
                            using (new EditorGUI.DisabledGroupScope(true))
                                EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250));
                            EditorGUI.indentLevel = baseIndent + 1;
                        } else {
                            EditorGUILayout.HelpBox("Page missing material!", MessageType.Warning);
                        }
                    }
 
                    EditorGUILayout.LabelField(new GUIContent(regions[i].name, SpineEditorUtilities.Icons.image));
                }
                EditorGUI.indentLevel = baseIndent;
            }
            #endif
 
            if (serializedObject.ApplyModifiedProperties() || SpineInspectorUtility.UndoRedoPerformed(Event.current))
                atlasAsset.Clear();
        }
 
        static public void UpdateSpriteSlices (Texture texture, Atlas atlas) {
            string texturePath = AssetDatabase.GetAssetPath(texture.GetInstanceID());
            var t = (TextureImporter)TextureImporter.GetAtPath(texturePath);
            t.spriteImportMode = SpriteImportMode.Multiple;
            var spriteSheet = t.spritesheet;
            var sprites = new List<SpriteMetaData>(spriteSheet);
 
            var regions = AtlasAssetInspector.GetRegions(atlas);
            char[] FilenameDelimiter = {'.'};
            int updatedCount = 0;
            int addedCount = 0;
 
            foreach (var r in regions) {
                string pageName = r.page.name.Split(FilenameDelimiter, StringSplitOptions.RemoveEmptyEntries)[0];
                string textureName = texture.name;
                bool pageMatch = string.Equals(pageName, textureName, StringComparison.Ordinal);
 
//                if (pageMatch) {
//                    int pw = r.page.width;
//                    int ph = r.page.height;
//                    bool mismatchSize = pw != texture.width || pw > t.maxTextureSize || ph != texture.height || ph > t.maxTextureSize;
//                    if (mismatchSize)
//                        Debug.LogWarningFormat("Size mismatch found.\nExpected atlas size is {0}x{1}. Texture Import Max Size of texture '{2}'({4}x{5}) is currently set to {3}.", pw, ph, texture.name, t.maxTextureSize, texture.width, texture.height);
//                }
 
                int spriteIndex = pageMatch ? sprites.FindIndex(
                    (s) => string.Equals(s.name, r.name, StringComparison.Ordinal)
                ) : -1;
                bool spriteNameMatchExists = spriteIndex >= 0;
 
                if (pageMatch) {
                    Rect spriteRect = new Rect();
 
                    if (r.rotate) {
                        spriteRect.width = r.height;
                        spriteRect.height = r.width;
                    } else {
                        spriteRect.width = r.width;
                        spriteRect.height = r.height;
                    }
                    spriteRect.x = r.x;
                    spriteRect.y = r.page.height - spriteRect.height - r.y;
 
                    if (spriteNameMatchExists) {
                        var s = sprites[spriteIndex];
                        s.rect = spriteRect;
                        sprites[spriteIndex] = s;
                        updatedCount++;
                    } else {
                        sprites.Add(new SpriteMetaData {
                            name = r.name,
                            pivot = new Vector2(0.5f, 0.5f),
                            rect = spriteRect
                        });
                        addedCount++;
                    }
                }
 
            }
 
            t.spritesheet = sprites.ToArray();
            EditorUtility.SetDirty(t);
            AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);
            EditorGUIUtility.PingObject(texture);
            Debug.Log(string.Format("Applied sprite slices to {2}. {0} added. {1} updated.", addedCount, updatedCount, texture.name));
        }
    }
 
}