private static void GetCommonRes_OnRunTime(ref ResNode reslut, string AssetBundleName, string AssetName, System.Type t) { if (!isOnRunTimeMode) { return; } string str = AssetBundleName + AssetName; string hashCode = str.GetHashCode().ToString(); if (m_CommonResNodeTable.TryGetValue(hashCode, out reslut)) { reslut.AddRef(); return; } Dictionary <string, UnityEngine.Object> assetTable = null; if (m_CommonAssetsTable.TryGetValue(AssetBundleName, out assetTable)) { UnityEngine.Object asset = null; if (assetTable.TryGetValue(AssetName, out asset)) { reslut = AllocResNode(hashCode.ToString()); reslut.Clear(); if (null == reslut.config) { reslut.config = new ResConfigData(); } reslut.config.AssetName = AssetName; reslut.config.AssetBundleName = AssetBundleName; reslut.config.AssetGUID = hashCode; reslut.isAsync = false; reslut.FillAssets(asset); reslut.status = ResNodeStatus.LoadFinish; reslut.AddRef(); reslut.assetType = AssetBundleName.EndsWith(AssetBundleManager.PrefabVariant) ? AssetType.Asset_Prefab : AssetType.Asset_CommonAssets; m_CommonResNodeTable.Add(hashCode, reslut); } else { AssetBudleLog("在通用资源包:" + AssetBundleName + "找不到资源:" + AssetName); } } return; }
private static void GetCommonRes(ref ResNode reslut, ResConfigData config) { if (!isOnRunTimeMode) { return; } if (m_CommonResNodeTable.TryGetValue(config.AssetGUID, out reslut)) { reslut.AddRef(); return; } Dictionary <string, UnityEngine.Object> assetTable = null; if (m_CommonAssetsTable.TryGetValue(config.AssetBundleName, out assetTable)) { UnityEngine.Object asset = null; if (assetTable.TryGetValue(config.AssetName, out asset)) { reslut = AllocResNode(config.AssetGUID); reslut.Clear(); reslut.config = config; reslut.assetType = config.AssetBundleName.EndsWith(AssetBundleManager.PrefabVariant) ? AssetType.Asset_Prefab : AssetType.Asset_CommonAssets; reslut.isAsync = false; reslut.FillAssets(asset); reslut.status = ResNodeStatus.LoadFinish; reslut.AddRef(); m_CommonResNodeTable.Add(config.AssetGUID, reslut); } else { AssetBudleLog("在通用资源包:" + config.AssetBundleName + "找不到资源:" + config.AssetName); } } return; }
private static void DeletePrefabAsync(ref ResNode res, System.Action <ResConfigData, ResNode, System.Object> callBack, bool Immediate = true) { if (null == res || !isVaild || res.GetRefCount() <= 0) { return; } //先看看是不是通用资源 if (IsCommonAssets(res)) { res.Release(); RemoveFromDelayCallBackList(res, callBack); return; } int guid = res.instanceID; //表里面有这个预制体的话,说明依赖是加载成功了的。但是预制体本体可能加载完成,也可能是在加载中 if (m_PrefabResTable.ContainsKey(guid)) { int refCount = res.Release(); //这个资源已经没有人引用了 if (refCount <= 0) { RemoveAllDelayCallBackList(res); RemoveAllResCallBack(m_PrefabRequestTable, res); DeletePrefabResourceAsync(ref res, PrefabResourceCallBack, Immediate); } else { RemoveFromDelayCallBackList(res, callBack); RemoveResCallBack(m_PrefabRequestTable, res, callBack); } } else { //如果没有在预制体表里的,说明还处于依赖加载中 if (m_AsyncLoadingDependicensPrefabResNode.ContainsKey(guid)) { int refCount = res.Release(); //这个资源已经没有人引用了 if (refCount <= 0) { UnLoadDependenicesAsync(guid); //m_NeedToDeletePrefab.Add(guid); m_AsyncLoadingDependicensPrefabResNode.Remove(guid); RemoveAllDelayCallBackList(res); RemoveAllResCallBack(m_PrefabRequestTable, res); res.Clear(); } else { RemoveFromDelayCallBackList(res, callBack); RemoveResCallBack(m_PrefabRequestTable, res, callBack); } } else { Debug.LogError("要卸载的资源不是预制体,忽略此资源,有可能会存在资源泄露!:" + res.AssetBundleName + "," + res.AssetName); } } res = null; }
private static void GetCommonRes_OnEditor(ref ResNode reslut, string AssetBundleName, string AssetName, System.Type t) { if (isOnRunTimeMode) { return; } string str = AssetBundleName + AssetName; string hashCode = str.GetHashCode().ToString(); if (m_CommonResNodeTable.TryGetValue(hashCode, out reslut)) { reslut.AddRef(); return; } Dictionary <string, UnityEngine.Object> assetTable = null; UnityEngine.Object asset = null; /*编辑器下找不到资源,就去加载*/ if (m_CommonAssetsTable.TryGetValue(AssetBundleName, out assetTable)) { if (assetTable.TryGetValue(AssetName, out asset)) { reslut = AllocResNode(hashCode.ToString()); reslut.Clear(); if (null == reslut.config) { reslut.config = new ResConfigData(); } reslut.config.AssetName = AssetName; reslut.config.AssetBundleName = AssetBundleName; reslut.config.AssetGUID = hashCode; reslut.isAsync = false; reslut.FillAssets(asset); reslut.assetType = AssetBundleName.EndsWith(AssetBundleManager.PrefabVariant) ? AssetType.Asset_Prefab : AssetType.Asset_CommonAssets; reslut.AddRef(); m_CommonResNodeTable.Add(hashCode, reslut); } else { UnityEngine.Object[] assets = LoadAssetOnEditorFromAbInstance(AssetBundleName, AssetName, t); if (null != assets && assets.Length > 0) { assetTable = new Dictionary <string, UnityEngine.Object>(); foreach (UnityEngine.Object ob in assets) { assetTable.Add(ob.name, ob); } //再将东西取出来 asset = assetTable[AssetName]; reslut = AllocResNode(hashCode.ToString()); reslut.Clear(); reslut.FillAssets(asset); reslut.status = ResNodeStatus.LoadFinish; if (reslut.config == null) { reslut.config = new ResConfigData(); } reslut.config.AssetName = AssetName; reslut.config.AssetBundleName = AssetBundleName; reslut.config.AssetGUID = hashCode; reslut.isAsync = false; reslut.assetType = AssetBundleName.EndsWith(AssetBundleManager.PrefabVariant) ? AssetType.Asset_Prefab : AssetType.Asset_CommonAssets; reslut.AddRef(); m_CommonResNodeTable.Add(hashCode, reslut); } else { AssetBudleLog("在通用资源包:" + AssetBundleName + "找不到资源:" + AssetName); } } } else { UnityEngine.Object[] assets = LoadAssetOnEditorFromAbInstance(AssetBundleName, AssetName, t); if (null != assets && assets.Length > 0) { assetTable = new Dictionary <string, UnityEngine.Object>(); foreach (UnityEngine.Object ob in assets) { assetTable.Add(ob.name, ob); } m_CommonAssetsTable.Add(AssetBundleName, assetTable); //再将东西取出来 asset = assetTable[AssetName]; reslut = AllocResNode(hashCode.ToString()); reslut.Clear(); if (reslut.config == null) { reslut.config = new ResConfigData(); } reslut.config.AssetName = AssetName; reslut.config.AssetBundleName = AssetBundleName; reslut.config.AssetGUID = hashCode; reslut.isAsync = false; reslut.FillAssets(asset); reslut.status = ResNodeStatus.LoadFinish; reslut.assetType = AssetBundleName.EndsWith(AssetBundleManager.PrefabVariant) ? AssetType.Asset_Prefab : AssetType.Asset_CommonAssets; reslut.AddRef(); m_CommonResNodeTable.Add(hashCode, reslut); } else { AssetBudleLog("在通用资源包:" + AssetBundleName + "找不到资源:" + AssetName); } } return; }
/// <summary> /// 异步构建package /// </summary> /// <param name="reslutTable"></param> /// <param name="config"></param> /// <param name="resourcePackageCallBack"></param> /// <param name="UserCallBack"></param> /// <param name="userDataObj"></param> /// <returns></returns> private static void BuildPacakgeAsync(ref ResNode result, Dictionary <int, ResNode> reslutTable, ResConfigData config, System.Action <ResourcePackageCallBackObj> resourcePackageCallBack, System.Action <ResConfigData, ResNode, System.Object> UserCallBack, System.Object userDataObj, bool isPrefabResNode = false) { string patch = AssetBundleManager.FullAssetPackagePathRoot + config.AssetBundleName; UnionResConfigData unionRes = config as UnionResConfigData; string abPath = config.AssetBundleName; if (unionRes != null) { patch = unionRes.UnionFilePath; abPath = unionRes.ShortUnionFilePath; } if (!m_RespackageTable.ContainsKey(abPath)) { AssetBudleLog("AssetBundle文件不存在!,路径:" + patch); return; } result = AllocResNode(config.AssetGUID); result.StartLoadedTime(); //预制体是预先创建好ResNode的,所以不能清除和增加引用,直接使用外部的即可 if (!isPrefabResNode) { result.Clear(); result.AddRef(); } result.config = config; result.isAsync = true; result.status = ResNodeStatus.WatingForLoad; AssetRequest are = null; are = AllocAssetRequest(result.instanceID); m_LoadingAssetList.Add(result.instanceID); are.Clear(); reslutTable.Add(result.instanceID, result); GenerateResCallBack(result, config, UserCallBack, userDataObj); ResourcePackage pakg = AllocResourcePackage(config.AssetBundleName); pakg.priority = config.priority; //已经有了 if (m_LoadedResourcePackage.Contains(pakg.instanceID)) { pakg.AddRef(); pakg.AddAsyncCallBackData(resourcePackageCallBack, BuildResourcePackageCallBackObj(pakg, result, config, UserCallBack, userDataObj)); PushToResourcePackageCallBackList(pakg.instanceID); } else { //如果卸载列表里,先移除掉,避免加载出来就被卸载了 RemoveNeedToUnLoadPackage(pakg.instanceID); //没有开启异步加载 if (!enabledAsyncLoadAssetFile) { pakg = GetResourcePackge(config); pakg.AddAsyncCallBackData(resourcePackageCallBack, BuildResourcePackageCallBackObj(pakg, result, config, UserCallBack, userDataObj)); PushToResourcePackageCallBackList(pakg.instanceID); return; } //全新的包 if (!m_ResourcePackageRequestTable.Contains(pakg.instanceID)) { pakg.Clear(); pakg.isAsync = true; pakg.filePatch = patch; FillUnioFileInfo(config as UnionResConfigData, pakg); EnQueueWaitForUpLoadAssetsFileAsyncRequestTable(pakg.instanceID); m_ResourcePackageRequestTable.Add(pakg.instanceID); } pakg.AddAsyncCallBackData(resourcePackageCallBack, BuildResourcePackageCallBackObj(pakg, result, config, UserCallBack, userDataObj)); pakg.AddRef(); } return; }