public bool IsStreamingAssets(string strAssetPath) { if (AssetManager.getInstance().IsFirstUseStreamingAssets) { string pre_str = ""; if (strAssetPath.Length > 10) { pre_str = strAssetPath.Substring(0, 10); } if (pre_str.Equals("Resources/") || pre_str.Equals("resources/")) { Utils.LogSys.Log("AssetManager Get Path Data 4_1:" + strAssetPath); EAssetPathType eType = _objPathData.getAssetPathType(strAssetPath); if (eType == EAssetPathType.ePersistent || eType == EAssetPathType.eStreamingAssets) { return(true); } } else { Utils.LogSys.Log("AssetManager Get Path Data 4:" + strAssetPath); EAssetPathType eType = _objPathData.getAssetPathType("resources/" + strAssetPath); if (eType == EAssetPathType.ePersistent || eType == EAssetPathType.eStreamingAssets) { return(true); } } } return(false); }
public override string getLatestVersionPath(string strFilePath) { //规范化路径名 strFilePath = strFilePath.Replace("\\", "/"); //Resource目录 string strResourcePath = resourcesPath(); if (strResourcePath != "") { strResourcePath = strResourcePath + "/"; } //所在文件夹目录 string strDirectoryPath = Path.GetDirectoryName(strFilePath); if (strDirectoryPath != "") { strDirectoryPath = strDirectoryPath + "/"; } strDirectoryPath = PathCheck(strDirectoryPath); //保存返回结果 string strRlt = ""; //首先尝试从缓存中查找,找到直接返回,找不到则继续搜索资源路径 //对应三处资源位置的全路径 EAssetPathType eType = getAssetPathType(strFilePath.ToLower()); if (eType == EAssetPathType.ePersistent) { strRlt = persistentDataPath() + "/" + strFilePath;// + ".assetbundle"; } else if (eType == EAssetPathType.eStreamingAssets) { strRlt = streamingAssetsPathPlatform() + "/" + strFilePath;// + ".assetbundle"; } else//(eType == EAssetPathType.eNone || eType == EAssetPathType.eResources) { strRlt = strResourcePath + strDirectoryPath + Path.GetFileNameWithoutExtension(strFilePath); eType = EAssetPathType.eResources; } //规范化路径 return(strRlt);//.Replace("\\", "/"); }
public override string urlForWWW(string strFilePath) { //规范化路径名 strFilePath = strFilePath.Replace("\\", "/"); EAssetPathType eType = getAssetPathType(strFilePath); string strURL = ""; //Resources下的资源路径不返回URL if (eType != EAssetPathType.eResources) { string strPrefix = "file://"; string strAssetFullPath = getLatestVersionPath(strFilePath); strURL = strPrefix + strAssetFullPath; } return(strURL); }
public override string urlForWWW(string strFilePath) { //规范化路径名 strFilePath = strFilePath.Replace("\\", "/"); EAssetPathType eType = getAssetPathType(strFilePath); if (eType == EAssetPathType.ePersistent) { return("file://" + getLatestVersionPath(strFilePath)); } else if (eType == EAssetPathType.eStreamingAssets) { return(getLatestVersionPath(strFilePath));//Application.streamingAssetsPath会自动添加协议jar:file:// } else { return("jar:file://" + Application.dataPath + "/assets/" + strFilePath); } }
/// <summary> /// 实例化2 /// appendedAssets:指定额外的特殊依赖项 /// </summary> public void Instantiate(string strAssetPath, Vector3 position, Quaternion rotation, InitiateHandler handlerFinish, string[] appendedAssets = null) { //string strLatesVersionpath = _objPathData.getLatestVersionPath(strPath); Utils.LogSys.Log("AssetManager Get Path Data 5:" + strAssetPath); EAssetPathType eType = _objPathData.getAssetPathType(strAssetPath); if (eType == EAssetPathType.eResources)//异步创建Resource下资源 { AssetLoadTask task = new AssetLoadTask(strAssetPath, appendedAssets); task.EventFinished += new task.TaskBase.FinishedHandler(delegate(bool manual, TaskBase currentTask) { Object assetObj = ((AssetLoadTask)currentTask).getTargetAsset(); if (assetObj != null) { GameObject obj = GameObject.Instantiate(assetObj, position, rotation) as GameObject; ((AssetLoadTask)currentTask).unloadUnusedAssetbundle(false); handlerFinish(obj); } }); } else { //异步创建StreamingAssets下资源 AssetBundleLoadTask task = new AssetBundleLoadTask(strAssetPath, appendedAssets); task.EventFinished += new task.TaskBase.FinishedHandler(delegate(bool manual, TaskBase currentTask) { Object assetObj = ((AssetBundleLoadTask)currentTask).getTargetAsset(); if (assetObj != null) { GameObject obj = GameObject.Instantiate(assetObj, position, rotation) as GameObject; ((AssetBundleLoadTask)currentTask).unloadUnusedAssetbundle(false); handlerFinish(obj); } }); } }
/// <summary> /// 更新资源路径信息 /// </summary> protected void updateAssetRecordBuff(string strAssetPath, EAssetPathType eAstPath) { _dictAssetsPathRecordBuff[strAssetPath.ToLower()] = eAstPath; }
/// <summary> /// 同步加载指定类型的asset /// needCheckPath: true时会根据_isFirstUseStreamingAssets判断是否要转成assetbundle下的目录 /// </summary> public T loadAsset <T>(string strPath, bool needCheckPath = true) where T : Object { string asset_name = GetNameFromPath(strPath); strPath = strPath.ToLower(); strPath = strPath.Replace("\\", "/"); if (AssetManager.getInstance().IsStreamingAssets(strPath)) { if (needCheckPath) { strPath = UtilTools.PathCheck(strPath); } } Object obj = null; if (_dictAssetBundles.ContainsKey(strPath))//判断缓存中是否有 { AssetBundle bundle = _dictAssetBundles[strPath] as AssetBundle; if (bundle != null) { obj = bundle.LoadAsset <Object>(asset_name); //引用计数+1 //_dictAssetbundlesRefCount[strPath]++; } else { obj = _dictAssetBundles[strPath]; //引用计数+1 //_dictAssetbundlesRefCount[strPath]++; } } else//没有再去同步加载 { Utils.LogSys.Log("AssetManager Get Path Data 2"); string strLatesVersionpath = _objPathData.getLatestVersionPath(strPath); EAssetPathType eType = _objPathData.getAssetPathType(strPath); LogSys.Log("[AssetManager.LoadAesst]:+" + strLatesVersionpath + " eType = " + eType); if (eType == EAssetPathType.ePersistent) { if (File.Exists(strLatesVersionpath)) { string[] objDepRec = AssetManager.getInstance().getAssetBundleDependencies(strPath); for (int i = 0; i < objDepRec.Length; i++) { loadAsset <Object>(objDepRec[i], false);//依赖的资源不需要再检测路径,否则会出导致路径错误 } AssetBundle assetbundle = AssetBundle.LoadFromFile(strLatesVersionpath); if (assetbundle != null) { obj = assetbundle.LoadAsset <Object>(asset_name); _dictAssetBundles[strPath] = assetbundle; //引用计数初始化 if (!_dictAssetbundlesRefCount.ContainsKey(strPath)) { _dictAssetbundlesRefCount[strPath] = 0; } } // byte[] bytes = File.ReadAllBytes(strLatesVersionpath); // if (bytes != null && bytes.Length != 0) // { // AssetBundle assetbundle = AssetBundle.LoadFromMemory(bytes); // if (assetbundle != null) // { // obj = assetbundle.LoadAsset<Object>(asset_name); // _dictAssetBundles[strPath] = assetbundle; // //引用计数初始化 // if (!_dictAssetbundlesRefCount.ContainsKey(strPath)) // _dictAssetbundlesRefCount[strPath] = 0; // } // } } } else if (eType == EAssetPathType.eStreamingAssets) { JARUtilTools tools = GameSceneManager.uiCameraObj.GetComponent <JARUtilTools>(); if (tools != null) { string[] objDepRec = AssetManager.getInstance().getAssetBundleDependencies(strPath); for (int i = 0; i < objDepRec.Length; i++) { loadAsset <Object>(objDepRec[i], false);//依赖的资源不需要再检测路径,否则会出导致路径错误 } string path = IPath.getPlatformName() + "/" + strPath; string stream_asset_path = IPath.getPlatformName() + "/" + strPath; #if UNITY_EDITOR stream_asset_path = strLatesVersionpath; #endif AssetBundle assetbundle = AssetBundle.LoadFromFile(strLatesVersionpath); if (assetbundle != null) { obj = assetbundle.LoadAsset <Object>(asset_name); _dictAssetBundles[strPath] = assetbundle; //引用计数初始化 if (!_dictAssetbundlesRefCount.ContainsKey(strPath)) { _dictAssetbundlesRefCount[strPath] = 0; } } // byte[] bytes = tools.GetAssetBundleBytes(stream_asset_path); // if (bytes != null && bytes.Length != 0) // { // AssetBundle assetbundle = AssetBundle.LoadFromMemory(bytes); // if (assetbundle != null) // { // obj = assetbundle.LoadAsset<Object>(asset_name); // _dictAssetBundles[strPath] = assetbundle; // //引用计数初始化 // if (!_dictAssetbundlesRefCount.ContainsKey(strPath)) // _dictAssetbundlesRefCount[strPath] = 0; // } // } } } else// (eType == EAssetPathType.eResources || (eType == EAssetPathType.eNone)) { obj = Resources.Load(strLatesVersionpath); //addAssetBundle(strPath, obj); } } return((T)obj); }
/// <summary> /// 任务功能执行 /// </summary> /// <returns>枚举器</returns> public override IEnumerator taskExec() { yield return(null); string strPath = _taskName; //将要加载的对象 AssetBundle objWillLoad = null; AssetBundleLoadTaskItem otherLoadingTask; bool isLoaded = AssetManager.getInstance().isAssetbundleLoaded(strPath); if (!isLoaded) { TaskManager.getInstance().OtherTaskLoadingAssetPath(out otherLoadingTask, strPath); while (otherLoadingTask != null && otherLoadingTask != this) { _bWaiting = true; if (otherLoadingTask.IsLoadComplete()) { otherLoadingTask = null; isLoaded = true; } yield return(null); } } _bWaiting = false; //已经加载则直接使用 if (isLoaded) { Object ab = AssetManager.getInstance().getAssetBundle(strPath); if (ab != null) { objWillLoad = (AssetBundle)ab; _assetBundle = ab; AssetManager.getInstance().addAssetbundleRefCount(strPath); } _bLoaded = true; } else { _bLoading = true; string strURL = AssetManager.getInstance().PathData.urlForWWW(strPath); EAssetPathType eType = AssetManager.getInstance().PathData.getAssetPathType(strPath); if (eType != EAssetPathType.eNone) { if (strPath.Contains(".shader")) { Utils.LogSys.Log("---------->shader:" + strPath); } w = new WWW(strURL); yield return(w); if (w.isDone) { objWillLoad = w.assetBundle; } if (objWillLoad != null) { _assetBundle = (Object)objWillLoad; AssetManager.getInstance().addAssetBundle(strPath, (Object)objWillLoad); if (strPath.IndexOf(".unity3d") > 0) { string[] names = objWillLoad.GetAllAssetNames(); for (int i = 0; i < names.Length; i++) { Utils.LogSys.Log("---------->asset:" + names[i]); } } } } _bLoading = false; _bLoaded = true; } }