2022-03-03 18:08:32 +08:00
|
|
|
|
# 资源加载
|
|
|
|
|
|
2022-05-08 22:21:13 +08:00
|
|
|
|
加载方法:
|
2022-03-03 18:08:32 +08:00
|
|
|
|
|
2022-05-08 22:21:13 +08:00
|
|
|
|
- YooAssets.LoadAssetSync() 同步加载资源对象
|
|
|
|
|
- YooAssets.LoadAssetAsync() 异步加载资源对象
|
2022-06-25 11:41:13 +08:00
|
|
|
|
- YooAssets.LoadSubAssetsSync() 同步加载子资源对象
|
2022-05-08 22:21:13 +08:00
|
|
|
|
- YooAssets.LoadSubAssetsAsync() 异步加载子资源对象
|
|
|
|
|
- YooAssets.LoadSceneAsync() 异步加载场景
|
|
|
|
|
- YooAssets.GetRawFileAsync() 异步获取原生文件
|
2022-03-03 18:08:32 +08:00
|
|
|
|
|
2022-06-25 11:41:13 +08:00
|
|
|
|
**统一约定**
|
|
|
|
|
|
|
|
|
|
location为资源的定位地址,也是加载资源对象的唯一标识符。
|
|
|
|
|
|
|
|
|
|
- DefaultLocationServices 默认资源定位服务,location代表的是资源对象的相对路径。
|
|
|
|
|
|
2022-04-14 12:38:20 +08:00
|
|
|
|
|
2022-06-25 11:41:13 +08:00
|
|
|
|
- AddressLocationServices 可寻址资源定位服务,location代表的是资源对象可寻址地址。
|
2022-05-04 00:12:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**注意**:以下范例执行环境是在DefaultLocationServices下。
|
|
|
|
|
|
2022-03-03 18:08:32 +08:00
|
|
|
|
**加载路径的匹配方式**
|
|
|
|
|
|
|
|
|
|
````C#
|
|
|
|
|
// 不带扩展名的模糊匹配
|
|
|
|
|
YooAssets.LoadAssetAsync<AudioClip>("Audio/bgMusic");
|
|
|
|
|
|
|
|
|
|
// 带扩展名的精准匹配
|
|
|
|
|
YooAssets.LoadAssetAsync<AudioClip>("Audio/bgMusic.mp3");
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
**异步加载范例**
|
|
|
|
|
|
|
|
|
|
````C#
|
|
|
|
|
// 委托加载方式
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
2022-03-03 19:13:07 +08:00
|
|
|
|
AssetOperationHandle handle = YooAssets.LoadAssetAsync<AudioClip>("Audio/bgMusic.mp3");
|
|
|
|
|
handle.Completed += Handle_Completed;
|
2022-03-03 18:08:32 +08:00
|
|
|
|
}
|
|
|
|
|
void Handle_Completed(AssetOperationHandle handle)
|
|
|
|
|
{
|
2022-03-03 19:13:07 +08:00
|
|
|
|
AudioClip audioClip = handle.AssetObject as AudioClip;
|
2022-03-03 18:08:32 +08:00
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
````C#
|
|
|
|
|
// 协程加载方式
|
2022-03-24 21:34:34 +08:00
|
|
|
|
IEnumerator Start()
|
2022-03-03 18:08:32 +08:00
|
|
|
|
{
|
2022-03-03 19:13:07 +08:00
|
|
|
|
AssetOperationHandle handle = YooAssets.LoadAssetAsync<AudioClip>("Audio/bgMusic.mp3");
|
|
|
|
|
yield return handle;
|
|
|
|
|
AudioClip audioClip = handle.AssetObject as AudioClip;
|
2022-03-03 18:08:32 +08:00
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
````C#
|
|
|
|
|
// Task加载方式
|
|
|
|
|
async void Start()
|
|
|
|
|
{
|
2022-03-03 19:13:07 +08:00
|
|
|
|
AssetOperationHandle handle = YooAssets.LoadAssetAsync<AudioClip>("Audio/bgMusic.mp3");
|
|
|
|
|
await handle.Task;
|
|
|
|
|
AudioClip audioClip = handle.AssetObject as AudioClip;
|
2022-03-03 18:08:32 +08:00
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
**资源卸载范例**
|
|
|
|
|
|
|
|
|
|
````C#
|
2022-03-23 14:58:24 +08:00
|
|
|
|
IEnumerator Start()
|
2022-03-03 18:08:32 +08:00
|
|
|
|
{
|
2022-03-03 19:13:07 +08:00
|
|
|
|
AssetOperationHandle handle = YooAssets.LoadAssetAsync<AudioClip>("Audio/bgMusic.mp3");
|
2022-03-23 14:58:24 +08:00
|
|
|
|
yield return handle;
|
2022-03-03 19:13:07 +08:00
|
|
|
|
...
|
|
|
|
|
handle.Release();
|
2022-03-03 18:08:32 +08:00
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
2022-04-28 19:25:14 +08:00
|
|
|
|
**资源释放范例**
|
|
|
|
|
|
2022-05-04 00:12:06 +08:00
|
|
|
|
可以在切换场景之后调用资源释放方法或者写定时器间隔时间去释放。
|
|
|
|
|
|
|
|
|
|
注意:只有调用资源释放方法,资源对象才会在内存里被移除。
|
|
|
|
|
|
2022-04-28 19:25:14 +08:00
|
|
|
|
````c#
|
|
|
|
|
private void UnloadAssets()
|
|
|
|
|
{
|
|
|
|
|
YooAssets.UnloadUnusedAssets();
|
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
2022-03-23 14:58:24 +08:00
|
|
|
|
**预制体加载范例**
|
2022-03-03 18:08:32 +08:00
|
|
|
|
|
|
|
|
|
````C#
|
2022-03-23 14:58:24 +08:00
|
|
|
|
IEnumerator Start()
|
|
|
|
|
{
|
|
|
|
|
AssetOperationHandle handle = YooAssets.LoadAssetAsync<GameObject>("Panel/login.prefab");
|
|
|
|
|
yield return handle;
|
|
|
|
|
GameObject go = handle.InstantiateSync();
|
|
|
|
|
Debug.Log($"Prefab name is {go.name}");
|
|
|
|
|
}
|
2022-03-03 18:08:32 +08:00
|
|
|
|
````
|
|
|
|
|
|
2022-03-23 14:58:24 +08:00
|
|
|
|
**子对象加载范例**
|
2022-03-03 18:08:32 +08:00
|
|
|
|
|
|
|
|
|
例如:通过TexturePacker创建的图集,如果需要访问图集的精灵对象,可以通过子对象加载接口。
|
|
|
|
|
|
|
|
|
|
````c#
|
2022-03-23 14:58:24 +08:00
|
|
|
|
IEnumerator Start()
|
2022-03-03 18:08:32 +08:00
|
|
|
|
{
|
2022-03-23 14:58:24 +08:00
|
|
|
|
SubAssetsOperationHandle handle = YooAssets.LoadSubAssetsAsync<Sprite>(location);
|
|
|
|
|
yield return handle;
|
|
|
|
|
var sprite = handle.GetSubAssetObject<Sprite>("spriteName");
|
|
|
|
|
Debug.Log($"Sprite name is {sprite.name}");
|
2022-03-03 18:08:32 +08:00
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
**场景异步加载范例**
|
|
|
|
|
|
2022-04-28 19:25:14 +08:00
|
|
|
|
注意:当加载新的主场景的时候,会自动释放之前加载的主场景以及附加场景。
|
|
|
|
|
|
2022-03-03 18:08:32 +08:00
|
|
|
|
````c#
|
2022-03-23 14:58:24 +08:00
|
|
|
|
IEnumerator Start()
|
2022-03-03 18:08:32 +08:00
|
|
|
|
{
|
2022-03-21 23:49:50 +08:00
|
|
|
|
var sceneMode = UnityEngine.SceneManagement.LoadSceneMode.Single;
|
|
|
|
|
bool activateOnLoad = true;
|
|
|
|
|
SceneOperationHandle handle = YooAssets.LoadSceneAsync("Scene/Login", sceneMode, activateOnLoad);
|
2022-03-23 14:58:24 +08:00
|
|
|
|
yield return handle;
|
|
|
|
|
Debug.Log($"Scene name is {handle.Scene.name}");
|
2022-03-03 18:08:32 +08:00
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
2022-03-10 16:55:12 +08:00
|
|
|
|
**原生文件加载范例**
|
|
|
|
|
|
|
|
|
|
例如:wwise的初始化文件
|
|
|
|
|
|
|
|
|
|
````c#
|
2022-03-23 14:58:24 +08:00
|
|
|
|
IEnumerator Start()
|
2022-03-10 16:55:12 +08:00
|
|
|
|
{
|
|
|
|
|
string location = "wwise/init.bnk";
|
2022-05-04 00:12:06 +08:00
|
|
|
|
string copyPath = $"{Application.persistentDataPath}/Audio/init.bnk";
|
2022-05-08 22:21:13 +08:00
|
|
|
|
RawFileOperation operation = YooAssets.GetRawFileAsync(location, copyPath);
|
2022-03-23 18:24:12 +08:00
|
|
|
|
yield return operation;
|
2022-03-23 14:58:24 +08:00
|
|
|
|
byte[] fileData = operation.GetFileData();
|
|
|
|
|
string fileText = operation.GetFileText();
|
2022-03-10 16:55:12 +08:00
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
2022-05-12 12:37:48 +08:00
|
|
|
|
**获取资源信息列表**
|
|
|
|
|
|
|
|
|
|
通过资源标签来获取资源信息列表。
|
|
|
|
|
|
|
|
|
|
````c#
|
|
|
|
|
private GetAssetInfosByTag(string tag)
|
|
|
|
|
{
|
|
|
|
|
AssetInfo[] assetInfos = YooAssets.GetAssetInfos(tag);
|
|
|
|
|
foreach (var assetInfo in assetInfos)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log(assetInfo.AssetPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
2022-05-08 22:21:13 +08:00
|
|
|
|
**FairyGUI支持解决方案**
|
2022-04-07 12:05:21 +08:00
|
|
|
|
|
|
|
|
|
注意:在FairyGUI的面板销毁的时候,将资源句柄列表释放,否则会造成资源泄漏。
|
|
|
|
|
|
|
|
|
|
````c#
|
|
|
|
|
// 资源句柄列表
|
|
|
|
|
private List<AssetOperationHandle> _handles = new List<AssetOperationHandle>(100);
|
|
|
|
|
|
|
|
|
|
// 加载方法
|
|
|
|
|
private object LoadFunc(string name, string extension, System.Type type, out DestroyMethod method)
|
|
|
|
|
{
|
2022-05-08 22:21:13 +08:00
|
|
|
|
method = DestroyMethod.None; //注意:这里一定要设置为None
|
2022-04-07 12:05:21 +08:00
|
|
|
|
string location = $"FairyRes/{name}{extension}";
|
|
|
|
|
var handle = YooAssets.LoadAssetSync(location , type);
|
|
|
|
|
_handles.Add(handle);
|
|
|
|
|
return handle.AssetObject;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 10:19:17 +08:00
|
|
|
|
// 执行FairyGUI的添加包函数
|
|
|
|
|
UIPackage.AddPackage(name, LoadFunc);
|
|
|
|
|
|
2022-04-07 12:05:21 +08:00
|
|
|
|
// 释放资源句柄列表
|
|
|
|
|
private void ReleaseHandles()
|
|
|
|
|
{
|
|
|
|
|
foreach(var handle in _handles)
|
|
|
|
|
{
|
|
|
|
|
handle.Release();
|
|
|
|
|
}
|
|
|
|
|
_handles.Clear();
|
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
2022-05-08 22:21:13 +08:00
|
|
|
|
**UniTask支持解决方案**
|
|
|
|
|
|
|
|
|
|
[解决方案](https://github.com/tuyoogame/YooAsset/blob/master/Assets/UniTask.YooAsset~/README.md)
|