From 8bc1201b8306356b49808bd319a91cbbe44525e5 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Fri, 6 Oct 2023 18:32:09 +0200 Subject: [PATCH] [unity] On-demand loading of Atlas Textures now supports SkeletonGraphic. Added method `RequestLoadTexture` and TextureRequested delegate to OnDemandTextureLoader. Fixed missing loader asset name suffix. See #1890. --- .../spine-unity/Asset Types/AtlasAssetBase.cs | 5 ++ .../Asset Types/OnDemandTextureLoader.cs | 32 +++++++++++ .../spine-unity/Components/SkeletonGraphic.cs | 55 ++++++++++++++++-- spine-unity/Assets/Spine/package.json | 2 +- .../AddressablesTextureLoaderInspector.cs | 3 +- .../Runtime/AddressablesTextureLoader.cs | 8 ++- .../package.json | 2 +- .../GenericOnDemandTextureLoaderInspector.cs | 22 ++++++-- .../Runtime/GenericOnDemandTextureLoader.cs | 56 +++++++++++++++---- .../package.json | 2 +- 10 files changed, 161 insertions(+), 26 deletions(-) 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 59b5bf5b6..4190ef2b1 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 @@ -71,6 +71,11 @@ namespace Spine.Unity { onDemandTextureLoader.RequestLoadMaterialTextures(material, ref overrideMaterial); } + public virtual void RequireTextureLoaded (Texture placeholderTexture, ref Texture replacementTexture, System.Action onTextureLoaded) { + if (onDemandTextureLoader) + onDemandTextureLoader.RequestLoadTexture(placeholderTexture, ref replacementTexture, onTextureLoaded); + } + [SerializeField] protected LoadingMode textureLoadingMode = LoadingMode.Normal; [SerializeField] protected OnDemandTextureLoader onDemandTextureLoader = null; #endif diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/OnDemandTextureLoader.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/OnDemandTextureLoader.cs index 16de1b725..a7b5c4613 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/OnDemandTextureLoader.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/OnDemandTextureLoader.cs @@ -51,6 +51,27 @@ namespace Spine.Unity { /// A newly created list of materials which has a placeholder texture assigned. /// True, if any placeholder texture is assigned at a Material of the associated AtlasAssetBase. public abstract bool HasPlaceholderTexturesAssigned (out List placeholderMaterials); + + /// + /// Returns whether any main texture is null at a Material of the associated AtlasAssetBase. + /// + /// A newly created list of materials which has a null main texture assigned. + /// True, if any null main texture is assigned at a Material of the associated AtlasAssetBase. + public virtual bool HasNullMainTexturesAssigned (out List nullTextureMaterials) { + nullTextureMaterials = null; + if (!atlasAsset) return false; + + bool anyNullTexture = false; + foreach (Material material in atlasAsset.Materials) { + if (material.mainTexture == null) { + anyNullTexture = true; + if (nullTextureMaterials == null) nullTextureMaterials = new List(); + nullTextureMaterials.Add(material); + } + } + return anyNullTexture; + } + /// /// Assigns previously setup target textures at each Material where placeholder textures are setup. /// True on success, false if the target texture could not be assigned at any of the @@ -60,13 +81,20 @@ namespace Spine.Unity { public abstract void EndCustomTextureLoading (); public abstract bool HasPlaceholderAssigned (Material material); public abstract void RequestLoadMaterialTextures (Material material, ref Material overrideMaterial); + public abstract void RequestLoadTexture (Texture placeholderTexture, ref Texture replacementTexture, + System.Action onTextureLoaded = null); public abstract void Clear (bool clearAtlasAsset = false); #region Event delegates public delegate void TextureLoadDelegate (OnDemandTextureLoader loader, Material material, int textureIndex); + protected event TextureLoadDelegate onTextureRequested; protected event TextureLoadDelegate onTextureLoaded; protected event TextureLoadDelegate onTextureUnloaded; + public event TextureLoadDelegate TextureRequested { + add { onTextureRequested += value; } + remove { onTextureRequested -= value; } + } public event TextureLoadDelegate TextureLoaded { add { onTextureLoaded += value; } remove { onTextureLoaded -= value; } @@ -76,6 +104,10 @@ namespace Spine.Unity { remove { onTextureUnloaded -= value; } } + protected void OnTextureRequested (Material material, int textureIndex) { + if (onTextureRequested != null) + onTextureRequested(this, material, textureIndex); + } protected void OnTextureLoaded (Material material, int textureIndex) { if (onTextureLoaded != null) onTextureLoaded(this, material, textureIndex); diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs index 92871e4c3..ff963b8c3 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs @@ -35,6 +35,8 @@ #define HAS_CULL_TRANSPARENT_MESH #endif +#define SPINE_OPTIONAL_ON_DEMAND_LOADING + using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; @@ -821,14 +823,22 @@ namespace Spine.Unity { else canvasRenderer.SetMesh(null); + bool assignTexture = false; if (currentInstructions.submeshInstructions.Count > 0) { Material material = currentInstructions.submeshInstructions.Items[0].material; if (material != null && baseTexture != material.mainTexture) { baseTexture = material.mainTexture; if (overrideTexture == null && assignAtCanvasRenderer) - canvasRenderer.SetTexture(this.mainTexture); + assignTexture = true; } } + +#if SPINE_OPTIONAL_ON_DEMAND_LOADING + if (Application.isPlaying) + HandleOnDemandLoading(); +#endif + if (assignTexture) + canvasRenderer.SetTexture(this.mainTexture); } protected void UpdateMaterialsMultipleCanvasRenderers (SkeletonRendererInstruction currentInstructions) { @@ -899,7 +909,6 @@ namespace Spine.Unity { bool pmaVertexColors = meshGenerator.settings.pmaVertexColors; Material[] usedMaterialItems = usedMaterials.Items; Texture[] usedTextureItems = usedTextures.Items; - bool assignAtCanvasRenderer = (assignMeshOverrideSingle == null || !disableMeshAssignmentOnOverride); for (int i = 0; i < submeshCount; i++) { SubmeshInstruction submeshInstructionItem = currentInstructions.submeshInstructions.Items[i]; meshGenerator.Begin(); @@ -932,13 +941,51 @@ namespace Spine.Unity { #endif } canvasRenderer.materialCount = 1; - if (assignAtCanvasRenderer) - canvasRenderer.SetMaterial(usedMaterialItems[i], usedTextureItems[i]); } + +#if SPINE_OPTIONAL_ON_DEMAND_LOADING + if (Application.isPlaying) + HandleOnDemandLoading(); +#endif + bool assignAtCanvasRenderer = (assignMeshOverrideSingle == null || !disableMeshAssignmentOnOverride); + if (assignAtCanvasRenderer) { + for (int i = 0; i < submeshCount; i++) { + CanvasRenderer canvasRenderer = canvasRenderers[i]; + canvasRenderer.SetMaterial(usedMaterialItems[i], usedTextureItems[i]); + } + } + if (assignMeshOverrideMultiple != null) assignMeshOverrideMultiple(submeshCount, meshesItems, usedMaterialItems, usedTextureItems); } +#if SPINE_OPTIONAL_ON_DEMAND_LOADING + void HandleOnDemandLoading () { + foreach (AtlasAssetBase atlasAsset in skeletonDataAsset.atlasAssets) { + if (atlasAsset.TextureLoadingMode != AtlasAssetBase.LoadingMode.Normal) { + atlasAsset.BeginCustomTextureLoading(); + + if (!this.allowMultipleCanvasRenderers) { + Texture loadedTexture = null; + atlasAsset.RequireTextureLoaded(this.mainTexture, ref loadedTexture, null); + if (loadedTexture) + this.baseTexture = loadedTexture; + } + else { + Texture[] textureItems = usedTextures.Items; + for (int i = 0, count = usedTextures.Count; i < count; ++i) { + Texture loadedTexture = null; + atlasAsset.RequireTextureLoaded(textureItems[i], ref loadedTexture, null); + if (loadedTexture) + usedTextures.Items[i] = loadedTexture; + } + } + atlasAsset.EndCustomTextureLoading(); + } + } + } +#endif + protected void EnsureCanvasRendererCount (int targetCount) { #if UNITY_EDITOR RemoveNullCanvasRenderers(); diff --git a/spine-unity/Assets/Spine/package.json b/spine-unity/Assets/Spine/package.json index 039bc6651..a21deed83 100644 --- a/spine-unity/Assets/Spine/package.json +++ b/spine-unity/Assets/Spine/package.json @@ -2,7 +2,7 @@ "name": "com.esotericsoftware.spine.spine-unity", "displayName": "spine-unity Runtime", "description": "This plugin provides the spine-unity runtime core.", - "version": "4.1.28", + "version": "4.1.29", "unity": "2018.3", "author": { "name": "Esoteric Software", diff --git a/spine-unity/Modules/com.esotericsoftware.spine.addressables/Editor/AddressablesTextureLoaderInspector.cs b/spine-unity/Modules/com.esotericsoftware.spine.addressables/Editor/AddressablesTextureLoaderInspector.cs index dcb3bf2a4..f0af3df67 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.addressables/Editor/AddressablesTextureLoaderInspector.cs +++ b/spine-unity/Modules/com.esotericsoftware.spine.addressables/Editor/AddressablesTextureLoaderInspector.cs @@ -47,9 +47,10 @@ namespace Spine.Unity.Editor { [CustomEditor(typeof(AddressablesTextureLoader)), CanEditMultipleObjects] public class AddressablesTextureLoaderInspector : GenericTextureLoaderInspector { - public string LoaderSuffix { get { return "_Addressable"; } } public class AddressablesMethodImplementations : StaticMethodImplementations { + public override string LoaderSuffix { get { return "_Addressable"; } } + public override GenericTextureLoader GetOrCreateLoader (string loaderPath) { AddressablesTextureLoader loader = AssetDatabase.LoadAssetAtPath(loaderPath); if (loader == null) { diff --git a/spine-unity/Modules/com.esotericsoftware.spine.addressables/Runtime/AddressablesTextureLoader.cs b/spine-unity/Modules/com.esotericsoftware.spine.addressables/Runtime/AddressablesTextureLoader.cs index be137bb4b..3411087f2 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.addressables/Runtime/AddressablesTextureLoader.cs +++ b/spine-unity/Modules/com.esotericsoftware.spine.addressables/Runtime/AddressablesTextureLoader.cs @@ -76,13 +76,17 @@ namespace Spine.Unity { [System.Serializable] public class AddressablesTextureLoader : GenericOnDemandTextureLoader { public override void CreateTextureRequest (AddressableTextureReference targetReference, - MaterialOnDemandData materialData, int textureIndex, Material materialToUpdate) { + MaterialOnDemandData materialData, int textureIndex, Material materialToUpdate, + System.Action onTextureLoaded) { + OnTextureRequested(materialToUpdate, textureIndex); materialData.textureRequests[textureIndex].handle = targetReference.assetReference.LoadAssetAsync(); materialData.textureRequests[textureIndex].handle.Completed += (obj) => { if (obj.Status == AsyncOperationStatus.Succeeded) { - materialToUpdate.mainTexture = (Texture)targetReference.assetReference.Asset; + Texture loadedTexture = (Texture)targetReference.assetReference.Asset; + materialToUpdate.mainTexture = loadedTexture; OnTextureLoaded(materialToUpdate, textureIndex); + if (onTextureLoaded != null) onTextureLoaded(loadedTexture); } }; } diff --git a/spine-unity/Modules/com.esotericsoftware.spine.addressables/package.json b/spine-unity/Modules/com.esotericsoftware.spine.addressables/package.json index 56662e150..f2ae6d3e0 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.addressables/package.json +++ b/spine-unity/Modules/com.esotericsoftware.spine.addressables/package.json @@ -2,7 +2,7 @@ "name": "com.esotericsoftware.spine.addressables", "displayName": "Spine Addressables Extensions [Experimental]", "description": "This experimental plugin provides integration of Addressables on-demand texture loading for the spine-unity runtime.\nPlease be sure to test this package first and create backups of your project before using.\n\nUsage: First declare your target Material textures as addressable. Then select the SpineAtlasAsset, right-click the SpineAtlasAsset Inspector heading and select 'Add Addressables Loader'. This generates an 'AddressableTextureLoader' asset providing configuration parameters and sets up low-resolution placeholder textures which are automatically assigned in a pre-build step when building your game executable.\n\nPrerequisites:\nIt requires a working installation of the spine-unity runtime (via the spine-unity unitypackage), version 4.1.\n(See http://esotericsoftware.com/git/spine-runtimes/spine-unity)", - "version": "4.1.0-preview.1", + "version": "4.1.0-preview.2", "unity": "2018.3", "author": { "name": "Esoteric Software", diff --git a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/GenericOnDemandTextureLoaderInspector.cs b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/GenericOnDemandTextureLoaderInspector.cs index 9744617fc..786e1b8b7 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/GenericOnDemandTextureLoaderInspector.cs +++ b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/GenericOnDemandTextureLoaderInspector.cs @@ -37,6 +37,7 @@ using System; using System.Collections.Generic; +using System.Linq; using UnityEditor; using UnityEngine; @@ -92,7 +93,7 @@ namespace Spine.Unity.Editor { /// When set to e.g. "_Addressable", the loader asset created for /// the "Skeleton_Atlas" asset is named "Skeleton_Addressable". /// - public string LoaderSuffix { get; } + public virtual string LoaderSuffix { get { return "_Loader"; } } public abstract bool SetupOnDemandLoadingReference ( ref TargetReference targetTextureReference, Texture targetTexture); @@ -237,7 +238,7 @@ namespace Spine.Unity.Editor { #if NEWPLAYMODECALLBACKS static void OnPlaymodeChanged (PlayModeStateChange mode) { - bool assignTargetTextures = mode == PlayModeStateChange.ExitingPlayMode; + bool assignTargetTextures = mode == PlayModeStateChange.EnteredEditMode; #else static void OnPlaymodeChanged () { bool assignTargetTextures = !Application.isPlaying; @@ -259,14 +260,23 @@ namespace Spine.Unity.Editor { public static void AssignTargetTexturesAtLoader (OnDemandTextureLoader loader) { List placeholderMaterials; + List nullTextureMaterials; bool anyPlaceholdersAssigned = loader.HasPlaceholderTexturesAssigned(out placeholderMaterials); - if (anyPlaceholdersAssigned) { - Debug.Log("OnDemandTextureLoader detected placeholders assigned at one or more materials. Resetting to target textures.", loader); + bool anyMaterialNull = loader.HasNullMainTexturesAssigned(out nullTextureMaterials); + if (anyPlaceholdersAssigned || anyMaterialNull) { + Debug.Log("OnDemandTextureLoader detected placeholders assigned or null main textures at one or more materials. Resetting to target textures.", loader); AssetDatabase.StartAssetEditing(); IEnumerable modifiedMaterials; loader.AssignTargetTextures(out modifiedMaterials); - foreach (Material placeholderMaterial in placeholderMaterials) { - EditorUtility.SetDirty(placeholderMaterial); + if (placeholderMaterials != null) { + foreach (Material placeholderMaterial in placeholderMaterials) { + EditorUtility.SetDirty(placeholderMaterial); + } + } + if (nullTextureMaterials != null) { + foreach (Material nullTextureMaterial in nullTextureMaterials) { + EditorUtility.SetDirty(nullTextureMaterial); + } } AssetDatabase.StopAssetEditing(); AssetDatabase.SaveAssets(); diff --git a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Runtime/GenericOnDemandTextureLoader.cs b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Runtime/GenericOnDemandTextureLoader.cs index 47a02f031..5025701b5 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Runtime/GenericOnDemandTextureLoader.cs +++ b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Runtime/GenericOnDemandTextureLoader.cs @@ -164,6 +164,7 @@ namespace Spine.Unity { if (placeholderMaterials == null) placeholderMaterials = new List(); placeholderMaterials.Add(material); } + materialIndex++; } return anyPlaceholderAssigned; } @@ -180,8 +181,7 @@ namespace Spine.Unity { atlasAsset, i + 1, targetMaterial), this); return false; } - Material ignoredArgument = null; - RequestLoadMaterialTextures(targetMaterial, ref ignoredArgument); + AssignTargetTextures(targetMaterial, i); ++i; } modifiedMaterials = atlasAsset.Materials; @@ -223,7 +223,7 @@ namespace Spine.Unity { int foundMaterialIndex = Array.FindIndex(placeholderMap, entry => entry.textures[textureIndex].placeholderTexture == currentTexture); if (foundMaterialIndex >= 0) - RequestLoadTexture(material, foundMaterialIndex, textureIndex); + RequestLoadTexture(material, foundMaterialIndex, textureIndex, null); int loadedMaterialIndex = Array.FindIndex(loadedDataAtMaterial, entry => entry.textureRequests[textureIndex].WasRequested && @@ -232,33 +232,69 @@ namespace Spine.Unity { loadedDataAtMaterial[loadedMaterialIndex].lastFrameRequested = Time.frameCount; } - protected virtual void RequestLoadTexture (Material material, int materialIndex, int textureIndex) { + public override void RequestLoadTexture (Texture placeholderTexture, ref Texture replacementTexture, + System.Action onTextureLoaded = null) { + + if (placeholderTexture == null) return; + + Texture currentTexture = placeholderTexture; + int textureIndex = 0; // Todo: currently only main texture is supported. + + int foundMaterialIndex = Array.FindIndex(placeholderMap, entry => entry.textures[textureIndex].placeholderTexture == currentTexture); + if (foundMaterialIndex >= 0) { + Material material = atlasAsset.Materials.ElementAt(foundMaterialIndex); + Texture loadedTexture = RequestLoadTexture(material, foundMaterialIndex, textureIndex, onTextureLoaded); + if (loadedTexture != null) + replacementTexture = loadedTexture; + } + + int loadedMaterialIndex = Array.FindIndex(loadedDataAtMaterial, entry => + entry.textureRequests[textureIndex].WasRequested && + entry.textureRequests[textureIndex].IsTarget(placeholderTexture)); + if (loadedMaterialIndex >= 0) + loadedDataAtMaterial[loadedMaterialIndex].lastFrameRequested = Time.frameCount; + } + + protected void AssignTargetTextures (Material material, int materialIndex) { + int textureIndex = 0; // Todo: currently only main texture is supported. + RequestLoadTexture(material, materialIndex, textureIndex, null); + } + + protected virtual Texture RequestLoadTexture (Material material, int materialIndex, int textureIndex, + System.Action onTextureLoaded) { + PlaceholderTextureMapping[] placeholderTextures = placeholderMap[materialIndex].textures; TargetReference targetReference = placeholderTextures[textureIndex].targetTextureReference; loadedDataAtMaterial[materialIndex].lastFrameRequested = Time.frameCount; #if UNITY_EDITOR if (!Application.isPlaying) { - if (targetReference.EditorTexture != null) + if (targetReference.EditorTexture != null) { material.mainTexture = targetReference.EditorTexture; - return; + if (onTextureLoaded != null) onTextureLoaded(targetReference.EditorTexture); + } + return targetReference.EditorTexture; } #endif MaterialOnDemandData materialData = loadedDataAtMaterial[materialIndex]; if (materialData.textureRequests[textureIndex].WasRequested) { Texture loadedTexture = GetAlreadyLoadedTexture(materialIndex, textureIndex); - if (loadedTexture != null) + if (loadedTexture != null) { material.mainTexture = loadedTexture; - return; + if (onTextureLoaded != null) onTextureLoaded(loadedTexture); + } + return loadedTexture; } - CreateTextureRequest(targetReference, materialData, textureIndex, material); + CreateTextureRequest(targetReference, materialData, textureIndex, material, onTextureLoaded); + return null; } public abstract Texture GetAlreadyLoadedTexture (int materialIndex, int textureIndex); public abstract void CreateTextureRequest (TargetReference targetReference, - MaterialOnDemandData materialData, int textureIndex, Material materialToUpdate); + MaterialOnDemandData materialData, int textureIndex, Material materialToUpdate, + System.Action onTextureLoaded); public virtual void UnloadUnusedTextures () { int currentFrameCount = Time.frameCount; diff --git a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/package.json b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/package.json index 5682f3443..88ff77e98 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/package.json +++ b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/package.json @@ -2,7 +2,7 @@ "name": "com.esotericsoftware.spine.on-demand-loading", "displayName": "Spine On-Demand Loading Extensions [Experimental]", "description": "This experimental plugin provides a generic basic implementation of on-demand texture loading for the spine-unity runtime. You might want to use the available com.esotericsoftware.spine.addressables package which depends on this package.\nPlease be sure to test this package first and create backups of your project before using.\n\nPrerequisites:\nIt requires a working installation of the spine-unity runtime (via the spine-unity unitypackage), version 4.1.\n(See http://esotericsoftware.com/git/spine-runtimes/spine-unity)", - "version": "4.1.0", + "version": "4.1.0-preview.2", "unity": "2018.3", "author": { "name": "Esoteric Software",