public Object LoadPrefabImm(string prefabPathName, bool bIntoCache = false) { Object asset = null; if (prefabPathName.Length < MinValidPathLen) { return(asset); } string assetPathName = prefabPathName; string assetPathNameExt = Path.GetExtension(prefabPathName); if (!String.IsNullOrEmpty(assetPathNameExt)) { assetPathName = (Path.GetDirectoryName(prefabPathName) + "/" + Path.GetFileNameWithoutExtension(prefabPathName)); } if (!AssetsPool.TryGetAsset(assetPathName, out asset)) { try{ #if UNITY_EDITOR //Debug.LogError("Load uncached prefab Imm :"+assetPathName); #endif asset = Resources.Load(assetPathName); }catch (Exception e) { Debug.LogError("[AssetsLoader]Failed to load " + assetPathName + " with error: " + e.ToString()); } if (bIntoCache && asset != null) { AssetsPool.RegisterAsset(assetPathName, asset); } } return(asset); }
public GameObject InstantiateAssetImm(string pathName, Vector3 position, Quaternion rotation, Vector3 scale, bool bIntoCache = false) { GameObject retGo = null; if (pathName.Length < MinValidPathLen) { return(retGo); } try{ Object asset = null; string assetPathName = pathName; string assetPathNameExt = Path.GetExtension(pathName); bool bAssetBundle = assetPathNameExt.Equals(AssetBundleExtension); if (!bAssetBundle && !String.IsNullOrEmpty(assetPathNameExt)) { assetPathName = (Path.GetDirectoryName(pathName) + "/" + Path.GetFileNameWithoutExtension(pathName)); } if (!AssetsPool.TryGetAsset(assetPathName, out asset)) { if (bAssetBundle) { FileStream fs = new FileStream(GameConfig.AssetBundlePath + assetPathName, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader r = new BinaryReader(fs); byte[] raw_data = r.ReadBytes((int)(fs.Length)); r.Close(); fs.Close(); #if UNITY_5_3 || UNITY_5_4 AssetBundle bundle = AssetBundle.LoadFromMemory(raw_data); #else AssetBundle bundle = AssetBundle.CreateFromMemoryImmediate(raw_data); #endif asset = bundle.LoadAsset <GameObject>(Path.GetFileNameWithoutExtension(assetPathName)); bundle.Unload(false); } else { #if UNITY_EDITOR if (assetPathName.StartsWith("../")) { asset = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/" + assetPathName.Substring(3), typeof(GameObject)); } else #endif { asset = Resources.Load(assetPathName); } } if (asset != null && bIntoCache) { AssetsPool.RegisterAsset(assetPathName, asset); } } if (asset != null) { retGo = Instantiate(asset, position, rotation) as GameObject; retGo.transform.localScale = scale; } } catch (Exception) { Debug.Log("Error: Faile to LoadAssetImm " + pathName); } return(retGo); }
//System.Diagnostics.Stopwatch _sw = new System.Diagnostics.Stopwatch(); IEnumerator ProcessReqs(Stack <AssetReq> assetReqList, string assetBundlePath) { while (true) { if (assetReqList.Count > 0) { AssetReq req = assetReqList.Pop(); if (req.ProcLevel <= AssetReq.ProcLvl.DoNothing) { continue; } /* test code for LoadAssetImm * GameObject goo = LoadAssetImm(req.pathName, req.pos.Position(), req.pos.Rotation(), req.pos.Scale()); * if(goo != null){ req.OnFinish(goo); } * yield return 0; * continue; */ //_sw.Reset(); //_sw.Start(); Object asset = null; string assetPathName = req.PathName; string assetPathNameExt = Path.GetExtension(req.PathName); bool bAssetBundle = assetPathNameExt.Equals(AssetBundleExtension); if (!bAssetBundle && !String.IsNullOrEmpty(assetPathNameExt)) { assetPathName = (Path.GetDirectoryName(req.PathName) + "/" + Path.GetFileNameWithoutExtension(req.PathName)); } if (!AssetsPool.TryGetAsset(assetPathName, out asset)) { if (bAssetBundle) { WWW www = WWW.LoadFromCacheOrDownload("file://" + assetBundlePath + assetPathName, AssetsPool.s_Version); yield return(www); if (www.error != null) { Debug.LogError(www.error); } else { AssetBundle assetBundle = www.assetBundle; AssetBundleRequest request = assetBundle.LoadAssetAsync(Path.GetFileNameWithoutExtension(assetPathName), typeof(GameObject)); yield return(request); if (request != null) { asset = request.asset; } request = null; assetBundle.Unload(false); } if (www != null) { www.Dispose(); www = null; } } else // Treat as prefab { #if UNITY_EDITOR if (assetPathName.StartsWith("../")) { asset = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/" + req.PathName.Substring(3), typeof(GameObject)); } else #endif { #if UNITY_EDITOR //Debug.LogError("Load uncached prefab :"+assetPathName); #endif ResourceRequest resReq = Resources.LoadAsync(assetPathName); while (!resReq.isDone) { yield return(null); } asset = resReq.asset; } } if (asset != null && req.NeedCaching) { AssetsPool.RegisterAsset(assetPathName, asset); } } //_sw.Stop(); //Debug.LogError("Load "+assetPathName+":"+_sw.ElapsedMilliseconds); if (asset != null && req.ProcLevel >= AssetReq.ProcLvl.DoInstantiation) { GameObject go = Instantiate(asset, req.Prs.Position(), req.Prs.Rotation()) as GameObject; if (go != null) { Profiler.BeginSample("AssetsLoader:Instantiate " + assetPathName); go.transform.localScale = req.Prs.Scale(); req.OnFinish(go); Profiler.EndSample(); } } } yield return(null); } }