123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System;
- using UnityEngine.Networking;
- public enum E_AssetBundleLoadType
- {
- /// <summary>
- /// www方式异步加载
- /// </summary>
- WWW = 0,
- /// <summary>
- /// LoadFromFile方式异步加载
- /// </summary>
- LoadFromFileAsync = 1,
- /// <summary>
- /// LoadFromFile方式同步加载
- /// </summary>
- LoadFromFileSynchro = 2,
- /// <summary>
- /// 网络加载
- /// </summary>
- NetUrlMode = 3,
- /// <summary>
- /// 新版资源包加载方式
- /// </summary>
- NewAssetBundle = 4,
- }
- /// <summary>
- /// 资源管理器
- /// By WJ 2019-12-12 09:03:30
- /// </summary>
- public class ResourceHelper : MonoSingleton<ResourceHelper>
- {
- /// <summary>
- /// 正在加载的相同AssetBundle列表
- /// </summary>
- private Dictionary<string, List<Action<AssetBundle>>> mSameAssetBundleAcList = new Dictionary<string, List<Action<AssetBundle>>>();
- /// <summary>
- /// 已加载的AssetBundle
- /// </summary>
- private Dictionary<string, AssetBundle> mAssetBundleList = new Dictionary<string, AssetBundle>();
- /// <summary>
- /// 正在加载的资源名称列表
- /// </summary>
- private List<string> mLoadingAssetList = new List<string>();
- /// <summary>
- /// 加载进度
- /// </summary>
- private float mLoadProgress = 0;
- /// <summary>
- /// 超时时间
- /// </summary>
- private float mTimeOutTime = 0;
- #region 新版加载方式
- /// <summary>
- /// 主文件
- /// </summary>
- private AssetBundleManifest abManifest = null;
- /// <summary>
- /// 主文件请求
- /// </summary>
- private AssetBundle manifesAssetBundle = null;
- /// <summary>
- /// 资源包的请求
- /// </summary>
- private Dictionary<string, AssetBundle> loadNewAssetBundles = new Dictionary<string, AssetBundle>();
- /// <summary>
- /// 资源包后缀
- /// </summary>
- private static string AssetBundleSuffix = ".unity3d";
- #endregion
- ///// <summary>
- ///// 加载资源
- ///// </summary>
- ///// <param name="_name">名称</param>
- ///// <param name="ac">加载完成后的回调</param>
- ///// <param name="multiLoad">同一文件是否支持多重加载,false时同一时间只会加载一份</param>
- ///// <param name="type">加载的类型,默认LoadFromFile异步加载</param>
- //public void LoadAssetBundle(string _name, Action<AssetBundle> ac, bool multiLoad = true, E_AssetBundleLoadType type = E_AssetBundleLoadType.LoadFromFileAsync)
- //{
- // switch (type)
- // {
- // case E_AssetBundleLoadType.WWW:
- // LoadAssetBundleWWW(_name, ac, multiLoad);
- // break;
- // case E_AssetBundleLoadType.LoadFromFileAsync:
- // LoadAssetBundleAsync(_name, ac, multiLoad);
- // break;
- // case E_AssetBundleLoadType.LoadFromFileSynchro:
- // LoadAssetBundleSynchro(_name, ac);
- // break;
- // //case E_AssetBundleLoadType.NetUrlMode:
- // // LoadAssetBundleUrl(_name, ac);
- // // break;
- // //case E_AssetBundleLoadType.NewAssetBundle:
- // // LoadAssetBundleNew(_name, ac);
- // // break;
- // }
- //}
- /// <summary>
- /// 新版加载资源包
- /// </summary>
- /// <param name="_type">所属类型名称</param>
- /// <param name="_name">加载的资源名称</param>
- /// <param name="ac">加载完成的回调</param>
- public void LoadAssetBundle(string resName, Action<AssetBundle> ac = null)
- {
- if (string.IsNullOrEmpty(resName))
- {
- return;
- }
- string assetBundleName = resName.ToLower() + AssetBundleSuffix;
- string mainPath = GetNewPath("", "");
- if (manifesAssetBundle == null)
- {
- manifesAssetBundle = AssetBundle.LoadFromFile(mainPath + "NewRes");
- }
- // 加载主文件
- if (abManifest == null)
- {
- abManifest = manifesAssetBundle.LoadAsset<AssetBundleManifest>("assetbundlemanifest");
- }
- var hash = abManifest.GetAssetBundleHash(assetBundleName);
- // 新版资源,查找依赖
- if (hash.isValid)
- {
- Debug.Log(resName+"存在依赖关系,使用新版加载方式");
- // 再查找依赖资源包加载并创建
- string[] strs = abManifest.GetAllDependencies(assetBundleName);
- foreach (var itemName in strs)
- {
- if (loadNewAssetBundles.ContainsKey(itemName) == false)
- {
- try
- {
- AssetBundle ylAssetBundle = AssetBundle.LoadFromFile(mainPath + itemName);
- loadNewAssetBundles.Add(itemName, ylAssetBundle);
- }
- catch (Exception e)
- {
- UI_CueDialog.Instance().Open("加载异常:" + e.Message);
- }
- }
- }
- }
- // 旧版资源包,用正常名称
- else
- {
- Debug.Log(resName + "不存在依赖关系,使用旧版加载方式");
- assetBundleName = resName + AssetBundleSuffix;
- }
- if (loadNewAssetBundles.ContainsKey(assetBundleName) == false)
- {
- // 最后再加载并创建自身资源包
- AssetBundle ab = AssetBundle.LoadFromFile(mainPath + assetBundleName);
- loadNewAssetBundles.Add(assetBundleName, ab);
- if (ac == null)
- {
- GameObject go = ab.LoadAsset<GameObject>(resName);
- if (go != null)
- {
- Instantiate(go);
- }
- }
- else
- {
- ac(ab);
- }
- }
- else
- {
- AssetBundle ab = loadNewAssetBundles[assetBundleName];
- if (ac == null)
- {
- GameObject go = ab.LoadAsset<GameObject>(resName);
- if (go != null)
- {
- Instantiate(go);
- }
- }
- else
- {
- ac(ab);
- }
- }
- }
- /// <summary>
- /// 获取加载进度
- /// </summary>
- /// <returns></returns>
- public float GetLoadProgress()
- {
- return mLoadProgress;
- }
- ///// <summary>
- ///// 加载网络资源
- ///// </summary>
- ///// <param name="url"></param>
- ///// <param name="ac"></param>
- ///// <param name="version"></param>
- //private void LoadAssetBundleUrl(string _name, Action<AssetBundle> ac)
- //{
- // if (_name == string.Empty)
- // {
- // ac(null);
- // return;
- // }
- // if (mAssetBundleList.ContainsKey(_name))
- // {
- // ac(mAssetBundleList[_name]);
- // }
- // else
- // {
- // if (mLoadingAssetList.Contains(_name) == false)
- // {
- // mLoadingAssetList.Add(_name);
- // StartCoroutine(IELoadAssetBundleUrl(_name, ac));
- // }
- // else
- // {
- // if (mSameAssetBundleAcList.ContainsKey(_name))
- // {
- // if (mSameAssetBundleAcList[_name].Contains(ac) == false)
- // {
- // mSameAssetBundleAcList[_name].Add(ac);
- // }
- // }
- // else
- // {
- // List<Action<AssetBundle>> acList = new List<Action<AssetBundle>>();
- // acList.Add(ac);
- // mSameAssetBundleAcList.Add(_name, acList);
- // }
- // }
- // }
- //}
- ///// <summary>
- ///// 协同加载网络资源
- ///// </summary>
- ///// <param name="url"></param>
- ///// <param name="ac"></param>
- ///// <param name="version"></param>
- ///// <returns></returns>
- //IEnumerator IELoadAssetBundleUrl(string _name, Action<AssetBundle> ac, string fileSuffix = ".unity3d")
- //{
- // // 重新加载
- // bool reLoad = true;
- // while (reLoad)
- // {
- // string url = "";
- // uint version = 0;
- // string streamingAssetsPath = Application.streamingAssetsPath + "/Res/" + _name + fileSuffix;
- // if (Launcher.netUpdateFileInfo != null)
- // {
- // if (Application.isEditor && File.Exists(streamingAssetsPath))
- // {
- // url = "file://" + streamingAssetsPath;
- // }
- // else
- // {
- // if (RuntimePlatform.IPhonePlayer == Application.platform)
- // {
- // url = Launcher.netUrl + "UpdateAndroid/" + _name + fileSuffix;
- // }
- // else
- // {
- // url = Launcher.netUrl + "UpdateAndroid/" + _name + fileSuffix;
- // }
- // }
- // foreach (FileMD5Info info in Launcher.netUpdateFileInfo.Infos)
- // {
- // if (info.Name == _name + fileSuffix)
- // {
- // if(info.PackSizeM >= 1)
- // {
- // UI_LoadingWindow.Instance().Show(1);
- // }
- // version = (uint)info.PackVersion;
- // break;
- // }
- // }
- // }
- // else
- // {
- // version = Launcher.netVersion;
- // }
- // UnityWebRequest request = null;
- // // 编辑器下,有工程资源,不走缓存机制
- // if (Application.isEditor && File.Exists(streamingAssetsPath))
- // {
- // request = UnityWebRequestAssetBundle.GetAssetBundle(url);
- // }
- // else
- // {
- // request = UnityWebRequestAssetBundle.GetAssetBundle(url, version, 0);
- // }
- // yield return request.SendWebRequest();
- // // 加载完成,移除正在加载的资源
- // mLoadingAssetList.Remove(_name);
- // // 等待加载完成
- // while (request.isDone == false)
- // {
- // yield return new WaitForSeconds(0.05f);
- // }
- // // 没有错误
- // if (request.error == null)
- // {
- // AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
- // mAssetBundleList.Add(_name, ab);
- // if (ac != null)
- // {
- // ac(ab);
- // }
- // reLoad = false;
- // // 关闭Loading
- // if (mLoadingAssetList.Count == 0)
- // {
- // UI_LoadingWindow.Instance().Hide(1);
- // }
- // // 返回相同AC
- // if (mSameAssetBundleAcList.ContainsKey(_name))
- // {
- // for (int i = 0; i < mSameAssetBundleAcList[_name].Count; i++)
- // {
- // mSameAssetBundleAcList[_name][i](ab);
- // }
- // mSameAssetBundleAcList[_name].Clear();
- // }
- // }
- // else
- // {
- // bool waiting = false;
- // if (request.isNetworkError)
- // {
- // // 不可写入
- // if (request.error == "Unable to write data")
- // {
- // reLoad = false;
- // }
- // else
- // {
- // reLoad = true;
- // waiting = true;
- // UI_CueDialog.Instance().Open("加载" + _name + " 失败,是否重试?", "", E_DialogType.TwoBntton, () =>
- // {
- // // 同意的回调
- // waiting = false;
- // }, () =>
- // {
- // // 取消的回调
- // Application.Quit();
- // });
- // }
- // }
- // else if (request.isHttpError)
- // {
- // reLoad = true;
- // waiting = true;
- // UI_CueDialog.Instance().Open("加载" + _name + " 失败,是否重试?", "", E_DialogType.TwoBntton, () =>
- // {
- // // 同意的回调
- // waiting = false;
- // }, () =>
- // {
- // // 取消的回调
- // Application.Quit();
- // });
- // }
- // Debug.LogError(_name + " 加载错误: " + request.error);
- // while (waiting)
- // {
- // yield return new WaitForSeconds(0.1f);
- // }
- // // 不重新加载,则返回回调
- // if (reLoad == false)
- // {
- // if (ac != null)
- // {
- // ac(null);
- // }
- // // 关闭Loading
- // UI_LoadingWindow.Instance().Hide(1);
- // }
- // }
- // }
- //}
- /// <summary>
- /// WWW加载AssetBundle
- /// </summary>
- /// <param name="_name">名称,不包含后缀</param>
- /// <param name="ac"> Lambda返回值</param>
- private void LoadAssetBundleWWW(string _name, Action<AssetBundle> ac, bool multiLoad = true)
- {
- if (_name == string.Empty)
- {
- ac(null);
- return;
- }
- if (mAssetBundleList.ContainsKey(_name))
- {
- ac(mAssetBundleList[_name]);
- }
- else
- {
- if (mLoadingAssetList.Contains(_name) == false)
- {
- mLoadingAssetList.Add(_name);
- StartCoroutine(IELoadAssetBundleWWW(_name, ac, multiLoad));
- }
- else
- {
- if (mSameAssetBundleAcList.ContainsKey(_name))
- {
- if (mSameAssetBundleAcList[_name].Contains(ac) == false)
- {
- mSameAssetBundleAcList[_name].Add(ac);
- }
- }
- else
- {
- List<Action<AssetBundle>> acList = new List<Action<AssetBundle>>();
- acList.Add(ac);
- mSameAssetBundleAcList.Add(_name, acList);
- }
- }
- }
- }
- /// <summary>
- /// 协同加载AssetBundle
- /// </summary>
- /// <param name="_name">名称,不包含后缀</param>
- /// <param name="ac"> Lambda返回值</param>
- /// <returns>返回资源AssetBundle</returns>
- private IEnumerator IELoadAssetBundleWWW(string _name, Action<AssetBundle> ac, bool multiLoad = true)
- {
- WWW www = new WWW(GetPath(_name));
- yield return www;
- // 加载完成
- mLoadingAssetList.Remove(_name);
- if (www != null && www.assetBundle != null)
- {
- mAssetBundleList.Add(_name, www.assetBundle);
- ac(www.assetBundle);
- // 返回相同AC
- if (mSameAssetBundleAcList.ContainsKey(_name))
- {
- for (int i = 0; i < mSameAssetBundleAcList[_name].Count; i++)
- {
- if (multiLoad)
- {
- mSameAssetBundleAcList[_name][i](www.assetBundle);
- }
- }
- mSameAssetBundleAcList[_name].Clear();
- }
- }
- else
- {
- ac(null);
- // 返回相同AC
- if (mSameAssetBundleAcList.ContainsKey(_name))
- {
- for (int i = 0; i < mSameAssetBundleAcList[_name].Count; i++)
- {
- if (multiLoad)
- {
- mSameAssetBundleAcList[_name][i](null);
- }
- }
- mSameAssetBundleAcList[_name].Clear();
- }
- Debug.LogWarning("没有找到资源__" + _name);
- }
- }
- /// <summary>
- /// 同步读取资源
- /// </summary>
- /// <param name="_name">资源名称(不带后缀)</param>
- /// <returns>Object</returns>
- private void LoadAssetBundleSynchro(string _name, Action<AssetBundle> ac)
- {
- if (string.IsNullOrEmpty(_name))
- {
- ac(null);
- return;
- }
- String path = GetPath2(_name);
- if (mAssetBundleList.ContainsKey(_name))
- {
- ac(mAssetBundleList[_name]);
- }
- else
- {
- AssetBundle ab = AssetBundle.LoadFromFile(path);
- if (ab != null)
- {
- mAssetBundleList.Add(_name, ab);
- ac(ab);
- }
- else
- {
- ac(null);
- }
- }
- }
- /// <summary>
- /// 异步读取资源
- /// </summary>
- /// <param name="_name">名称,不包含后缀</param>
- /// <param name="ac"> Lambda返回值(必须经过实例化才可用)</param>
- private void LoadAssetBundleAsync(string _name, Action<AssetBundle> ac, bool multiLoad = true)
- {
- if (string.IsNullOrEmpty(_name))
- {
- ac(null);
- return;
- }
- if (mAssetBundleList.ContainsKey(_name))
- {
- ac(mAssetBundleList[_name]);
- }
- else
- {
- if (mLoadingAssetList.Contains(_name) == false)
- {
- mLoadingAssetList.Add(_name);
- StartCoroutine(IELoadAssetBundleAsync(_name, ac, multiLoad));
- }
- else
- {
- if (mSameAssetBundleAcList.ContainsKey(_name))
- {
- if (mSameAssetBundleAcList[_name].Contains(ac) == false)
- {
- mSameAssetBundleAcList[_name].Add(ac);
- }
- }
- else
- {
- List<Action<AssetBundle>> acList = new List<Action<AssetBundle>>();
- acList.Add(ac);
- mSameAssetBundleAcList.Add(_name, acList);
- }
- }
- }
- }
- /// <summary>
- /// 协同加载AssetBundle
- /// </summary>
- /// <param name="_name">名称,不包含后缀</param>
- /// <param name="ac"> Lambda返回值</param>
- /// <returns>返回资源AssetBundle</returns>
- private IEnumerator IELoadAssetBundleAsync(string _name, Action<AssetBundle> ac, bool multiLoad = true)
- {
- String path = GetPath2(_name);
- AssetBundleCreateRequest abcRequest = AssetBundle.LoadFromFileAsync(path);
- // 等待加载完成
- while (abcRequest != null && abcRequest.isDone == false)
- {
- mLoadProgress = abcRequest.progress;
- yield return new WaitForSeconds(0.015f);
- }
- mLoadProgress = 1.0f;
- // 加载完成
- mLoadingAssetList.Remove(_name);
- if (abcRequest != null && abcRequest.assetBundle != null)
- {
- mAssetBundleList.Add(_name, abcRequest.assetBundle);
- ac(abcRequest.assetBundle);
- // 返回相同AC
- if (mSameAssetBundleAcList.ContainsKey(_name))
- {
- for (int i = 0; i < mSameAssetBundleAcList[_name].Count; i++)
- {
- if (multiLoad)
- {
- mSameAssetBundleAcList[_name][i](abcRequest.assetBundle);
- }
- }
- mSameAssetBundleAcList[_name].Clear();
- }
- }
- else
- {
- ac(null);
- // 返回相同AC
- if (mSameAssetBundleAcList.ContainsKey(_name))
- {
- for (int i = 0; i < mSameAssetBundleAcList[_name].Count; i++)
- {
- mSameAssetBundleAcList[_name][i](null);
- }
- mSameAssetBundleAcList[_name].Clear();
- }
- Debug.LogWarning("没有找到资源__" + _name);
- }
- }
- /// <summary>
- /// 加载外部纯文本 by:jgao
- /// </summary>
- /// <param name="strName">url路径</param>
- /// <param name="ac">回调</param>
- /// <param name="postData">数据</param>
- public void LoadText(string strName, Action<string> ac, Dictionary<string, string> postData = null)
- {
- if (strName == string.Empty)
- {
- return;
- }
- StartCoroutine(IELoadText(strName, ac, postData));
- }
- /// <summary>
- /// 加载外部纯文本 by:jgao
- /// </summary>
- /// <param name="name">名称</param>
- /// <param name="ac">回调</param>
- /// <param name="postData">数据</param>
- /// <returns>返回资源AssetBundle</returns>
- private IEnumerator IELoadText(string name, Action<string> ac, Dictionary<string, string> postData)
- {
- WWW www;
- if (postData == null)
- {
- www = new WWW(name);
- }
- else
- {
- WWWForm form = new WWWForm();
- foreach (KeyValuePair<string, string> kv in postData)
- {
- form.AddField(kv.Key, kv.Value);
- }
- www = new WWW(name, form);
- }
- DateTime timer = DateTime.MinValue;
- float lastProgress = 0.0f;
- // 超时检测(苹果支付时不考虑超时问题)
- while (false == www.isDone && postData == null)
- {
- if (www.error != null)
- {
- Debug.LogWarning("网络超时,请检查网络后重试!");
- yield break;
- }
- if (www.progress.Equals(0))
- {
- if (lastProgress.Equals(0) && timer == DateTime.MinValue)
- {
- lastProgress = www.progress;
- timer = DateTime.Now;
- }
- else
- {
- double diff = (DateTime.Now - timer).TotalMilliseconds;
- if (diff > Const.NetDefaultTimeOut)
- {
- Debug.LogWarning("网络超时,请检查网络后重试!");
- yield break;
- }
- }
- }
- else
- {
- // 进度值一直不动,超过10s认为断网
- if (www.progress.Equals(lastProgress))
- {
- double diff = (DateTime.Now - timer).TotalMilliseconds;
- if (diff > Const.NetDefaultTimeOut)
- {
- Debug.LogWarning("网络超时,请检查网络后重试!");
- yield break;
- }
- }
- else
- {
- // 正常下载,重置状态
- lastProgress = www.progress;
- timer = DateTime.Now;
- }
- }
- yield return null;
- }
- yield return www;
- if (www.error == null)
- {
- ac(www.text);
- www.Dispose();
- }
- else
- {
- Debug.LogWarning(www.error);
- }
- }
- /// <summary>
- /// 卸载AssetBundle
- /// </summary>
- /// <param name="_name">名称,不包含后缀</param>
- /// <param name="forceUnload">强力卸载,为真时即便是常驻内存也将被卸载</param>
- /// <returns>是否卸载成功</returns>
- public bool UnloadAssetBundle(string _name)
- {
- if (mAssetBundleList.ContainsKey(_name))
- {
- mAssetBundleList[_name].Unload(true);
- mAssetBundleList.Remove(_name);
- return true;
- }
- return false;
- }
- /// <summary>
- /// 清除所有AssetBundle
- /// </summary>
- /// <param name="forceUnload">强力卸载,为真时即便是常驻内存也将被卸载</param>
- public void ClearAllAssetBundle()
- {
- List<string> KeyList = new List<string>(mAssetBundleList.Keys);
- for (int i = 0; i < KeyList.Count; i++)
- {
- UnloadAssetBundle(KeyList[i]);
- }
- }
- /// <summary>
- /// 获取异步加载资源路径(用于 www 加载)
- /// </summary>
- /// <param name="strName">资源名称</param>
- /// <param name="fileSuffix">文件后缀</param>
- /// <returns>返回实际路径</returns>
- public static string GetPath(string strName, string fileSuffix = ".unity3d")
- {
- string strPath = string.Empty;
- string persistentDataPath = Application.persistentDataPath + "/Res/" + strName + fileSuffix;
- string streamingAssetsPath = Application.streamingAssetsPath + "/Res/" + strName + fileSuffix;
- if (Application.isEditor)
- {
- if (File.Exists(streamingAssetsPath))
- {
- strPath = "file://" + streamingAssetsPath;
- }
- else
- {
- strPath = "file:///" + persistentDataPath;
- }
- }
- else
- {
- if (File.Exists(persistentDataPath))
- {
- strPath = "file://" + persistentDataPath;
- }
- else
- {
- if (RuntimePlatform.Android == Application.platform)
- {
- strPath = streamingAssetsPath;
- }
- else
- {
- strPath = "file://" + streamingAssetsPath;
- }
- }
- }
- return strPath;
- }
- /// <summary>
- /// 获取加载资源路径(用于 LoadFromFile 的同步和异步加载)
- /// </summary>
- /// <param name="strName">资源名称</param>
- /// <param name="fileSuffix">文件后缀</param>
- /// <returns>返回实际路径</returns>
- public static string GetPath2(string strName, string fileSuffix = ".unity3d")
- {
- string path = "";
- string persistentDataPath = Application.persistentDataPath + "/Res/" + strName + fileSuffix;// 沙盒路径
- string streamingAssetsPath = Application.streamingAssetsPath + "/Res/" + strName + fileSuffix;
- if (Application.isEditor)
- {
- if (File.Exists(streamingAssetsPath))
- {
- path = streamingAssetsPath;
- }
- else
- {
- path = persistentDataPath;
- }
- }
- else
- {
- if (File.Exists(persistentDataPath))
- {
- path = persistentDataPath;
- }
- else
- {
- if (RuntimePlatform.Android == Application.platform)
- {
- path = Application.dataPath + "!assets" + "/Res/" + strName + fileSuffix;
- }
- else
- {
- path = streamingAssetsPath;
- }
- }
- }
- return path;
- }
- /// <summary>
- /// 获取加载资源路径(用于 LoadFromFile 的同步和异步加载)
- /// </summary>
- /// <param name="strName">资源名称</param>
- /// <param name="fileSuffix">文件后缀</param>
- /// <returns>返回实际路径</returns>
- public static string GetNewPath(string strName, string fileSuffix = ".unity3d")
- {
- string path = "";
- //string persistentDataPath = Application.persistentDataPath + "/NewRes/" + strName + fileSuffix;// 沙盒路径
- string streamingAssetsPath = Application.streamingAssetsPath + "/NewRes/" + strName + fileSuffix;
- if (Application.isEditor)
- {
- //if (File.Exists(streamingAssetsPath))
- {
- path = streamingAssetsPath;
- }
- //else
- //{
- // path = persistentDataPath;
- //}
- }
- else
- {
- //if (File.Exists(persistentDataPath))
- //{
- // path = persistentDataPath;
- //}
- //else
- {
- if (RuntimePlatform.Android == Application.platform)
- {
- path = Application.dataPath + "!assets" + "/NewRes/" + strName + fileSuffix;
- }
- else
- {
- path = streamingAssetsPath;
- }
- }
- }
- return path;
- }
- }
|