From 5a1a19b4e558817644824832f0ab87c9d2547d8b Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Wed, 15 Dec 2021 19:44:10 +0100 Subject: [PATCH] [unity] `SpineAtlasAsset.CreateRuntimeInstance` methods now provide an optional `newCustomTextureLoader` parameter. --- CHANGELOG.md | 2 + .../spine-unity/Asset Types/AtlasAssetBase.cs | 1 - .../Asset Types/SpineAtlasAsset.cs | 37 +++++++++++++------ .../Asset Types/SpineSpriteAtlasAsset.cs | 6 +-- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bb3f751f..74ef6a364 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,8 @@ * **Breaking changes** * **Additions** + * `BoneFollower` and `BoneFollowerGraphic` now provide an additional `Follow Parent World Scale` parameter to allow following simple scale of parent bones (rotated/skewed scale can't be supported). + * `SpineAtlasAsset.CreateRuntimeInstance` methods now provide an optional `newCustomTextureLoader` parameter (defaults to `null`) which can be set to e.g. `(a) => new YourCustomTextureLoader(a)` to use your own `TextureLoader` subclass instead of `MaterialsTextureLoader`. * **Changes of default values** diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/AtlasAssetBase.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/AtlasAssetBase.cs index 8509e7127..e7abeec72 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/AtlasAssetBase.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/AtlasAssetBase.cs @@ -27,7 +27,6 @@ * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *****************************************************************************/ -using System.Collections; using System.Collections.Generic; using UnityEngine; diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SpineAtlasAsset.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SpineAtlasAsset.cs index f617c6a13..25692e713 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SpineAtlasAsset.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SpineAtlasAsset.cs @@ -27,7 +27,6 @@ * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *****************************************************************************/ -using Spine; using System; using System.Collections.Generic; using System.IO; @@ -39,6 +38,7 @@ namespace Spine.Unity { public class SpineAtlasAsset : AtlasAssetBase { public TextAsset atlasFile; public Material[] materials; + public TextureLoader customTextureLoader; protected Atlas atlas; public override bool IsLoaded { get { return this.atlas != null; } } @@ -50,11 +50,19 @@ namespace Spine.Unity { #region Runtime Instantiation /// /// Creates a runtime AtlasAsset - public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText, Material[] materials, bool initialize) { + /// When not null, a function instantiating + /// a custom TextureLoader with the newly created SpineAtlasAsset as argument + /// is used instead of instantiating the default MaterialsTextureLoader. + /// A valid parameter is e.g. (a) => new CustomTextureLoader(a) + public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText, Material[] materials, bool initialize, + Func newCustomTextureLoader = null) { + SpineAtlasAsset atlasAsset = ScriptableObject.CreateInstance(); atlasAsset.Reset(); atlasAsset.atlasFile = atlasText; atlasAsset.materials = materials; + if (newCustomTextureLoader != null) + atlasAsset.customTextureLoader = newCustomTextureLoader(atlasAsset); if (initialize) atlasAsset.GetAtlas(); @@ -70,8 +78,11 @@ namespace Spine.Unity { /// atlas asset JSON file. When procedurally creating textures, each Texture.name /// needs to be set to the atlas page texture filename without the .png extension, /// e.g. 'my_skeleton' if the png filename listed in the atlas asset file is 'my_skeleton.png'. - /// - public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText, Texture2D[] textures, Material materialPropertySource, bool initialize) { + /// + public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText, Texture2D[] textures, + Material materialPropertySource, bool initialize, + Func newCustomTextureLoader = null) { + // Get atlas page names. string atlasString = atlasText.text; atlasString = atlasString.Replace("\r", ""); @@ -106,24 +117,26 @@ namespace Spine.Unity { } // Create AtlasAsset normally - return CreateRuntimeInstance(atlasText, materials, initialize); + return CreateRuntimeInstance(atlasText, materials, initialize, newCustomTextureLoader); } /// - /// Creates a runtime AtlasAsset. Only providing the textures is slower because it has to search for atlas page matches. + /// Creates a runtime AtlasAsset. Only providing the textures is slower because + /// it has to search for atlas page matches. /// An array of all textures referenced in the provided atlasText /// atlas asset JSON file. When procedurally creating textures, each Texture.name /// needs to be set to the atlas page texture filename without the .png extension, /// e.g. 'my_skeleton' if the png filename listed in the atlas asset file is 'my_skeleton.png'. - /// - public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText, Texture2D[] textures, Shader shader, bool initialize) { + /// + public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText, + Texture2D[] textures, Shader shader, bool initialize, + Func newCustomTextureLoader = null) { + if (shader == null) shader = Shader.Find("Spine/Skeleton"); Material materialProperySource = new Material(shader); - var oa = CreateRuntimeInstance(atlasText, textures, materialProperySource, initialize); - - return oa; + return CreateRuntimeInstance(atlasText, textures, materialProperySource, initialize, newCustomTextureLoader); } #endregion @@ -154,7 +167,7 @@ namespace Spine.Unity { try { TextureLoader loader; if (!onlyMetaData) - loader = new MaterialsTextureLoader(this); + loader = customTextureLoader == null ? new MaterialsTextureLoader(this) : customTextureLoader; else loader = new NoOpTextureLoader(); atlas = new Atlas(new StringReader(atlasFile.text), "", loader); diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SpineSpriteAtlasAsset.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SpineSpriteAtlasAsset.cs index f02fb5e79..e2e044bfd 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SpineSpriteAtlasAsset.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/SpineSpriteAtlasAsset.cs @@ -34,7 +34,6 @@ using Spine; using System; using System.Collections.Generic; -using System.IO; using UnityEngine; using UnityEngine.U2D; @@ -185,7 +184,7 @@ namespace Spine.Unity { texture = AccessPackedTextureEditor(spriteAtlas); else #endif - texture = AccessPackedTexture(sprites); + texture = AccessPackedTexture(sprites); Material material = materials[0]; #if !UNITY_EDITOR @@ -300,8 +299,7 @@ namespace Spine.Unity { 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) { + } 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; }