• RuntimesUnity
  • on-demand texture loading status events/delegates

Hi Team,
I been exploring the spine on-demand texture loading in unity and its working great so far! I wanted to implement a loading indicator when displaying a low res-textures on the spine, while it fetches the high-res textures addressables and loads onto the spine. I've looked into the available TextureRequested & TextureLoaded delegates to enable/disable a loading indicator. But can those help when there are more than one individual skin texture being fetched on a same spine ? Also, how can I handled a failed case ? Or any other better way to achieve this?

Thanks!

Related Discussions
...
  • Изменено

To implement a loading indicator while fetching high-res textures for Spine in Unity, you can indeed use the TextureRequested and TextureLoaded delegates. These delegates can help you manage the loading state for multiple textures. Here’s a general approach to achieve this:

  1. Subscribe to Delegates: Subscribe to the TextureRequested and TextureLoaded delegates to show and hide the loading indicator.

  2. Track Loading State: Maintain a counter or a list to track the number of textures being loaded. Increment the counter when a texture is requested and decrement it when a texture is loaded.

  3. Show/Hide Loading Indicator: Show the loading indicator when the counter is greater than zero and hide it when the counter reaches zero.

  4. Handle Failures: Implement a timeout or error handling mechanism to hide the loading indicator if a texture fails to load.

Here’s a sample implementation:

using UnityEngine;
using Spine.Unity;

public class SpineTextureLoader : MonoBehaviour
{
    public SkeletonRenderer skeletonRenderer;
    public GameObject loadingIndicator;

    private int texturesLoading = 0;

    void OnEnable()
    {
        OnDemandTextureLoader loader = skeletonRenderer.SkeletonDataAsset.atlasAssets[0].OnDemandTextureLoader;
        loader.TextureRequested += HandleTextureRequested;
        loader.TextureLoaded += HandleTextureLoaded;
    }

    void OnDisable()
    {
        OnDemandTextureLoader loader = skeletonRenderer.SkeletonDataAsset.atlasAssets[0].OnDemandTextureLoader;
        loader.TextureRequested -= HandleTextureRequested;
        loader.TextureLoaded -= HandleTextureLoaded;
    }

    private void HandleTextureRequested(OnDemandTextureLoader loader, Material material, int textureIndex)
    {
        texturesLoading++;
        UpdateLoadingIndicator();
    }

    private void HandleTextureLoaded(OnDemandTextureLoader loader, Material material, int textureIndex)
    {
        texturesLoading--;
        UpdateLoadingIndicator();
    }

    private void UpdateLoadingIndicator()
    {
        loadingIndicator.SetActive(texturesLoading > 0);
    }

    private void HandleTextureLoadFailed()
    {
        // Implement your error handling logic here
        texturesLoading = Mathf.Max(0, texturesLoading - 1);
        UpdateLoadingIndicator();
    }
}

Handling Failures

To handle failed cases, you can implement a timeout mechanism or listen for error events if available. For example, you can start a coroutine when a texture is requested and stop it when the texture is loaded. If the coroutine times out, you can assume the texture load failed and update the loading indicator accordingly.

private IEnumerator TextureLoadTimeout(float timeout)
{
    yield return new WaitForSeconds(timeout);
    if (texturesLoading > 0)
    {
        HandleTextureLoadFailed();
    }
}

Summary

You can use the TextureRequested and TextureLoaded delegates to manage a loading indicator for multiple textures. Track the number of textures being loaded and update the indicator accordingly. Implement a timeout or error handling mechanism to handle failed texture loads.

razor10 изменил(а) название на "on-demand texture loading status events/delegates".
  • Изменено

@razor10 Thanks for the feedback! Sorry that Spinebot got a few calls wrong, there is no SkeletonRenderer.OnTextureRequested or .OnTextureLoaded callback, these callbacks are located at the OnDemandTextureLoader, and the callbacks are TextureRequested, TextureLoaded and TextureUnloaded. I have corrected the post above. Without storing a direct asset reference, you could access the OnDemandTextureLoader from the SkeletonRenderer via skeletonRenderer.SkeletonDataAsset.atlasAssets[0].OnDemandTextureLoader.

You are correct that a TextureLoadFailed callback is currently missing, thanks for reporting! We will add such a callback and let you know when the commit is up.

@razor10 A new spine-unity 4.2 package with the added TextureLoadFailed callback delegate is available for download:
https://esotericsoftware.com/spine-unity-download
Thanks again for reporting! Please let us know if you are missing anything else.

  • razor10 ответили на это сообщение.
  • razor10 оценил это.

    Harald Wonderful! Thank you for your support
    minor : finding made in above code generate by spinebot. Guessing it must be because of latest updates made
    It generated the method with parameters as HandleTextureRequested(Material material, string textureName) but the structure it requires is found to be HandleTextureRequested(OnDemandTextureLoader loader, Material material, int textureIndex). it'd be great if you could correct this too on the response above so it can help any other referring that 🙂

    • Harald ответили на это сообщение.

      razor10 Thanks for reporting! I have now corrected the method signature in Spinebot's posting as well.