257 lines
8.2 KiB
C#
257 lines
8.2 KiB
C#
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using JetBrains.Annotations;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.SceneManagement;
|
|||
|
using YooAsset;
|
|||
|
using Object = UnityEngine.Object;
|
|||
|
|
|||
|
namespace Game
|
|||
|
{
|
|||
|
public enum LoadType
|
|||
|
{
|
|||
|
Editor,
|
|||
|
Single,
|
|||
|
Online
|
|||
|
}
|
|||
|
|
|||
|
public class ResourceManager : ManagerBase, IResourceManager
|
|||
|
{
|
|||
|
private static ResourceManager instance;
|
|||
|
|
|||
|
public static ResourceManager Instance
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (instance == null)
|
|||
|
{
|
|||
|
instance = new ResourceManager();
|
|||
|
}
|
|||
|
|
|||
|
return instance;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private List<string> _packageName = new List<string>();
|
|||
|
private ResourcePackage _defaultPackage;
|
|||
|
private Dictionary<string, AssetHandle> _assetHandles = new Dictionary<string, AssetHandle>();
|
|||
|
private Dictionary<string, SceneHandle> _sceneHandles = new Dictionary<string, SceneHandle>();
|
|||
|
private Dictionary<string, GameObject> _gameObjects = new Dictionary<string, GameObject>();
|
|||
|
|
|||
|
protected override void OnInit()
|
|||
|
{
|
|||
|
base.OnInit();
|
|||
|
YooAssets.Initialize();
|
|||
|
_defaultPackage = YooAssets.CreatePackage("DefaultPackage");
|
|||
|
YooAssets.SetDefaultPackage(_defaultPackage);
|
|||
|
}
|
|||
|
|
|||
|
protected override void OnUpdate(float dateTime)
|
|||
|
{
|
|||
|
base.OnUpdate(dateTime);
|
|||
|
}
|
|||
|
|
|||
|
public void LoadPackage(string packageName)
|
|||
|
{
|
|||
|
if (_packageName.Contains(packageName)) return;
|
|||
|
YooAssets.CreatePackage(packageName);
|
|||
|
_packageName.Add(packageName);
|
|||
|
}
|
|||
|
|
|||
|
public void UnloadPackage(string packageName)
|
|||
|
{
|
|||
|
var resourcePackage = YooAssets.GetPackage(packageName);
|
|||
|
if (resourcePackage != null)
|
|||
|
{
|
|||
|
resourcePackage.UnloadUnusedAssets();
|
|||
|
YooAssets.DestroyPackage(packageName);
|
|||
|
_packageName.Remove(packageName);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public IEnumerator InitLoadMode(LoadType _loadType)
|
|||
|
{
|
|||
|
switch (_loadType)
|
|||
|
{
|
|||
|
case LoadType.Editor:
|
|||
|
#if UNITY_EDITOR
|
|||
|
Debug.Log("EditorMode-Loading");
|
|||
|
yield return InitializeYooAsset_Editor();
|
|||
|
#endif
|
|||
|
break;
|
|||
|
case LoadType.Single:
|
|||
|
Debug.Log("SingleMode-Loading");
|
|||
|
yield return InitializeYooAsset_Single();
|
|||
|
break;
|
|||
|
case LoadType.Online:
|
|||
|
break;
|
|||
|
default:
|
|||
|
throw new ArgumentOutOfRangeException();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#if UNITY_EDITOR
|
|||
|
private IEnumerator InitializeYooAsset_Editor()
|
|||
|
{
|
|||
|
var initParameters = new EditorSimulateModeParameters();
|
|||
|
var simulateManifestFilePath =
|
|||
|
EditorSimulateModeHelper.SimulateBuild(EDefaultBuildPipeline.BuiltinBuildPipeline, "DefaultPackage");
|
|||
|
initParameters.SimulateManifestFilePath = simulateManifestFilePath;
|
|||
|
yield return _defaultPackage.InitializeAsync(initParameters);
|
|||
|
Debug.Log("EditorMode-LoadFinish");
|
|||
|
}
|
|||
|
#endif
|
|||
|
|
|||
|
private IEnumerator InitializeYooAsset_Single()
|
|||
|
{
|
|||
|
var initParameters = new OfflinePlayModeParameters();
|
|||
|
yield return _defaultPackage.InitializeAsync(initParameters);
|
|||
|
Debug.Log("SingleMode-LoadFinish");
|
|||
|
}
|
|||
|
|
|||
|
// private IEnumerator InitializeYooAsset_Online()
|
|||
|
// {
|
|||
|
// // 注意:GameQueryServices.cs 太空战机的脚本类,详细见StreamingAssetsHelper.cs
|
|||
|
// string defaultHostServer = "http://127.0.0.1/CDN/Android/v1.0";
|
|||
|
// string fallbackHostServer = "http://127.0.0.1/CDN/Android/v1.0";
|
|||
|
// var initParameters = new HostPlayModeParameters();
|
|||
|
// initParameters.BuildinQueryServices = new GameQueryServices();
|
|||
|
// initParameters.DecryptionServices = new FileOffsetDecryption();
|
|||
|
// initParameters.RemoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
|
|||
|
// var initOperation = package.InitializeAsync(initParameters);
|
|||
|
// yield return initOperation;
|
|||
|
//
|
|||
|
// if (initOperation.Status == EOperationStatus.Succeed)
|
|||
|
// {
|
|||
|
// Debug.Log("资源包初始化成功!");
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// Debug.LogError($"资源包初始化失败:{initOperation.Error}");
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
public AssetHandle LoadAssetSync(string name)
|
|||
|
{
|
|||
|
if (_assetHandles.TryGetValue(name, out var assetHandle))
|
|||
|
{
|
|||
|
return assetHandle;
|
|||
|
}
|
|||
|
|
|||
|
var loadAssetSync = YooAssets.LoadAssetSync(name);
|
|||
|
_assetHandles.Add(name, loadAssetSync);
|
|||
|
return loadAssetSync;
|
|||
|
}
|
|||
|
|
|||
|
public GameObject LoadGameObjectSync(string name)
|
|||
|
{
|
|||
|
var loadAssetSync = LoadAssetSync(name);
|
|||
|
var assetObject = loadAssetSync.AssetObject as GameObject;
|
|||
|
var gameObject = Object.Instantiate(assetObject);
|
|||
|
|
|||
|
return gameObject;
|
|||
|
}
|
|||
|
|
|||
|
public async void LoadSceneAsync(string name, LoadSceneMode loadSceneMode = LoadSceneMode.Single,
|
|||
|
bool suspendLoad = false)
|
|||
|
{
|
|||
|
// if (_assetHandles.TryGetValue(name, out var assetHandle))
|
|||
|
// {
|
|||
|
// return assetHandle;
|
|||
|
// }
|
|||
|
|
|||
|
var loadSceneAsync = YooAssets.LoadSceneAsync(name, loadSceneMode, suspendLoad);
|
|||
|
_sceneHandles.Add(name, loadSceneAsync);
|
|||
|
await loadSceneAsync.Task;
|
|||
|
Debug.Log($"{name}场景加载成功!");
|
|||
|
}
|
|||
|
|
|||
|
public async void UnloadSceneAsync(string name, LoadSceneMode loadSceneMode = LoadSceneMode.Single,
|
|||
|
bool suspendLoad = false)
|
|||
|
{
|
|||
|
if (_sceneHandles.TryGetValue(name, out var handle))
|
|||
|
{
|
|||
|
await handle.UnloadAsync().Task;
|
|||
|
Debug.Log($"{name}场景卸载成功!");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public async Task<Object> LoadAssetAsync(string name)
|
|||
|
{
|
|||
|
if (_assetHandles.TryGetValue(name, out var assetHandle))
|
|||
|
{
|
|||
|
return assetHandle.AssetObject;
|
|||
|
}
|
|||
|
|
|||
|
var loadAssetSync = YooAssets.LoadAssetAsync(name);
|
|||
|
await loadAssetSync.Task;
|
|||
|
_assetHandles.Add(name, loadAssetSync);
|
|||
|
return loadAssetSync.AssetObject;
|
|||
|
}
|
|||
|
|
|||
|
public async Task<GameObject> LoadGameObjectAsync(string name)
|
|||
|
{
|
|||
|
var loadAssetSync = await LoadAssetAsync(name);
|
|||
|
var assetObject = loadAssetSync as GameObject;
|
|||
|
var gameObject = Object.Instantiate(assetObject);
|
|||
|
|
|||
|
return gameObject;
|
|||
|
}
|
|||
|
|
|||
|
public void Release(string name)
|
|||
|
{
|
|||
|
if (_assetHandles.TryGetValue(name, out var assetHandle))
|
|||
|
{
|
|||
|
assetHandle.Release();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
protected override void OnDispose()
|
|||
|
{
|
|||
|
base.OnDispose();
|
|||
|
while (_packageName.Count > 0)
|
|||
|
{
|
|||
|
var packageName = _packageName[0];
|
|||
|
UnloadPackage(packageName);
|
|||
|
_packageName.Remove(packageName);
|
|||
|
}
|
|||
|
|
|||
|
foreach (var handle in _sceneHandles.Values)
|
|||
|
{
|
|||
|
handle.UnloadAsync();
|
|||
|
}
|
|||
|
|
|||
|
_sceneHandles = null;
|
|||
|
_packageName = null;
|
|||
|
_gameObjects = null;
|
|||
|
_assetHandles = null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// public class GO_S
|
|||
|
// {
|
|||
|
// public string name;
|
|||
|
// private AssetHandle AssetHandle;
|
|||
|
// private GameObject go;
|
|||
|
//
|
|||
|
// public GO_S(string name, AssetHandle assetHandle, GameObject go)
|
|||
|
// {
|
|||
|
// this.name = name;
|
|||
|
// AssetHandle = assetHandle;
|
|||
|
// this.go = go;
|
|||
|
// }
|
|||
|
//
|
|||
|
// public void Release()
|
|||
|
// {
|
|||
|
// AssetHandle.Release();
|
|||
|
// GameObject.Destroy(go);
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
public interface IResourceManager
|
|||
|
{
|
|||
|
AssetHandle LoadAssetSync(string name);
|
|||
|
}
|
|||
|
}
|