2024-10-23 17:55:55 +08:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Spine Runtimes License Agreement
2024-11-21 09:35:48 +08:00
* Last updated July 28 , 2023. Replaces all prior versions .
2024-10-23 17:55:55 +08:00
*
2024-11-21 09:35:48 +08:00
* Copyright ( c ) 2013 - 2023 , Esoteric Software LLC
2024-10-23 17:55:55 +08:00
*
* 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
*
2024-11-21 09:35:48 +08:00
* Otherwise , it is permitted to integrate the Spine Runtimes into software or
* otherwise create derivative works of the Spine Runtimes ( collectively ,
2024-10-23 17:55:55 +08:00
* "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
2024-11-21 09:35:48 +08:00
* ( 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 .
2024-10-23 17:55:55 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
using System.Collections ;
2024-11-21 09:35:48 +08:00
using System.Collections.Generic ;
using UnityEngine ;
2024-10-23 17:55:55 +08:00
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" ) ;
2024-11-21 09:35:48 +08:00
AtlasRegion region = sprite . ToAtlasRegion ( page ) ;
float unitsPerPixel = 1f / sprite . pixelsPerUnit ;
2024-10-23 17:55:55 +08:00
return region . ToRegionAttachment ( sprite . name , unitsPerPixel , rotation ) ;
}
/// <summary>
2024-11-21 09:35:48 +08:00
/// 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>
2024-10-23 17:55:55 +08:00
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" ) ;
2024-11-21 09:35:48 +08:00
AtlasRegion region = sprite . ToAtlasRegionPMAClone ( shader , textureFormat , mipmaps , materialPropertySource ) ;
float unitsPerPixel = 1f / sprite . pixelsPerUnit ;
2024-10-23 17:55:55 +08:00
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)
2024-11-21 09:35:48 +08:00
RegionAttachment attachment = new RegionAttachment ( attachmentName ) ;
2024-10-23 17:55:55 +08:00
2024-11-21 09:35:48 +08:00
attachment . Region = region ;
2024-10-23 17:55:55 +08:00
attachment . Path = region . name ;
2024-11-21 09:35:48 +08:00
attachment . ScaleX = 1 ;
attachment . ScaleY = 1 ;
attachment . Rotation = rotation ;
2024-10-23 17:55:55 +08:00
2024-11-21 09:35:48 +08:00
attachment . R = 1 ;
attachment . G = 1 ;
attachment . B = 1 ;
attachment . A = 1 ;
2024-10-23 17:55:55 +08:00
// pass OriginalWidth and OriginalHeight because UpdateOffset uses it in its calculation.
2024-11-21 09:35:48 +08:00
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 ;
2024-10-23 17:55:55 +08:00
attachment . SetColor ( Color . white ) ;
2024-11-21 09:35:48 +08:00
attachment . UpdateRegion ( ) ;
2024-10-23 17:55:55 +08:00
return attachment ;
}
/// <summary> Sets the scale. Call regionAttachment.UpdateOffset to apply the change.</summary>
public static void SetScale ( this RegionAttachment regionAttachment , Vector2 scale ) {
2024-11-21 09:35:48 +08:00
regionAttachment . ScaleX = scale . x ;
regionAttachment . ScaleY = scale . y ;
2024-10-23 17:55:55 +08:00
}
/// <summary> Sets the scale. Call regionAttachment.UpdateOffset to apply the change.</summary>
public static void SetScale ( this RegionAttachment regionAttachment , float x , float y ) {
2024-11-21 09:35:48 +08:00
regionAttachment . ScaleX = x ;
regionAttachment . ScaleY = y ;
2024-10-23 17:55:55 +08:00
}
/// <summary> Sets the position offset. Call regionAttachment.UpdateOffset to apply the change.</summary>
public static void SetPositionOffset ( this RegionAttachment regionAttachment , Vector2 offset ) {
2024-11-21 09:35:48 +08:00
regionAttachment . X = offset . x ;
regionAttachment . Y = offset . y ;
2024-10-23 17:55:55 +08:00
}
/// <summary> Sets the position offset. Call regionAttachment.UpdateOffset to apply the change.</summary>
public static void SetPositionOffset ( this RegionAttachment regionAttachment , float x , float y ) {
2024-11-21 09:35:48 +08:00
regionAttachment . X = x ;
regionAttachment . Y = y ;
2024-10-23 17:55:55 +08:00
}
/// <summary> Sets the rotation. Call regionAttachment.UpdateOffset to apply the change.</summary>
public static void SetRotation ( this RegionAttachment regionAttachment , float rotation ) {
2024-11-21 09:35:48 +08:00
regionAttachment . Rotation = rotation ;
2024-10-23 17:55:55 +08:00
}
#endregion
}
}