static public void setImageSpriteByAssetBundle(Image image, string assetbundleName, string imageName) { //#if UNITY_EDITOR // string[] realPaths = AssetDatabase.GetAssetPathsFromAssetBundle(assetbundleName); // foreach (var path in realPaths) // { // string assetName = Path.GetFileNameWithoutExtension(path); // if (assetName == imageName) // { // image.sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path); // break; // } // } // return; //#endif AssetBundle ab = AssetBundlesManager.getInstance().getAssetBundlesDataByName(assetbundleName); if (ab != null) { Sprite sprite = ab.LoadAsset <Sprite>(imageName); if (sprite != null) { image.sprite = sprite; } else { Debug.Log("---------------------不存在此图片:" + assetbundleName + "." + imageName); } } else { Debug.Log("---------------------不存在此ab包:" + assetbundleName); } }
private void InitMyRank() { var Text_Ranking = MyRank.transform.Find("Text_Ranking"); Text RankText = Text_Ranking.GetComponent <Text>(); var Image_Head = MyRank.transform.Find("Image_Head"); var Name = MyRank.transform.Find("Name"); var Count = MyRank.transform.Find("Count"); var Ranking = MyRank.transform.Find("Ranking"); var Image_icon = MyRank.transform.Find("Image_icon"); var Image_vip = MyRank.transform.Find("Image_vip").GetComponent <Image>(); CommonUtil.setImageSpriteByAssetBundle(Image_vip, "vip.unity3d", "user_vip_" + VipUtil.GetVipLevel(UserData.rechargeVip)); Image rankImage = Ranking.GetComponent <Image>(); Count.GetComponent <Text>().text = "" + UserData.medal; string s = "Sprites/Head/head_" + UserData.head; LogUtil.Log("head" + s); Image_Head.GetComponent <Image>().sprite = AssetBundlesManager.getInstance().getAssetBundlesDataByName("head.unity3d").LoadAsset <Sprite>(UserData.head); Name.GetComponent <Text>().text = UserData.name; if (VipUtil.GetVipLevel(UserData.rechargeVip) > 0) { Name.GetComponent <Text>().color = new Color(253 / (float)255, 239 / (float)255, 82 / (float)255, 1); } CommonUtil.setImageSpriteByAssetBundle(MyRank.GetComponent <Image>(), "main.unity3d", "di7"); Image_icon.GetComponent <Image>().sprite = Resources.Load <Sprite>("Sprites/Icon/Prop/icon_huizhang"); if (string.IsNullOrEmpty(mymedalRank)) { Text_Ranking.gameObject.SetActive(true); Ranking.gameObject.SetActive(false); RankText.text = "无"; } else { int i = int.Parse(mymedalRank) - 1; if (i < 3) { Text_Ranking.gameObject.SetActive(false); Ranking.gameObject.SetActive(true); if (i == 1) { CommonUtil.setImageSpriteByAssetBundle(rankImage, "main.unity3d", "award_2"); } else if (i == 2) { CommonUtil.setImageSpriteByAssetBundle(rankImage, "main.unity3d", "award_3"); } } else { Text_Ranking.gameObject.SetActive(true); Ranking.gameObject.SetActive(false); RankText.text = i + 1 + ""; } } }
private void Awake() { if (ClearCach) { Caching.CleanCache(); } Instance = this; }
static public void setImageSpriteByAssetBundle(Image image, string assetbundleName, string imageName) { AssetBundle ab = AssetBundlesManager.getInstance().getAssetBundlesDataByName(assetbundleName); if (ab != null) { image.sprite = ab.LoadAsset <Sprite>(imageName); } }
public static AssetBundlesManager getInstance() { if (s_instance == null) { s_instance = new AssetBundlesManager(); } return(s_instance); }
static public GameObject getGameObjectByAssetBundle(string assetbundleName, string objName) { AssetBundle ab = AssetBundlesManager.getInstance().getAssetBundlesDataByName(assetbundleName); if (ab != null) { return(ab.LoadAsset <GameObject>(objName)); } return(null); }
static public Sprite getImageSpriteByAssetBundle(string assetbundleName, string imageName) { AssetBundle ab = AssetBundlesManager.getInstance().getAssetBundlesDataByName(assetbundleName); if (ab != null) { return(ab.LoadAsset <Sprite>(imageName)); } return(null); }
static public AudioClip getAudioClipByAssetBundle(string assetbundleName, string audioName) { AssetBundle ab = AssetBundlesManager.getInstance().getAssetBundlesDataByName(assetbundleName); if (ab != null) { return(ab.LoadAsset <AudioClip>(audioName)); } return(null); }
static public bool checkSpriteIsExistByAssetBundle(string assetbundleName, string imageName) { AssetBundle ab = AssetBundlesManager.getInstance().getAssetBundlesDataByName(assetbundleName); if (ab != null) { Sprite sprite = ab.LoadAsset <Sprite>(imageName); if (sprite != null) { return(true); } } return(false); }
/// <summary> /// 加载函数(单个:同步). /// </summary> /// <returns>加载对象</returns> /// <param name="iPath">路径.</param> public static T Load <T>(string iPath) where T : UnityEngine.Object { if (true == string.IsNullOrEmpty(iPath)) { return(default(T)); } T objRet = default(T); string _fileName = AssetBundlesManager.GetKeyOfMapByFilePath(iPath); if (true == string.IsNullOrEmpty(_fileName)) { return(default(T)); } objRet = AssetBundlesManager.Instance.LoadFromAssetBundle <T>(_fileName); if (null == objRet) { objRet = ResourcesLoad.Load <T>(iPath); } return(objRet); }
/// <summary> /// 加载函数(单个:同步). /// </summary> /// <returns>加载对象</returns> /// <param name="iPath">路径.</param> public static T Load <T>(string iPath) where T : UnityEngine.Object { if (string.IsNullOrEmpty(iPath)) { return(default(T)); } Object objRet; #if BUILD_DEBUG objRet = ResourcesLoad.Load <T>(iPath); if (null != objRet) { return((T)objRet); } #endif var fileName = AssetBundlesManager.GetKeyOfMapByFilePath(iPath, typeof(T)); if (string.IsNullOrEmpty(fileName)) { Loger.Error($"DataLoader::Load(T):File not exist in bundles maps!!(path:{iPath})"); return(null); } objRet = AssetBundlesManager.Instance.LoadFromAssetBundle <T>(fileName); if (null != objRet) { return((T)objRet); } var path = ResourcesLoad.ToDefaultPath(iPath); objRet = ResourcesLoad.Load <T>(path); // if(null == objRet) { // UtilsLog.Error("DataLoader", // "Load(T):Failed!!(path:{0})", iPath); // } return((T)objRet); }
static public AudioClip getAudioClipByAssetBundle(string assetbundleName, string audioName) { //#if UNITY_EDITOR // string[] realPaths = AssetDatabase.GetAssetPathsFromAssetBundle(assetbundleName); // foreach (var path in realPaths) // { // string assetName = Path.GetFileNameWithoutExtension(path); // if (assetName == audioName) // { // return AssetDatabase.LoadAssetAtPath<AudioClip>(path); // } // } // return null; //#endif AssetBundle ab = AssetBundlesManager.getInstance().getAssetBundlesDataByName(assetbundleName); if (ab != null) { return(ab.LoadAsset <AudioClip>(audioName)); } return(null); }
private void VersionBack(string s, string data) { LogUtil.Log(data); try { //网络 webVersionConfig = LitJson.JsonMapper.ToObject <VersionConfig>(data); Dictionary <string, FileVersionInfo> webDic = new Dictionary <string, FileVersionInfo>(); foreach (var item in webVersionConfig.FileVersionInfos) { webDic.Add(item.File, item); webVersionConfig.TotalSize += item.Size; } //本地 string versionPath = Path.Combine(fileRootPath, "Version.txt"); if (!File.Exists(versionPath)) { for (int i = 0; i < webVersionConfig.FileVersionInfos.Count; i++) { if (webVersionConfig.FileVersionInfos[i].File.EndsWith(".unity3d")) { m_needDownlist.Add(webVersionConfig.FileVersionInfos[i].File); } } LogUtil.Log("本地version不存在"); } else { LogUtil.Log("本地version存在"); VersionConfig localVersionConfig = LitJson.JsonMapper.ToObject <VersionConfig>(File.ReadAllText(versionPath)); Dictionary <string, FileVersionInfo> localDic = new Dictionary <string, FileVersionInfo>(); foreach (var item in localVersionConfig.FileVersionInfos) { localDic.Add(item.File, item); localVersionConfig.TotalSize += item.Size; } // 先删除服务器端没有的ab foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileVersionInfos) { if (webDic.ContainsKey(fileVersionInfo.File)) { continue; } string abPath = Path.Combine(fileRootPath, fileVersionInfo.File); File.Delete(abPath); } // 再下载 foreach (FileVersionInfo fileVersionInfo in webVersionConfig.FileVersionInfos) { FileVersionInfo localVersionInfo; if (localDic.TryGetValue(fileVersionInfo.File, out localVersionInfo)) { if (fileVersionInfo.MD5 == localVersionInfo.MD5) { string filePath = fileRootPath + "/" + fileVersionInfo.File; //缓存ab包 if (filePath.EndsWith(".unity3d")) { AssetBundle loadFromFile = AssetBundle.LoadFromFile(fileRootPath + "/" + fileVersionInfo.File); AssetBundlesManager.getInstance().ABDic.Add(fileVersionInfo.File, loadFromFile); } continue; } } if (fileVersionInfo.File == "Version.txt") { continue; } if (fileVersionInfo.File.EndsWith(".unity3d")) { m_needDownlist.Add(fileVersionInfo.File); } } } if (m_needDownlist.Count > 0) { GameUtil.showGameObject(gameObject); startDown(); InvokeRepeating("onInvoke", 0.5f, 0.5f); } } catch (Exception e) { LogUtil.LogError(e.ToString()); } }
IEnumerator onStartDown() { string url; #if UNITY_ANDROID url = OtherData.getWebUrl() + "AssetBundles/android/" + m_needDownlist[m_curDownIndex]; #endif #if UNITY_IPHONE url = OtherData.getWebUrl() + "AssetBundles/ios/" + m_needDownlist[m_curDownIndex]; #endif #if UNITY_STANDALONE_WIN url = OtherData.getWebUrl() + "AssetBundles/pc/" + m_needDownlist[m_curDownIndex]; #endif string ab_name = m_needDownlist[m_curDownIndex]; LogUtil.Log("下载ab:" + ab_name + " " + url); UnityWebRequest request = UnityWebRequest.Get(url); yield return(request.Send()); //缓存ab包 AssetBundle myLoadedAssetBundle = AssetBundle.LoadFromMemory(request.downloadHandler.data); AssetBundlesManager.getInstance().ABDic.Add(ab_name, myLoadedAssetBundle); //保存ab到本地 { byte[] bytes = request.downloadHandler.data; string filePath = fileRootPath + "/" + ab_name; if (!Directory.Exists(Path.GetDirectoryName(filePath))) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)); } using (FileStream fs = new FileStream(filePath, FileMode.Create)) { fs.Write(bytes, 0, bytes.Length); } } { if (m_curDownIndex < (m_needDownlist.Count - 1)) { ++m_curDownIndex; startDown(); } else { LogUtil.Log("下载完毕"); CancelInvoke("onInvoke"); GameUtil.hideGameObject(gameObject); OtherData.s_loginScript.netDataDown(); using (FileStream fs = new FileStream(Path.Combine(fileRootPath, "Version.txt"), FileMode.Create)) using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(LitJson.JsonMapper.ToJson(webVersionConfig)); } } } }