Files
aibis-dream/Assets/Scripts/SceneManagement/Day2Sleep/Day2SleepSpriteLoader.cs
T

70 lines
2.2 KiB
C#

using System;
using System.Collections;
using AibisDream.Framework;
using UnityEngine;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace AibisDream
{
internal static class Day2SleepSpriteLoader
{
private const string DreamSpritePath = "Sprite/梦境/{0}.png";
public static string NormalizeName(string picName)
{
if (string.IsNullOrEmpty(picName)) return picName;
return picName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
? picName.Substring(0, picName.Length - 4)
: picName;
}
public static IEnumerator LoadInto(
SpriteRenderer target,
string picName,
Action<bool> completed = null,
Action<string> warning = null)
{
if (target == null)
{
completed?.Invoke(false);
yield break;
}
var key = string.Format(DreamSpritePath, NormalizeName(picName));
var handle = ResourceSystem.LoadAsync<Sprite>(key);
yield return handle;
if (handle.Status == AsyncOperationStatus.Succeeded && handle.Result != null)
{
target.sprite = handle.Result;
completed?.Invoke(true);
yield break;
}
var message = $"[Day2SleepPresentation] Failed to load sprite: {key}";
Debug.LogError(message);
warning?.Invoke(message);
completed?.Invoke(false);
}
public static IEnumerator Load(
string picName,
Action<Sprite> completed,
Action<string> warning = null)
{
var key = string.Format(DreamSpritePath, NormalizeName(picName));
var handle = ResourceSystem.LoadAsync<Sprite>(key);
yield return handle;
if (handle.Status == AsyncOperationStatus.Succeeded && handle.Result != null)
{
completed?.Invoke(handle.Result);
yield break;
}
var message = $"[Day2SleepPresentation] Failed to load sprite: {key}";
Debug.LogError(message);
warning?.Invoke(message);
completed?.Invoke(null);
}
}
}