2022-03-03 18:08:32 +08:00
|
|
|
|
# 初始化
|
|
|
|
|
|
2022-10-08 16:25:36 +08:00
|
|
|
|
初始化资源系统
|
2022-03-03 18:08:32 +08:00
|
|
|
|
|
2022-10-08 16:25:36 +08:00
|
|
|
|
```c#
|
2022-10-18 19:27:47 +08:00
|
|
|
|
// 初始化资源系统
|
2022-10-08 16:25:36 +08:00
|
|
|
|
YooAssets.Initialize();
|
2022-10-18 19:27:47 +08:00
|
|
|
|
|
|
|
|
|
// 创建默认的资源包
|
|
|
|
|
var defaultPackage = YooAssets.CreateAssetsPackage("DefaultPackage");
|
|
|
|
|
|
|
|
|
|
// 设置该资源包为默认的资源包,可以使用YooAssets相关加载接口加载该资源包内容。
|
|
|
|
|
YooAssets.SetDefaultAssetsPackage(defaultPackage);
|
2022-10-08 16:25:36 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
资源系统的运行模式支持三种:编辑器模拟模式,单机运行模式,联机运行模式。
|
2022-03-03 18:08:32 +08:00
|
|
|
|
|
|
|
|
|
**编辑器模拟模式**
|
|
|
|
|
|
|
|
|
|
在编辑器下,不需要构建资源包,来模拟运行游戏。
|
|
|
|
|
|
|
|
|
|
注意:该模式只在编辑器下起效
|
|
|
|
|
|
|
|
|
|
````c#
|
|
|
|
|
private IEnumerator InitializeYooAsset()
|
|
|
|
|
{
|
2022-10-18 19:27:47 +08:00
|
|
|
|
var initParameters = new EditorSimulateModeParameters();
|
|
|
|
|
initParameters.SimulatePatchManifestPath = EditorSimulateModeHelper.SimulateBuild("DefaultPackage");
|
|
|
|
|
yield return defaultPackage.InitializeAsync(initParameters);
|
2022-03-03 18:08:32 +08:00
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
**单机运行模式**
|
|
|
|
|
|
|
|
|
|
对于不需要热更新资源的游戏,可以使用单机运行模式。
|
|
|
|
|
|
|
|
|
|
注意:该模式需要构建资源包
|
|
|
|
|
|
|
|
|
|
````c#
|
|
|
|
|
private IEnumerator InitializeYooAsset()
|
|
|
|
|
{
|
2022-10-18 19:27:47 +08:00
|
|
|
|
var initParameters = new OfflinePlayModeParameters();
|
|
|
|
|
yield return defaultPackage.InitializeAsync(initParameters);
|
2022-03-03 18:08:32 +08:00
|
|
|
|
}
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
**联机运行模式**
|
|
|
|
|
|
|
|
|
|
对于需要热更新资源的游戏,可以使用联机运行模式,该模式下初始化参数会很多。
|
|
|
|
|
|
|
|
|
|
注意:该模式需要构建资源包
|
|
|
|
|
|
2022-03-10 10:18:48 +08:00
|
|
|
|
- DecryptionServices : 如果资源包在构建的时候有加密,需要提供实现IDecryptionServices接口的实例类。
|
2022-04-28 19:25:14 +08:00
|
|
|
|
|
2022-10-18 19:27:47 +08:00
|
|
|
|
- QueryServices:内置资源查询服务接口。
|
|
|
|
|
|
2022-03-03 18:08:32 +08:00
|
|
|
|
- DefaultHostServer : 默认的资源服务器IP地址。
|
2022-04-28 19:25:14 +08:00
|
|
|
|
|
2022-03-03 18:08:32 +08:00
|
|
|
|
- FallbackHostServer : 备用的资源服务器IP地址。
|
|
|
|
|
|
|
|
|
|
````c#
|
|
|
|
|
private IEnumerator InitializeYooAsset()
|
|
|
|
|
{
|
2022-10-18 19:27:47 +08:00
|
|
|
|
var initParameters = new HostPlayModeParameters();
|
2022-06-30 12:02:06 +08:00
|
|
|
|
initParameters.LocationServices = new DefaultLocationServices("Assets/GameRes");
|
2022-10-08 16:25:36 +08:00
|
|
|
|
initParameters.DecryptionServices = new BundleDecryptionServices();
|
|
|
|
|
initParameters.QueryServices = new QueryStreamingAssetsServices();
|
2022-07-19 16:30:18 +08:00
|
|
|
|
initParameters.DefaultHostServer = "http://127.0.0.1/CDN1/Android/v1.0";
|
|
|
|
|
initParameters.FallbackHostServer = "http://127.0.0.1/CDN2/Android/v1.0";
|
2022-10-18 19:27:47 +08:00
|
|
|
|
yield return defaultPackage.InitializeAsync(initParameters);
|
2022-03-03 18:08:32 +08:00
|
|
|
|
}
|
2022-03-21 23:49:50 +08:00
|
|
|
|
|
2022-10-08 16:25:36 +08:00
|
|
|
|
// 文件解密服务类
|
|
|
|
|
private class BundleDecryptionServices : IDecryptionServices
|
2022-03-21 23:49:50 +08:00
|
|
|
|
{
|
2022-07-19 16:30:18 +08:00
|
|
|
|
public ulong GetFileOffset(DecryptionFileInfo fileInfo)
|
2022-03-21 23:49:50 +08:00
|
|
|
|
{
|
|
|
|
|
return 32;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-08 16:25:36 +08:00
|
|
|
|
|
|
|
|
|
// 内置文件查询服务类
|
|
|
|
|
private class QueryStreamingAssetsServices : IQueryServices
|
|
|
|
|
{
|
|
|
|
|
public bool QueryStreamingAssets(string fileName)
|
|
|
|
|
{
|
|
|
|
|
// 注意:使用了BetterStreamingAssets插件
|
2022-10-18 19:27:47 +08:00
|
|
|
|
string buildinFolderName = YooAssets.GetStreamingAssetBuildinFolderName();
|
|
|
|
|
return BetterStreamingAssets.FileExists($"{buildinFolderName}/{fileName}");
|
2022-10-08 16:25:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-21 23:49:50 +08:00
|
|
|
|
````
|
|
|
|
|
|