[unity] SpineAtlasAsset.CreateRuntimeInstance methods now provide an optional newCustomTextureLoader parameter.

This commit is contained in:
Harald Csaszar 2021-12-15 19:44:10 +01:00
parent af4a511722
commit 5a1a19b4e5
4 changed files with 29 additions and 17 deletions

View File

@ -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**

View File

@ -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;

View File

@ -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
/// <summary>
/// Creates a runtime AtlasAsset</summary>
public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText, Material[] materials, bool initialize) {
/// <param name="newCustomTextureLoader">When not null, a function instantiating
/// a custom <c>TextureLoader</c> with the newly created <c>SpineAtlasAsset</c> as argument
/// is used instead of instantiating the default <c>MaterialsTextureLoader</c>.
/// A valid parameter is e.g. <c>(a) => new CustomTextureLoader(a)</c></param>
public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText, Material[] materials, bool initialize,
Func<SpineAtlasAsset, TextureLoader> newCustomTextureLoader = null) {
SpineAtlasAsset atlasAsset = ScriptableObject.CreateInstance<SpineAtlasAsset>();
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 <c>Texture.name</c>
/// 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'.</param>
/// <seealso cref="Spine.Unity.SpineAtlasAsset.CreateRuntimeInstance(TextAsset, Material[], bool)"/>
public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText, Texture2D[] textures, Material materialPropertySource, bool initialize) {
/// <seealso cref="SpineAtlasAsset.CreateRuntimeInstance(TextAsset, Material[], bool, Func{SpineAtlasAsset, TextureLoader})"/>
public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText, Texture2D[] textures,
Material materialPropertySource, bool initialize,
Func<SpineAtlasAsset, TextureLoader> 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);
}
/// <summary>
/// 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.
/// <param name="textures">An array of all textures referenced in the provided <c>atlasText</c>
/// atlas asset JSON file. When procedurally creating textures, each <c>Texture.name</c>
/// 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'.</param>
/// <seealso cref="Spine.Unity.AtlasAssetBase.CreateRuntimeInstance(TextAsset, Material[], bool)"/></summary>
public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText, Texture2D[] textures, Shader shader, bool initialize) {
/// <seealso cref="SpineAtlasAsset.CreateRuntimeInstance(TextAsset, Material[], bool, Func{SpineAtlasAsset, TextureLoader})"/>
public static SpineAtlasAsset CreateRuntimeInstance (TextAsset atlasText,
Texture2D[] textures, Shader shader, bool initialize,
Func<SpineAtlasAsset, TextureLoader> 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);

View File

@ -34,7 +34,6 @@
using Spine;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.U2D;
@ -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;
}