三国卡牌客户端基础资源仓库
yyl
3 天以前 cec146fc3fe287928e075c79ece20a20a9b16b20
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
/******************************************************************************
 * 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.
 *****************************************************************************/
 
#pragma warning disable 0219
 
#define SPINE_SKELETONMECANIM
 
#if UNITY_2017_2_OR_NEWER
#define NEWPLAYMODECALLBACKS
#endif
 
#if UNITY_2018_3 || UNITY_2019 || UNITY_2018_3_OR_NEWER
#define NEW_PREFAB_SYSTEM
#endif
 
#if UNITY_2018 || UNITY_2019 || UNITY_2018_3_OR_NEWER
#define NEWHIERARCHYWINDOWCALLBACKS
#endif
 
#if UNITY_2017_1_OR_NEWER
#define BUILT_IN_SPRITE_MASK_COMPONENT
#endif
 
#if BUILT_IN_SPRITE_MASK_COMPONENT
 
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEngine;
 
namespace Spine.Unity.Editor {
 
    public class SpineMaskUtilities {
 
        private const string MATERIAL_FILENAME_SUFFIX_INSIDE_MASK = "_InsideMask";
        private const string MATERIAL_FILENAME_SUFFIX_OUTSIDE_MASK = "_OutsideMask";
 
        public static void EditorAssignSpriteMaskMaterials (SkeletonRenderer skeleton) {
            SkeletonRenderer.SpriteMaskInteractionMaterials maskMaterials = skeleton.maskMaterials;
            SpriteMaskInteraction maskInteraction = skeleton.maskInteraction;
            MeshRenderer meshRenderer = skeleton.GetComponent<MeshRenderer>();
 
            if (maskMaterials.materialsMaskDisabled.Length > 0 && maskMaterials.materialsMaskDisabled[0] != null &&
                maskInteraction == SpriteMaskInteraction.None) {
                meshRenderer.materials = maskMaterials.materialsMaskDisabled;
            } else if (maskInteraction == SpriteMaskInteraction.VisibleInsideMask) {
                if (maskMaterials.materialsInsideMask.Length == 0 || maskMaterials.materialsInsideMask[0] == null)
                    EditorInitSpriteMaskMaterialsInsideMask(skeleton);
                meshRenderer.materials = maskMaterials.materialsInsideMask;
            } else if (maskInteraction == SpriteMaskInteraction.VisibleOutsideMask) {
                if (maskMaterials.materialsOutsideMask.Length == 0 || maskMaterials.materialsOutsideMask[0] == null)
                    EditorInitSpriteMaskMaterialsOutsideMask(skeleton);
                meshRenderer.materials = maskMaterials.materialsOutsideMask;
            }
        }
 
        public static bool AreMaskMaterialsMissing (SkeletonRenderer skeleton) {
            SkeletonRenderer.SpriteMaskInteractionMaterials maskMaterials = skeleton.maskMaterials;
            SpriteMaskInteraction maskInteraction = skeleton.maskInteraction;
 
            if (maskInteraction == SpriteMaskInteraction.VisibleInsideMask) {
                return (maskMaterials.materialsInsideMask.Length == 0 || maskMaterials.materialsInsideMask[0] == null);
            } else if (maskInteraction == SpriteMaskInteraction.VisibleOutsideMask) {
                return (maskMaterials.materialsOutsideMask.Length == 0 || maskMaterials.materialsOutsideMask[0] == null);
            }
            return false;
        }
 
        public static void EditorInitMaskMaterials (SkeletonRenderer skeleton, SkeletonRenderer.SpriteMaskInteractionMaterials maskMaterials, SpriteMaskInteraction maskType) {
            if (maskType == SpriteMaskInteraction.None) {
                EditorConfirmDisabledMaskMaterialsInit(skeleton);
            } else if (maskType == SpriteMaskInteraction.VisibleInsideMask) {
                EditorInitSpriteMaskMaterialsInsideMask(skeleton);
            } else if (maskType == SpriteMaskInteraction.VisibleOutsideMask) {
                EditorInitSpriteMaskMaterialsOutsideMask(skeleton);
            }
        }
 
        public static void EditorDeleteMaskMaterials (SkeletonRenderer.SpriteMaskInteractionMaterials maskMaterials, SpriteMaskInteraction maskType) {
            Material[] targetMaterials;
            if (maskType == SpriteMaskInteraction.VisibleInsideMask) {
                targetMaterials = maskMaterials.materialsInsideMask;
            } else if (maskType == SpriteMaskInteraction.VisibleOutsideMask) {
                targetMaterials = maskMaterials.materialsOutsideMask;
            } else {
                Debug.LogWarning("EditorDeleteMaskMaterials: Normal materials are kept as a reference and shall never be deleted.");
                return;
            }
 
            for (int i = 0; i < targetMaterials.Length; ++i) {
                Material material = targetMaterials[i];
                if (material != null) {
                    string materialPath = UnityEditor.AssetDatabase.GetAssetPath(material);
                    UnityEditor.AssetDatabase.DeleteAsset(materialPath);
                    Debug.Log(string.Concat("Deleted material '", materialPath, "'"));
                }
            }
 
            if (maskType == SpriteMaskInteraction.VisibleInsideMask) {
                maskMaterials.materialsInsideMask = new Material[0];
            } else if (maskType == SpriteMaskInteraction.VisibleOutsideMask) {
                maskMaterials.materialsOutsideMask = new Material[0];
            }
        }
 
        private static void EditorInitSpriteMaskMaterialsInsideMask (SkeletonRenderer skeleton) {
            SkeletonRenderer.SpriteMaskInteractionMaterials maskMaterials = skeleton.maskMaterials;
            EditorInitSpriteMaskMaterialsForMaskType(skeleton, SkeletonRenderer.STENCIL_COMP_MASKINTERACTION_VISIBLE_INSIDE,
                                                    ref maskMaterials.materialsInsideMask);
        }
 
        private static void EditorInitSpriteMaskMaterialsOutsideMask (SkeletonRenderer skeleton) {
            SkeletonRenderer.SpriteMaskInteractionMaterials maskMaterials = skeleton.maskMaterials;
            EditorInitSpriteMaskMaterialsForMaskType(skeleton, SkeletonRenderer.STENCIL_COMP_MASKINTERACTION_VISIBLE_OUTSIDE,
                                                    ref maskMaterials.materialsOutsideMask);
        }
 
        private static void EditorInitSpriteMaskMaterialsForMaskType (SkeletonRenderer skeleton, UnityEngine.Rendering.CompareFunction maskFunction,
                                                                ref Material[] materialsToFill) {
            if (!EditorConfirmDisabledMaskMaterialsInit(skeleton))
                return;
 
            SkeletonRenderer.SpriteMaskInteractionMaterials maskMaterials = skeleton.maskMaterials;
            Material[] originalMaterials = maskMaterials.materialsMaskDisabled;
            materialsToFill = new Material[originalMaterials.Length];
            for (int i = 0; i < originalMaterials.Length; i++) {
                Material newMaterial = null;
 
                if (!Application.isPlaying) {
                    newMaterial = EditorCreateOrLoadMaskMaterialAsset(maskMaterials, maskFunction, originalMaterials[i]);
                }
                if (newMaterial == null) {
                    newMaterial = new Material(originalMaterials[i]);
                    newMaterial.SetFloat(SkeletonRenderer.STENCIL_COMP_PARAM_ID, (int)maskFunction);
                }
                materialsToFill[i] = newMaterial;
            }
        }
 
        private static bool EditorConfirmDisabledMaskMaterialsInit (SkeletonRenderer skeleton) {
            SkeletonRenderer.SpriteMaskInteractionMaterials maskMaterials = skeleton.maskMaterials;
            if (maskMaterials.materialsMaskDisabled.Length > 0 && maskMaterials.materialsMaskDisabled[0] != null) {
                return true;
            }
 
            MeshRenderer meshRenderer = skeleton.GetComponent<MeshRenderer>();
            Material[] currentMaterials = meshRenderer.sharedMaterials;
 
            if (currentMaterials.Length == 0 || currentMaterials[0] == null) {
                // Note: if no attachments are visible, no materials are set. This is a valid state.
                return false;
            }
 
            // We have to be sure that there has not been a recompilation or similar events that led to
            // inside- or outside-mask materials being assigned to meshRenderer.sharedMaterials.
            string firstMaterialPath = UnityEditor.AssetDatabase.GetAssetPath(currentMaterials[0]);
            if (firstMaterialPath.Contains(MATERIAL_FILENAME_SUFFIX_INSIDE_MASK) ||
                firstMaterialPath.Contains(MATERIAL_FILENAME_SUFFIX_OUTSIDE_MASK)) {
 
                maskMaterials.materialsMaskDisabled = new Material[currentMaterials.Length];
                for (int i = 0; i < currentMaterials.Length; ++i) {
                    string path = UnityEditor.AssetDatabase.GetAssetPath(currentMaterials[i]);
                    string correctPath = null;
                    if (path.Contains(MATERIAL_FILENAME_SUFFIX_INSIDE_MASK)) {
                        correctPath = path.Replace(MATERIAL_FILENAME_SUFFIX_INSIDE_MASK, "");
                    } else if (path.Contains(MATERIAL_FILENAME_SUFFIX_OUTSIDE_MASK)) {
                        correctPath = path.Replace(MATERIAL_FILENAME_SUFFIX_OUTSIDE_MASK, "");
                    }
 
                    if (correctPath != null) {
                        Material material = UnityEditor.AssetDatabase.LoadAssetAtPath<Material>(correctPath);
                        if (material == null)
                            Debug.LogWarning("No original ignore-mask material found for path " + correctPath);
                        maskMaterials.materialsMaskDisabled[i] = material;
                    }
                }
            } else {
                maskMaterials.materialsMaskDisabled = currentMaterials;
            }
            return true;
        }
 
        public static Material EditorCreateOrLoadMaskMaterialAsset (SkeletonRenderer.SpriteMaskInteractionMaterials maskMaterials,
                                                                UnityEngine.Rendering.CompareFunction maskFunction, Material originalMaterial) {
            string originalMaterialPath = UnityEditor.AssetDatabase.GetAssetPath(originalMaterial);
            int posOfExtensionDot = originalMaterialPath.LastIndexOf('.');
            string materialPath = (maskFunction == SkeletonRenderer.STENCIL_COMP_MASKINTERACTION_VISIBLE_INSIDE) ?
                                                    originalMaterialPath.Insert(posOfExtensionDot, MATERIAL_FILENAME_SUFFIX_INSIDE_MASK) :
                                                    originalMaterialPath.Insert(posOfExtensionDot, MATERIAL_FILENAME_SUFFIX_OUTSIDE_MASK);
 
            Material material = UnityEditor.AssetDatabase.LoadAssetAtPath<Material>(materialPath);
            if (material != null) {
                return material;
            }
 
            material = new Material(originalMaterial);
            material.SetFloat(SkeletonRenderer.STENCIL_COMP_PARAM_ID, (int)maskFunction);
 
            UnityEditor.AssetDatabase.CreateAsset(material, materialPath);
            Debug.Log(string.Concat("Created material '", materialPath, "' for mask interaction based on '", originalMaterialPath, "'."));
            UnityEditor.EditorUtility.SetDirty(material);
            UnityEditor.AssetDatabase.SaveAssets();
            return material;
        }
    }
}
#endif // BUILT_IN_SPRITE_MASK_COMPONENT