You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

145 lines
3.9 KiB

using System.Collections;
using UnityEngine;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.U2D;
public class ExtendSpriteRenderer : MonoBehaviour
{
[SerializeField] SpriteRenderer spRenderer;
string recentLoadedSpriteName;
Sprite recentLoadedSprite;
string recentLoadedAtlasPath;
string recentLoadedTexInAtlasName;
SpriteAtlas recentLoadedAtlas;
public SpriteRenderer RendererComponent => spRenderer;
private void Awake()
{
if (spRenderer is null)
spRenderer = GetComponent<SpriteRenderer>();
}
private void OnDestroy()
{
if (recentLoadedSprite != null)
AddressableMgr.ReleaseAsset(recentLoadedSprite);
if (recentLoadedAtlasPath != null)
AddressableMgr.ReleaseAsset(recentLoadedAtlas);
}
public void SetImageAsync(string path)
{
CoroutineHandler.Instance.StartCoroutine(LoadImageAsync(path));
}
IEnumerator LoadImageAsync(string path)
{
if (recentLoadedSpriteName == path)
yield break;
if (recentLoadedSprite != null)
{
AddressableMgr.ReleaseAsset(recentLoadedSprite);
recentLoadedSprite = null;
}
recentLoadedSpriteName = path;
if (!AddressableMgr.LoadAssetAsync(path, out AsyncOperationHandle<Sprite> handler))
yield break;
yield return handler;
if (handler.Status != AsyncOperationStatus.Succeeded)
yield break;
if (recentLoadedSpriteName == path)
{
RendererComponent.sprite = handler.Result;
recentLoadedSprite = handler.Result;
}
else
{
AddressableMgr.ReleaseAsset(handler.Result);
}
}
public void ReleaseAsyncImage()
{
if (recentLoadedSprite != null)
{
AddressableMgr.ReleaseAsset(recentLoadedSprite);
RendererComponent.sprite = null;
recentLoadedSpriteName = null;
recentLoadedSprite = null;
}
}
public void SetImageInAtlasAsync(string atlasPath, string imgName)
{
CoroutineHandler.Instance.StartCoroutine(LoadImageInAtlasAsync(atlasPath, imgName));
}
IEnumerator LoadImageInAtlasAsync(string atlasPath, string imgName)
{
if (recentLoadedAtlasPath == atlasPath)
{
if (recentLoadedTexInAtlasName != imgName)
{
RendererComponent.sprite = recentLoadedAtlas.GetSprite(imgName);
recentLoadedTexInAtlasName = imgName;
}
yield break;
}
if (recentLoadedAtlas != null)
{
AddressableMgr.ReleaseAsset(recentLoadedAtlas);
recentLoadedAtlas = null;
}
recentLoadedAtlasPath = atlasPath;
recentLoadedTexInAtlasName = imgName;
if (!AddressableMgr.LoadAssetAsync(atlasPath, out AsyncOperationHandle<SpriteAtlas> handler))
yield break;
yield return handler;
if (handler.Status != AsyncOperationStatus.Succeeded)
yield break;
if (recentLoadedAtlasPath == atlasPath && recentLoadedTexInAtlasName == imgName)
{
RendererComponent.sprite = handler.Result.GetSprite(imgName);
recentLoadedAtlas = handler.Result;
}
else
{
AddressableMgr.ReleaseAsset(handler.Result);
}
}
public void ReleaseAsyncAtlasImage()
{
if (recentLoadedAtlas != null)
{
AddressableMgr.ReleaseAsset(recentLoadedAtlas);
RendererComponent.sprite = null;
recentLoadedAtlasPath = null;
recentLoadedTexInAtlasName = null;
recentLoadedAtlas = null;
}
}
private void OnValidate()
{
if (spRenderer == null)
spRenderer = GetComponent<SpriteRenderer>();
}
}