了解
阅读YooAsset文档,快速入门
测试环境
- Unity:Unity2021.3.6f1c1
- YooAsset:1.4.11
示例
准备加载资源
包名:Test Package
包内有一个Cube预制体和一个场景
启用Enable Addressable
加载资源
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}");
}
}
}