三国卡牌客户端基础资源仓库
hch
2025-09-15 ea3ad185fcbccce1bd49d467de7186ac08edab24
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/******************************************************************************
 * Spine Runtimes License Agreement
 * Last updated July 28, 2023. Replaces all prior versions.
 *
 * Copyright (c) 2013-2023, Esoteric Software LLC
 *
 * Integration of the Spine Runtimes into software or otherwise creating
 * derivative works of the Spine Runtimes is permitted under the terms and
 * conditions of Section 2 of the Spine Editor License Agreement:
 * http://esotericsoftware.com/spine-editor-license
 *
 * Otherwise, it is permitted to integrate the Spine Runtimes into software or
 * otherwise create derivative works of the Spine Runtimes (collectively,
 * "Products"), provided that each user of the Products must obtain their own
 * Spine Editor license and redistribution of the Products in any form must
 * include this license and copyright notice.
 *
 * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "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 LLC 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 THE
 * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
 
#if UNITY_2018_2_OR_NEWER
#define EXPOSES_SPRITE_ATLAS_UTILITIES
#endif
 
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
 
#if UNITY_EDITOR
using UnityEditor;
using System.Reflection;
#endif
 
namespace Spine.Unity {
    /// <summary>Loads and stores a Spine atlas and list of materials.</summary>
    [CreateAssetMenu(fileName = "New Spine SpriteAtlas Asset", menuName = "Spine/Spine SpriteAtlas Asset")]
    public class SpineSpriteAtlasAsset : AtlasAssetBase {
        public SpriteAtlas spriteAtlasFile;
        public Material[] materials;
        protected Atlas atlas;
        public bool updateRegionsInPlayMode;
 
        [System.Serializable]
        protected class SavedRegionInfo {
            public float x, y, width, height;
            public SpritePackingRotation packingRotation;
        }
        [SerializeField] protected SavedRegionInfo[] savedRegions;
 
        public override bool IsLoaded { get { return this.atlas != null; } }
 
        public override IEnumerable<Material> Materials { get { return materials; } }
        public override int MaterialCount { get { return materials == null ? 0 : materials.Length; } }
        public override Material PrimaryMaterial { get { return materials[0]; } }
 
#if UNITY_EDITOR
        static MethodInfo GetPackedSpritesMethod, GetPreviewTexturesMethod;
#if !EXPOSES_SPRITE_ATLAS_UTILITIES
        static MethodInfo PackAtlasesMethod;
#endif
#endif
 
        #region Runtime Instantiation
        /// <summary>
        /// Creates a runtime AtlasAsset</summary>
        public static SpineSpriteAtlasAsset CreateRuntimeInstance (SpriteAtlas spriteAtlasFile, Material[] materials, bool initialize) {
            SpineSpriteAtlasAsset atlasAsset = ScriptableObject.CreateInstance<SpineSpriteAtlasAsset>();
            atlasAsset.Reset();
            atlasAsset.spriteAtlasFile = spriteAtlasFile;
            atlasAsset.materials = materials;
 
            if (initialize)
                atlasAsset.GetAtlas();
 
            return atlasAsset;
        }
        #endregion
 
        void Reset () {
            Clear();
        }
 
        public override void Clear () {
            atlas = null;
        }
 
        /// <returns>The atlas or null if it could not be loaded.</returns>
        public override Atlas GetAtlas (bool onlyMetaData = false) {
            if (spriteAtlasFile == null) {
                Debug.LogError("SpriteAtlas file not set for SpineSpriteAtlasAsset: " + name, this);
                Clear();
                return null;
            }
 
            if (!onlyMetaData && (materials == null || materials.Length == 0)) {
                Debug.LogError("Materials not set for SpineSpriteAtlasAsset: " + name, this);
                Clear();
                return null;
            }
 
            if (atlas != null) return atlas;
 
            try {
                atlas = LoadAtlas(spriteAtlasFile);
                return atlas;
            } catch (Exception ex) {
                Debug.LogError("Error analyzing SpriteAtlas for SpineSpriteAtlasAsset: " + name + "\n" + ex.Message + "\n" + ex.StackTrace, this);
                return null;
            }
        }
 
        protected void AssignRegionsFromSavedRegions (Sprite[] sprites, Atlas usedAtlas) {
 
            if (savedRegions == null || savedRegions.Length != sprites.Length)
                return;
 
            int i = 0;
            foreach (AtlasRegion region in usedAtlas) {
                SavedRegionInfo savedRegion = savedRegions[i];
                AtlasPage page = region.page;
 
                region.degrees = savedRegion.packingRotation == SpritePackingRotation.None ? 0 : 90;
 
                float x = savedRegion.x;
                float y = savedRegion.y;
                float width = savedRegion.width;
                float height = savedRegion.height;
 
                region.u = x / (float)page.width;
                region.v = y / (float)page.height;
                if (region.degrees == 90) {
                    region.u2 = (x + height) / (float)page.width;
                    region.v2 = (y + width) / (float)page.height;
                } else {
                    region.u2 = (x + width) / (float)page.width;
                    region.v2 = (y + height) / (float)page.height;
                }
                region.x = (int)x;
                region.y = (int)y;
                region.width = Math.Abs((int)width);
                region.height = Math.Abs((int)height);
 
                // flip upside down
                float temp = region.v;
                region.v = region.v2;
                region.v2 = temp;
 
                region.originalWidth = (int)width;
                region.originalHeight = (int)height;
 
                // note: currently sprite pivot offsets are ignored.
                // Sprite sprite = sprites[i];
                region.offsetX = 0;//sprite.pivot.x;
                region.offsetY = 0;//sprite.pivot.y;
 
                ++i;
            }
        }
 
        private Atlas LoadAtlas (UnityEngine.U2D.SpriteAtlas spriteAtlas) {
 
            List<AtlasPage> pages = new List<AtlasPage>();
            List<AtlasRegion> regions = new List<AtlasRegion>();
 
            Sprite[] sprites = new UnityEngine.Sprite[spriteAtlas.spriteCount];
            spriteAtlas.GetSprites(sprites);
            if (sprites.Length == 0)
                return new Atlas(pages, regions);
 
            Texture2D texture = null;
#if UNITY_EDITOR
            if (!Application.isPlaying)
                texture = AccessPackedTextureEditor(spriteAtlas);
            else
#endif
            texture = AccessPackedTexture(sprites);
 
            Material material = materials[0];
#if !UNITY_EDITOR
            material.mainTexture = texture;
#endif
 
            Spine.AtlasPage page = new AtlasPage();
            page.name = spriteAtlas.name;
            page.width = texture.width;
            page.height = texture.height;
            page.format = Spine.Format.RGBA8888;
 
            page.minFilter = TextureFilter.Linear;
            page.magFilter = TextureFilter.Linear;
            page.uWrap = TextureWrap.ClampToEdge;
            page.vWrap = TextureWrap.ClampToEdge;
            page.rendererObject = material;
            pages.Add(page);
 
            sprites = AccessPackedSprites(spriteAtlas);
 
            int i = 0;
            for (; i < sprites.Length; ++i) {
                Sprite sprite = sprites[i];
                AtlasRegion region = new AtlasRegion();
                region.name = sprite.name.Replace("(Clone)", "");
                region.page = page;
                region.degrees = sprite.packingRotation == SpritePackingRotation.None ? 0 : 90;
 
                region.u2 = 1;
                region.v2 = 1;
                region.width = page.width;
                region.height = page.height;
                region.originalWidth = page.width;
                region.originalHeight = page.height;
 
                region.index = i;
                regions.Add(region);
            }
 
            Atlas atlas = new Atlas(pages, regions);
            AssignRegionsFromSavedRegions(sprites, atlas);
 
            return atlas;
        }
 
#if UNITY_EDITOR
        public static void UpdateByStartingEditorPlayMode () {
            EditorApplication.isPlaying = true;
        }
 
        public static bool AnySpriteAtlasNeedsRegionsLoaded () {
            string[] guids = UnityEditor.AssetDatabase.FindAssets("t:SpineSpriteAtlasAsset");
            foreach (string guid in guids) {
                string path = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
                if (!string.IsNullOrEmpty(path)) {
                    SpineSpriteAtlasAsset atlasAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<SpineSpriteAtlasAsset>(path);
                    if (atlasAsset) {
                        if (atlasAsset.RegionsNeedLoading)
                            return true;
                    }
                }
            }
            return false;
        }
 
        public static void UpdateWhenEditorPlayModeStarted () {
            if (!EditorApplication.isPlaying)
                return;
 
            EditorApplication.update -= UpdateWhenEditorPlayModeStarted;
            string[] guids = UnityEditor.AssetDatabase.FindAssets("t:SpineSpriteAtlasAsset");
            if (guids.Length == 0)
                return;
 
            Debug.Log("Updating SpineSpriteAtlasAssets");
            foreach (string guid in guids) {
                string path = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
                if (!string.IsNullOrEmpty(path)) {
                    SpineSpriteAtlasAsset atlasAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<SpineSpriteAtlasAsset>(path);
                    if (atlasAsset) {
                        atlasAsset.atlas = atlasAsset.LoadAtlas(atlasAsset.spriteAtlasFile);
                        atlasAsset.LoadRegionsInEditorPlayMode();
                        Debug.Log(string.Format("Updated regions of '{0}'", atlasAsset.name), atlasAsset);
                    }
                }
            }
 
            EditorApplication.isPlaying = false;
        }
 
        public bool RegionsNeedLoading {
            get { return savedRegions == null || savedRegions.Length == 0 || updateRegionsInPlayMode; }
        }
 
        public void LoadRegionsInEditorPlayMode () {
 
            Sprite[] sprites = null;
            System.Type T = Type.GetType("UnityEditor.U2D.SpriteAtlasExtensions,UnityEditor");
            MethodInfo method = T.GetMethod("GetPackedSprites", BindingFlags.NonPublic | BindingFlags.Static);
            if (method != null) {
                object retval = method.Invoke(null, new object[] { spriteAtlasFile });
                Sprite[] spritesArray = retval as Sprite[];
                if (spritesArray != null && spritesArray.Length > 0) {
                    sprites = spritesArray;
                }
            }
            if (sprites == null) {
                sprites = new UnityEngine.Sprite[spriteAtlasFile.spriteCount];
                spriteAtlasFile.GetSprites(sprites);
            }
            if (sprites.Length == 0) {
                Debug.LogWarning(string.Format("SpriteAtlas '{0}' contains no sprites. Please make sure all assigned images are set to import type 'Sprite'.", spriteAtlasFile.name), spriteAtlasFile);
                return;
            } else if (sprites[0].packingMode == SpritePackingMode.Tight) {
                Debug.LogError(string.Format("SpriteAtlas '{0}': Tight packing is not supported. Please disable 'Tight Packing' in the SpriteAtlas Inspector.", spriteAtlasFile.name), spriteAtlasFile);
                return;
            }
 
            if (savedRegions == null || savedRegions.Length != sprites.Length)
                savedRegions = new SavedRegionInfo[sprites.Length];
 
            int i = 0;
            foreach (AtlasRegion region in atlas) {
                Sprite sprite = sprites[i];
                Rect rect = sprite.textureRect;
                float x = rect.min.x;
                float y = rect.min.y;
                float width = rect.width;
                float height = rect.height;
 
                SavedRegionInfo savedRegion = new SavedRegionInfo();
                savedRegion.x = x;
                savedRegion.y = y;
                savedRegion.width = width;
                savedRegion.height = height;
                savedRegion.packingRotation = sprite.packingRotation;
                savedRegions[i] = savedRegion;
 
                ++i;
            }
            updateRegionsInPlayMode = false;
            AssignRegionsFromSavedRegions(sprites, atlas);
            EditorUtility.SetDirty(this);
            AssetDatabase.SaveAssets();
        }
 
        public static Texture2D AccessPackedTextureEditor (SpriteAtlas spriteAtlas) {
#if EXPOSES_SPRITE_ATLAS_UTILITIES
            UnityEditor.U2D.SpriteAtlasUtility.PackAtlases(new SpriteAtlas[] { spriteAtlas }, EditorUserBuildSettings.activeBuildTarget);
#else
            /*if (PackAtlasesMethod == null) {
                System.Type T = Type.GetType("UnityEditor.U2D.SpriteAtlasUtility,UnityEditor");
                PackAtlasesMethod = T.GetMethod("PackAtlases", BindingFlags.NonPublic | BindingFlags.Static);
            }
            if (PackAtlasesMethod != null) {
                PackAtlasesMethod.Invoke(null, new object[] { new SpriteAtlas[] { spriteAtlas }, EditorUserBuildSettings.activeBuildTarget });
            }*/
#endif
            if (GetPreviewTexturesMethod == null) {
                System.Type T = Type.GetType("UnityEditor.U2D.SpriteAtlasExtensions,UnityEditor");
                GetPreviewTexturesMethod = T.GetMethod("GetPreviewTextures", BindingFlags.NonPublic | BindingFlags.Static);
            }
            if (GetPreviewTexturesMethod != null) {
                object retval = GetPreviewTexturesMethod.Invoke(null, new object[] { spriteAtlas });
                Texture2D[] textures = retval as Texture2D[];
                if (textures.Length > 0)
                    return textures[0];
            }
            return null;
        }
#endif
        public static Texture2D AccessPackedTexture (Sprite[] sprites) {
            return sprites[0].texture;
        }
 
 
        public static Sprite[] AccessPackedSprites (UnityEngine.U2D.SpriteAtlas spriteAtlas) {
            Sprite[] sprites = null;
#if UNITY_EDITOR
            if (!Application.isPlaying) {
 
                if (GetPackedSpritesMethod == null) {
                    System.Type T = Type.GetType("UnityEditor.U2D.SpriteAtlasExtensions,UnityEditor");
                    GetPackedSpritesMethod = T.GetMethod("GetPackedSprites", BindingFlags.NonPublic | BindingFlags.Static);
                }
                if (GetPackedSpritesMethod != null) {
                    object retval = GetPackedSpritesMethod.Invoke(null, new object[] { spriteAtlas });
                    Sprite[] spritesArray = retval as Sprite[];
                    if (spritesArray != null && spritesArray.Length > 0) {
                        sprites = spritesArray;
                    }
                }
            }
#endif
            if (sprites == null) {
                sprites = new UnityEngine.Sprite[spriteAtlas.spriteCount];
                spriteAtlas.GetSprites(sprites);
                if (sprites.Length == 0)
                    return null;
            }
            return sprites;
        }
    }
}