了解
阅读YooAsset文档,快速入门文章来源:https://www.toymoban.com/news/detail-734023.html
测试环境
- Unity:Unity2021.3.6f1c1
- YooAsset:1.4.11
示例
准备加载资源
包名:Test Package
包内有一个Cube预制体和一个场景
启用Enable Addressable
文章来源地址https://www.toymoban.com/news/detail-734023.html
加载资源
using System;
using System.Collections;
using UnityEngine;
using YooAsset;
public class YooAssetTest : MonoBehaviour
{
public EPlayMode playMode;//设置加载YooAsset模式
string packageName;//包名
ResourcePackage package;
private void Awake()
{
packageName = "Test Package";//编辑器中创建的YooAsset包名
YooAssets.Initialize();
package = YooAssets.CreatePackage(packageName);
YooAssets.SetDefaultPackage(package);
StartCoroutine(InitializeYooAsset(playMode, () =>
{
//异步加载资源 加载Cube
/* var handle = YooAssets.LoadAssetAsync<GameObject>("Cube");
handle.Completed += (value) =>
{
var prefab = value.AssetObject as GameObject;
Instantiate(prefab, Vector3.zero, Quaternion.identity);
};*/
//异步加载场景
var handle = YooAssets.LoadSceneAsync("New Scene");
handle.Completed += (value) =>
{
Debug.Log("加载新的场景");
};
}));
}
private IEnumerator InitializeYooAsset(EPlayMode playMode, Action callBack = null)
{
InitializationOperation initializationOperation = null;
if (playMode == EPlayMode.EditorSimulateMode)//编辑器中运行 无需构建
{
var createParameters = new EditorSimulateModeParameters();
createParameters.SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(packageName);
initializationOperation = package.InitializeAsync(createParameters);
}
else if (playMode == EPlayMode.OfflinePlayMode)//离线模式 需要构建
{
var createParameters = new OfflinePlayModeParameters();
initializationOperation = package.InitializeAsync(createParameters);
}
else
{
Debug.Log("暂无联机模式");
yield break;
}
yield return initializationOperation;
if (package.InitializeStatus == EOperationStatus.Succeed)
{
Debug.Log("包初始化成功");
callBack?.Invoke();
}
else
{
Debug.LogWarning($"{initializationOperation.Error}");
}
}
}
到了这里,关于YooAsset资源管理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!