三国卡牌客户端基础资源仓库
hch
2 天以前 9b89d57827524ea02d367a717af45e2360319312
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
/******************************************************************************
 * 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.
 *****************************************************************************/
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
namespace Spine.Unity.AttachmentTools {
    public static class AttachmentRegionExtensions {
        #region Runtime RegionAttachments
        /// <summary>
        /// Creates a RegionAttachment based on a sprite. This method creates a real, usable AtlasRegion. That AtlasRegion uses a new AtlasPage with the Material provided./// </summary>
        public static RegionAttachment ToRegionAttachment (this Sprite sprite, Material material, float rotation = 0f) {
            return sprite.ToRegionAttachment(material.ToSpineAtlasPage(), rotation);
        }
 
        /// <summary>
        /// Creates a RegionAttachment based on a sprite. This method creates a real, usable AtlasRegion. That AtlasRegion uses the AtlasPage provided./// </summary>
        public static RegionAttachment ToRegionAttachment (this Sprite sprite, AtlasPage page, float rotation = 0f) {
            if (sprite == null) throw new System.ArgumentNullException("sprite");
            if (page == null) throw new System.ArgumentNullException("page");
            AtlasRegion region = sprite.ToAtlasRegion(page);
            float unitsPerPixel = 1f / sprite.pixelsPerUnit;
            return region.ToRegionAttachment(sprite.name, unitsPerPixel, rotation);
        }
 
        /// <summary>
        /// Creates a Spine.AtlasRegion that uses a premultiplied alpha duplicate texture of the Sprite's texture data.
        /// Returns a RegionAttachment that uses it. Use this if you plan to use a premultiply alpha shader such as "Spine/Skeleton".</summary>
        /// <remarks>The duplicate texture is cached for later re-use. See documentation of
        /// <see cref="AttachmentCloneExtensions.GetRemappedClone"/> for additional details.</remarks>
        public static RegionAttachment ToRegionAttachmentPMAClone (this Sprite sprite, Shader shader, TextureFormat textureFormat = AtlasUtilities.SpineTextureFormat, bool mipmaps = AtlasUtilities.UseMipMaps, Material materialPropertySource = null, float rotation = 0f) {
            if (sprite == null) throw new System.ArgumentNullException("sprite");
            if (shader == null) throw new System.ArgumentNullException("shader");
            AtlasRegion region = sprite.ToAtlasRegionPMAClone(shader, textureFormat, mipmaps, materialPropertySource);
            float unitsPerPixel = 1f / sprite.pixelsPerUnit;
            return region.ToRegionAttachment(sprite.name, unitsPerPixel, rotation);
        }
 
        public static RegionAttachment ToRegionAttachmentPMAClone (this Sprite sprite, Material materialPropertySource, TextureFormat textureFormat = AtlasUtilities.SpineTextureFormat, bool mipmaps = AtlasUtilities.UseMipMaps, float rotation = 0f) {
            return sprite.ToRegionAttachmentPMAClone(materialPropertySource.shader, textureFormat, mipmaps, materialPropertySource, rotation);
        }
 
        /// <summary>
        /// Creates a new RegionAttachment from a given AtlasRegion.</summary>
        public static RegionAttachment ToRegionAttachment (this AtlasRegion region, string attachmentName, float scale = 0.01f, float rotation = 0f) {
            if (string.IsNullOrEmpty(attachmentName)) throw new System.ArgumentException("attachmentName can't be null or empty.", "attachmentName");
            if (region == null) throw new System.ArgumentNullException("region");
 
            // (AtlasAttachmentLoader.cs)
            RegionAttachment attachment = new RegionAttachment(attachmentName);
 
            attachment.Region = region;
            attachment.Path = region.name;
            attachment.ScaleX = 1;
            attachment.ScaleY = 1;
            attachment.Rotation = rotation;
 
            attachment.R = 1;
            attachment.G = 1;
            attachment.B = 1;
            attachment.A = 1;
 
            // pass OriginalWidth and OriginalHeight because UpdateOffset uses it in its calculation.
            TextureRegion textreRegion = attachment.Region;
            AtlasRegion atlasRegion = textreRegion as AtlasRegion;
            float originalWidth = atlasRegion != null ? atlasRegion.originalWidth : textreRegion.width;
            float originalHeight = atlasRegion != null ? atlasRegion.originalHeight : textreRegion.height;
            attachment.Width = originalWidth * scale;
            attachment.Height = originalHeight * scale;
 
            attachment.SetColor(Color.white);
            attachment.UpdateRegion();
            return attachment;
        }
 
        /// <summary> Sets the scale. Call regionAttachment.UpdateOffset to apply the change.</summary>
        public static void SetScale (this RegionAttachment regionAttachment, Vector2 scale) {
            regionAttachment.ScaleX = scale.x;
            regionAttachment.ScaleY = scale.y;
        }
 
        /// <summary> Sets the scale. Call regionAttachment.UpdateOffset to apply the change.</summary>
        public static void SetScale (this RegionAttachment regionAttachment, float x, float y) {
            regionAttachment.ScaleX = x;
            regionAttachment.ScaleY = y;
        }
 
        /// <summary> Sets the position offset. Call regionAttachment.UpdateOffset to apply the change.</summary>
        public static void SetPositionOffset (this RegionAttachment regionAttachment, Vector2 offset) {
            regionAttachment.X = offset.x;
            regionAttachment.Y = offset.y;
        }
 
        /// <summary> Sets the position offset. Call regionAttachment.UpdateOffset to apply the change.</summary>
        public static void SetPositionOffset (this RegionAttachment regionAttachment, float x, float y) {
            regionAttachment.X = x;
            regionAttachment.Y = y;
        }
 
        /// <summary> Sets the rotation. Call regionAttachment.UpdateOffset to apply the change.</summary>
        public static void SetRotation (this RegionAttachment regionAttachment, float rotation) {
            regionAttachment.Rotation = rotation;
        }
        #endregion
    }
}