private void _OnAssetObjDestroy(AssetObj a_assetObj) { if (a_assetObj != null) { a_assetObj.OnDestroyInPool(); } }
private void _OnAssetObjRelease(AssetObj a_assetObj) { if (a_assetObj != null) { a_assetObj.OnReleaseToPool(); } }
private void _OnAssetObjGet(AssetObj a_assetObj) { if (a_assetObj != null) { a_assetObj.OnGetFromPool(); } }
/// <summary> /// 设置图片,支持异步加载图片 /// </summary> /// <param name="a_strPath"></param> /// <param name="a_bAsync"></param> public void SetSprite(string a_strPath, bool a_bAsync = false) { if (m_asset != null) { if (m_asset.Desc.Path == a_strPath) { return; } AssetManager.Get().DestroyAsset(m_asset); m_asset = null; } AssetObj asset = AssetManager.Get().CreateAsset(a_strPath, typeof(Sprite), true, a_bAsync, _OnSpriteLoaded); if (asset.LoadState == EAssetLoadState.Error) { AssetManager.Get().DestroyAsset(asset); asset = null; m_image.sprite = null; } else if (asset.LoadState == EAssetLoadState.Done) { m_asset = asset; m_image.sprite = m_asset.Obj as Sprite; } else if (asset.LoadState == EAssetLoadState.Loading) { if (m_defaultAsset != null) { m_image.sprite = m_defaultAsset.Obj as Sprite; } } }
/// <summary> /// 加载资源 /// 注意: /// 如果资源正在异步加载,那么无法再进行同步加载资源,返回的AssetObj里的资源对象为空,并有异步加载的进度,可供查询! /// 养成良好的编码习惯,查询AssetObj的加载状态 /// </summary> /// <param name="a_strPath">资源路径</param> /// <param name="a_assetType">资源类型,默认类型填写typeof(Object)</param> /// <param name="a_bFromPool">是否从池子里加载</param> /// <param name="a_bAsync">是否异步加载</param> /// <param name="a_onLoadFinished">加载完成的回调</param> /// <returns></returns> public AssetObj CreateAsset(string a_strPath, Type a_assetType = null, bool a_bFromPool = false, bool a_bAsync = false, UnityAction <AssetObj> a_onLoadFinished = null) { AssetObj assetObj = null; if (a_bFromPool) { // 从池子里取资源对象 ObjectPool <AssetObj> objectPool = null; m_dictAssetObjPool.TryGetValue(a_strPath, out objectPool); if (objectPool == null) { objectPool = new ObjectPool <AssetObj>(_OnAssetObjGet, _OnAssetObjRelease, _OnAssetObjDestroy); m_dictAssetObjPool.Add(a_strPath, objectPool); } assetObj = objectPool.Get(); if (assetObj.LoadState == EAssetLoadState.Invalid) { assetObj.Load(_GetAssetDesc(a_strPath, a_assetType), a_bAsync, a_onLoadFinished); } else if (assetObj.LoadState == EAssetLoadState.Loading) { assetObj.RegisterFinishedCallback(a_onLoadFinished); } else { a_onLoadFinished(assetObj); } } else { // 直接创建资源对象 assetObj = new AssetObj(); assetObj.Load(_GetAssetDesc(a_strPath, a_assetType), a_bAsync, a_onLoadFinished); } if (assetObj.LoadState == EAssetLoadState.Loading) { AssetDesc assetDesc = assetObj.Desc; if (assetDesc != null && assetDesc.LoadState == EAssetLoadState.Loading) { if (m_listLoadingAssetDescs.Contains(assetDesc) == false) { m_listLoadingAssetDescs.Add(assetDesc); } AssetPackage assetPackage = assetDesc.Package; if (assetPackage != null && assetPackage.LoadState == EAssetLoadState.Loading) { if (m_listLoadingPackage.Contains(assetPackage) == false) { m_listLoadingPackage.Add(assetPackage); } } } } return(assetObj); }
private void _OnSpriteLoaded(AssetObj a_assetObj) { if (a_assetObj.LoadState == EAssetLoadState.Error) { AssetManager.Get().DestroyAsset(a_assetObj); m_image.sprite = null; } else { m_asset = a_assetObj; m_image.sprite = m_asset.Obj as Sprite; } }
public void DestroyAsset(AssetObj a_asset) { if (a_asset != null) { if (a_asset.PooledState == AssetObj.EPooledState.OutPool) { ObjectPool <AssetObj> objectPool = null; m_dictAssetObjPool.TryGetValue(a_asset.Desc.Path, out objectPool); Assert.IsNotNull(objectPool); objectPool.Release(a_asset); } else if (a_asset.PooledState == AssetObj.EPooledState.Invalid) { a_asset.UnLoad(); } } }
public GuiObject LoadGuiObject(string a_strName, string a_strPath) { GuiObject guiObject = FindGuiObject(a_strName); if (guiObject == null) { AssetObj assetObj = AssetManager.Get().CreateAsset(a_strPath); Assert.IsTrue(assetObj.LoadState != EAssetLoadState.Loading); GameObject objRoot = assetObj.gameObject; Assert.IsTrue(objRoot != null); guiObject = objRoot.GetComponent <GuiObject>(); Assert.IsNotNull(guiObject); objRoot.transform.SetParent(GuiManager.Get().UIRoot.transform, false); guiObject.Initialize(a_strName); GuiInfo guiInfo = new GuiInfo(); guiInfo.assetObj = assetObj; guiInfo.guiObject = guiObject; m_listGuiInfos.Add(guiInfo); } return(guiObject); }
private void _OnSpriteLoaded(AssetObj a_assetObj) { m_image.sprite = m_asset.Obj as Sprite; }