Explorar o código

[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.

Harald Csaszar hai 1 ano
pai
achega
8bc1201b83

+ 5 - 0
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<Texture> onTextureLoaded) {
+			if (onDemandTextureLoader)
+				onDemandTextureLoader.RequestLoadTexture(placeholderTexture, ref replacementTexture, onTextureLoaded);
+		}
+
 		[SerializeField] protected LoadingMode textureLoadingMode = LoadingMode.Normal;
 		[SerializeField] protected OnDemandTextureLoader onDemandTextureLoader = null;
 #endif

+ 32 - 0
spine-unity/Assets/Spine/Runtime/spine-unity/Asset Types/OnDemandTextureLoader.cs

@@ -51,6 +51,27 @@ namespace Spine.Unity {
 		/// <param name="placeholderMaterials">A newly created list of materials which has a placeholder texture assigned.</param>
 		/// <returns>True, if any placeholder texture is assigned at a Material of the associated AtlasAssetBase.</returns>
 		public abstract bool HasPlaceholderTexturesAssigned (out List<Material> placeholderMaterials);
+
+		/// <summary>
+		/// Returns whether any main texture is null at a Material of the associated AtlasAssetBase.
+		/// </summary>
+		/// <param name="nullTextureMaterials">A newly created list of materials which has a null main texture assigned.</param>
+		/// <returns>True, if any null main texture is assigned at a Material of the associated AtlasAssetBase.</returns>
+		public virtual bool HasNullMainTexturesAssigned (out List<Material> 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<Material>();
+					nullTextureMaterials.Add(material);
+				}
+			}
+			return anyNullTexture;
+		}
+
 		/// <summary>
 		/// Assigns previously setup target textures at each Material where placeholder textures are setup.</summary>
 		/// <returns>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<Texture> 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);

+ 50 - 3
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)
+			}
+
+#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();

+ 1 - 1
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",

+ 2 - 1
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<AddressablesTextureLoader>(loaderPath);
 				if (loader == null) {

+ 6 - 2
spine-unity/Modules/com.esotericsoftware.spine.addressables/Runtime/AddressablesTextureLoader.cs

@@ -76,13 +76,17 @@ namespace Spine.Unity {
 	[System.Serializable]
 	public class AddressablesTextureLoader : GenericOnDemandTextureLoader<AddressableTextureReference, AddressableRequest> {
 		public override void CreateTextureRequest (AddressableTextureReference targetReference,
-			MaterialOnDemandData materialData, int textureIndex, Material materialToUpdate) {
+			MaterialOnDemandData materialData, int textureIndex, Material materialToUpdate,
+			System.Action<Texture> onTextureLoaded) {
 
+			OnTextureRequested(materialToUpdate, textureIndex);
 			materialData.textureRequests[textureIndex].handle = targetReference.assetReference.LoadAssetAsync<Texture>();
 			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);
 				}
 			};
 		}

+ 1 - 1
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",

+ 16 - 6
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".
 			/// </summary>
-			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<Material> placeholderMaterials;
+			List<Material> 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<Material> 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();

+ 46 - 10
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<Material>();
 					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<Texture> 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<Texture>  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<Texture> onTextureLoaded);
 
 		public virtual void UnloadUnusedTextures () {
 			int currentFrameCount = Time.frameCount;

+ 1 - 1
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",