示例场景 : Assets/Scripts/Example/AF/1.ResFrame/ASyncLoadExample.unity
一.从Resources路径下加载资源
public void loadSprite()
{
//请注意不要带后缀,加载Resource路径下的Sprite
resLoader.LoadAsync<Sprite>(ResFromType.ResourcesRes, "LocalImageExample/ChangeSetGo", LoadSpriteCallBack, true);
}
public void LoadSpriteCallBack(bool result,ResInfo resInfo)
{
if(result)
{
spriteRender.sprite = resInfo.ResObject as Sprite;
}
else
{
AFLogger.e("加载" + resInfo.ResPath + "失败!");
}
}
//释放resLoader加载的单个资源
public void ReleaseSprite()
{
spriteRender.sprite = null;
//如果传false内存不会释放
resLoader.ReleaseRes("LocalImageExample/ChangeSetGo", true);
}
public void LoadPrefab()
{
//加载Resource路径下的Prefabs
resLoader.LoadAsync<GameObject>(ResFromType.ResourcesRes, "ExampleRes/Models/OxygenTank", LoadResPrefab, DestroyCache: true);
}
public void LoadResPrefab(bool result, ResInfo resInfo)
{
if (result)
{
Res.Add(Instantiate(resInfo.ResObject as GameObject));
AFLogger.d("从Resources加载并实例化了" + Res.Count + "个");
}
else
{
AFLogger.e("加载" + resInfo.ResPath + "失败!");
}
}
public void ReleaseResPrefab()
{
if (Res.Count > 0)
{
AFLogger.d("释放从Resources加载的资源");
DestroyImmediate(Res[Res.Count - 1]);
Res.RemoveAt(Res.Count - 1);
//满足释放的条件 : 1.释放资源时destoryObj为true,默认为false 2.此资源的引用计数为0
//也就是说你加载此资源实例化了多少次,就需要释放多少次,并且最后一次释放调用destoryObj = true
resLoader.ReleaseRes("ExampleRes/Models/OxygenTank", true);
}
else
{
AFLogger.d("请先加载资源再释放");
}
}
二.从AB同步加载prefab
public void LoadSpriteFromLocalAB()
{
//从AB包加载Sprite,这里只测试了本地加载,加载来源配置要设置为streamasset
resLoader.LoadAsync<Sprite>(ResFromType.ABRes, "Assets/ResForAB/ABMain/Img/Help.png", LoadABSpriteCallBack, true);
}
public void LoadABSpriteCallBack(bool result,ResInfo resInfo)
{
spriteRender1.sprite = resInfo.ResObject as Sprite;
}
//释放resLoader加载的单个资源
public void ReleaseABSprite()
{
spriteRender1.sprite = null;
//如果传false内存不会释放
resLoader.ReleaseRes("Assets/ResForAB/ABMain/Img/Help.png", true);
}
public void LoadPrefabFromLocalAB()
{
resLoader.InstantiateObjectASync(ResFromType.ABRes, "Assets/ResForAB/ABMain/Prefabs/ABLoadTestPre.prefab",
LoadObjCallBack, UIPar, bClear:true);
}
public void LoadObjCallBack(bool result,ResObject resObject)
{
if(result)
{
Obj.Add(resObject.mCloneObj);
}
}
public void ReleaseObjPrefab()
{
if (Obj.Count > 0)
{
AFLogger.d("释放从AB包加载的资源");
//满足释放的条件 : 1.maxCacheCount为0 2.destoryObj为true 3.引用计数为0
resLoader.ReleaseObj(Obj[Obj.Count - 1], 0, true);
Obj.RemoveAt(Obj.Count - 1);
}
else
{
AFLogger.d("请先加载资源再释放");
}
}
三.编辑器中加载资源
同一中一样使用resloader释放
//专用于编辑器测试,路径为相对于Assets,加载Sprite
public void LoadSpriteFromEditor()
{
resLoader.LoadAsync<Sprite>(ResFromType.EditorRes, "Assets/AFramewprk/Example/Resources/LocalImageExample/ChangeSetGo.png", LoadSprite, true, true);
}
//异步加载Sprite回调
public void LoadSprite(bool result, ResInfo resInfo)
{
if (result)
{
spriteRender.sprite = resInfo.ResObject as Sprite;
}
}
四.API
LoadAsync :
resType | 加载来源,与assetPath配合,确定资源从哪里进行加载 |
---|---|
assetPath | 加载资源路径, 其他方式都是相对于Asset路径,从Reources加载是相对Resources并且没有后缀名 |
loadFinish | 加载回调函数 |
isSprite | 是否加载Sprite,因为Sprite有自己单独的加载方法 |
DestroyCache | 回收时是否销毁内存 |
loadResPriority | 加载优先级,暂未实现 |
同名参数不再赘述
InstantiateObjectSync :
parTrans | 实例化GameObject的父物体 |
---|---|
bClear | 同DestroyCache |