同步
/// <summary>
/// 重构FGUI的包加载方式
/// </summary>
/// <param name="packName"></param>
public void AddPackage(string packName)
{
//拼接包名
var _key = packName + "_fui";
var taInfo = 加载_fui包//GameModule.Resource.LoadAsset<TextAsset>(_key);
AddPackage(taInfo.bytes, _key);
}
/// <summary>
/// 重构FGUI的包加载方式
/// </summary>
private void AddPackage(byte[] data, string key)
{
UIPackage.LoadResource _loadFromAssetsPath = (string name, string extension, System.Type type, out DestroyMethod destroyMethod) =>
{
destroyMethod = DestroyMethod.Unload;
//过滤特殊包
if (name.Contains("!"))
{
return null;
}
return 加载包关联对象//GameModule.Resource.LoadAsset<UnityEngine.Object>(name);
};
UIPackage.AddPackage(data, key, _loadFromAssetsPath);
}
异步
在这里插入代码片
/// <summary>
/// 重构FGUI的包加载方式
/// </summary>
/// <param name="packName"></param>
public async UniTask AddPackageAsync(string packName)
{
//拼接包名
var _key = packName + "_fui";
var taInfo = await GameModule.Resource.LoadAssetAsync<TextAsset>(_key);
await AddPackageAsync(taInfo.bytes, _key);
}
/// <summary>
/// 重构FGUI的包加载方式
/// </summary>
private async UniTask AddPackageAsync(byte[] data, string key)
{
UIPackage.LoadResourceAsync LoadPackageInternalAsync = async (string name, string extension, System.Type type, PackageItem item) =>
{
Log.Debug($"##########---加载addpack包---{name}");
//异步加载包关联对象
var a = await //GameModule.Resource.LoadAssetAsync<UnityEngine.Object>(name);
//设置资源
item.owner.SetItemAsset(item, a, DestroyMethod.Unload);
};
var _package = UIPackage.AddPackage(data, key, LoadPackageInternalAsync);
}
//最后显示UI
var pkgName = "aaaaa";
var resName = "bbbbb"
if (isAsync)
{
await AddPackageAsync(pkgName);;
}
else
{
AddPackage(pkgName);
}
gObject = UIPackage.CreateObject(pkgName, resName);
GRoot.inst.AddChild(gObject);
这样就完成自定义加载方式。