Forest_Client/Forest/Assets/PhxhSDK/Phxh/AssetManager.cs

145 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using Cysharp.Threading.Tasks;
using PhxhSDK.Res;
using UnityEngine;
using YooAsset;
using Object = UnityEngine.Object;
namespace PhxhSDK
{
/// <summary>
/// 提供统一的资源管理
/// 注意此类型中的API都要成对使用每一次load(如preload,load,loadAsync)都要对应一次unload
/// </summary>
public class AssetManager : Singlenton<AssetManager>
{
private IResLoader _resLoader;
public bool isInited;
public AssetManager()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
{
// 没有运行的时候,使用假的加载器
_resLoader = new ResLoader_Fake();
_Init();
return;
}
#endif
#if USE_YOO
_resLoader = new ResLoader_YooAsset();
#else
_resLoader = new ResLoader_Addressables();
#endif
_Init();
}
private async void _Init()
{
isInited = false;
await _resLoader.Init();
isInited = true;
}
#region API
/// <summary>
/// 预加载,不需要资源的时候一定要调用<see cref="Unload"/>,
/// 获取预加载的资源请调用<see cref="GetPreLoadResult{T}"/>,不能使用<see cref="LoadAsset{T}"/>或者<see cref="LoadAssetAsync{T}"/>,会产生多次引用计数
/// </summary>
/// <param name="path"></param>
/// <typeparam name="T"></typeparam>
public async UniTask PostPreload<T>(string path) where T : Object
{
await _resLoader.PostPreload<T>(path);
}
public void PostPreload(string path)
{
PostPreload<Object>(path).Forget();
}
/// <summary>
/// 获取预加载的结果
/// </summary>
/// <param name="path"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetPreLoadResult<T>(string path) where T : Object
{
return _resLoader.GetPreLoadResult<T>(path);
}
/// <summary>
/// 等待所有预加载结束
/// </summary>
public async UniTask WaitAllPreloads()
{
await _resLoader.WaitAllPreloads();
}
/// <summary>
/// 同步加载资源,不需要资源的时候请调用<see cref="Unload"/>
/// </summary>
/// <param name="path"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T LoadAsset<T>(string path) where T : Object
{
LoadAssetAsync<T>(path).Forget();
return _resLoader.GetPreLoadResult<T>(path);
}
/// <summary>
/// 异步加载资源,不需要资源的时候请调用<see cref="Unload"/>
/// </summary>
/// <param name="path">路径</param>
/// <param name="onLoaded">加载完成后的回调</param>
/// <typeparam name="T">资源类型</typeparam>
/// <returns></returns>
public async UniTask<T> LoadAssetAsync<T>(string path, Action<T> onLoaded = null) where T : Object
{
while (!isInited)
{
await UniTask.Delay(10);
}
return await _resLoader.LoadAsync<T>(path, onLoaded);
}
/// <summary>
/// 同步加载原生文件
/// </summary>
/// <param name="lacation"></param>
/// <returns></returns>
public RawFileHandle LoadRawFileAsync(string lacation)
{
return _resLoader.LoadRawFileAsync(lacation);
}
public void Unload(string path, bool force = false)
{
_resLoader.Unload(path, force);
}
/// <summary>
/// 路径是否存在
/// </summary>
/// <param name="path">路径</param>
/// <param name="location">资源的location</param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public bool CanLocateAsset<T>(string path) where T : UnityEngine.Object
{
return _resLoader.CanLocate<T>(path);
}
#endregion
}
}