コード例 #1
0
        public IEnumerator Init()
        {
            m_AssetBundleManifest = null;

            string manifestPath = IcConfig.GetPlatform();
            int    version      = 1;//AssetBundleManager.GetResources().GetRunningResourceVer();

            // Set base downloading url.
            m_BaseDownloadingWWWURL = IcResources.GetPersistentDataPath_WWW();//GetPersistentDataPath_WWW();
            m_BaseDownloadingFSURL  = IcResources.GetPersistentDataPath_FileStream();

            //// Set base downloading url.
            //string relativePath = GetRelativePath();
            //m_BaseDownloadingURL = relativePath + kAssetBundlesPath + platformFolderForAssetBundles + "/";

            // Initialize AssetBundleManifest which loads the AssetBundleManifest object.
            var request = Initialize(manifestPath, version);

            if (request != null)
            {
                yield return(StartCoroutine(request));
            }

            UnloadAssetBundle(manifestPath, false);//add by xlm
        }
コード例 #2
0
        /// <summary>
        /// 有资源依赖的情况
        /// </summary>
        /// <returns></returns>
        public Object GetPrefabDepend(string assetPath, string extension = IcConfig.EXTENSION_PREFAB)
        {
            string      m_loaderPath = null;
            Object      obj          = null;
            AssetBundle ab           = null;

            if (string.IsNullOrEmpty(assetPath))
            {
                return(null);
            }


            string dir;
            string name;

            CUtil.SplitPath(assetPath, out dir, out name);
            stResourcePath resourcePath = AssetBundleManager.GetResources().GetResourcePath(dir, name, extension);

            if (!AssetBundleManager.Instance.m_isLocaAsset)
            {
                if (resourcePath.asset != null)
                {
                    //(assetbundle 已经异步load好了)
                    obj = resourcePath.asset;
                }
                else if (resourcePath.bAssetbundle)
                {
                    string path = resourcePath.path;
                    AssetBundleManager.GetAssetBundleManager().LoadAssetSynchronization(path, resourcePath.ver, name, typeof(Object), out obj, out ab);
                    if (!m_loadAb.Contains(resourcePath.path) && ab != null)
                    {
                        m_loadAb.Add(resourcePath.path);
                    }
                }
                else
                {
                    string path = resourcePath.AssetPath;
                    AssetBundleManager.GetAssetBundleManager().LoadAssetSynchronization(path, resourcePath.ver, name, typeof(Object), out obj, out ab);
                    if (!m_loadAb.Contains(resourcePath.path) && ab != null)
                    {
                        m_loadAb.Add(resourcePath.path);
                    }
                }
            }
            else
            {
                string path = IcResources.GetEditorAssetResourcePath(dir, name, extension);
                #if UNITY_EDITOR
                obj = AssetDatabase.LoadAssetAtPath(path, typeof(Object));
                #endif

                if (obj == null)
                {
                    //同步
                    path = IcResources.GetEditorResourcePath(dir, name, extension);
                    obj  = Resources.Load(path, typeof(Object));
                }
            }
            return(obj);
        }
コード例 #3
0
        public void LoadAssetbundleScene(string path, string levelName, bool isAdditive)
        {
            UnLoadAssetbundleDirectly(path);

            AssetBundle ab = IcResources.LoadAssetbundleFromFile(path);

            if (ab != null)
            {
                //if (isAdditive)
                //    Application.LoadLevelAdditive(levelName);
                //else
                //    Application.LoadLevel(levelName);

                if (isAdditive)
                {
                    UnityEngine.SceneManagement.SceneManager.LoadScene(levelName, UnityEngine.SceneManagement.LoadSceneMode.Additive);
                }
                else
                {
                    UnityEngine.SceneManagement.SceneManager.LoadScene(levelName, UnityEngine.SceneManagement.LoadSceneMode.Single);
                }

                stLoadActor pInfo = new stLoadActor();
                pInfo.assetName    = levelName;
                pInfo.path         = path;
                pInfo.pObj         = null;
                pInfo.m_iCount     = 1;
                pInfo.pAsset       = ab;
                m_strAllLoad[path] = pInfo;
            }
            else
            {
                UnityEngine.Debug.LogError("LoadAssetbundleSceneDirectly failed. levelName=" + levelName);
            }
        }
コード例 #4
0
        //bSpecial : 非编辑器模式, alaways load from resources
        private T Load <T>(string relativePath, string fileExtention, bool bSpecial = false) where T : UnityEngine.Object
        {
            T asset = default(T);

            //#if UNITY_EDITOR
            //        if (AssetBundleManager.GetResources().IsLoadAssetBundle())
            //        {
            //#endif
            if (bSpecial)
            {   //只从Resources里读取, 用于gameConfig
                //load from resource
                asset = Resources.Load(relativePath, typeof(T)) as T;
            }
            else
            {
                string dir;
                string name;
                CUtil.SplitPath(relativePath, out dir, out name);

                stResourcePath resourcePath = GetResourcePath(dir, name, fileExtention);

                if (!AssetBundleManager.Instance.m_isLocaAsset)
                {
                    if (resourcePath.asset != null)
                    {
                        //(assetbundle 已经异步load好了)
                        asset = resourcePath.asset as T;
                    }
                    else if (resourcePath.bAssetbundle)
                    {
                        //load assetbundle
                        //同步load的, 不支持异步assetbundleload, 应该提前load好, 由resourcePath.asset返回

                        //使用CreateFromMemoryImmediate和createFromFile都会引起crash (unity3d, 5.0.4)
                        //Unity5.4以上使用AssetBundle.LoadFromFile
                        asset = LoadAssetbundle <T>(resourcePath.path, resourcePath.ver, name);
                    }
                    else
                    {
                        //load from resource
                        asset = Resources.Load(resourcePath.path, typeof(T)) as T;
                    }
                }
                else
                {
                    #if UNITY_EDITOR
                    if (asset == null)
                    {
                        string path = IcResources.GetEditorAssetResourcePath(dir, name, fileExtention);
                        asset = AssetDatabase.LoadAssetAtPath(path, typeof(T)) as T;
                    }
                    #endif
                }
            }

            return(asset);
        }
コード例 #5
0
        public Object LoadAssetbundleDirectly(string path, string assetName, System.Type type)
        {
            //  if (ajfs != null)
            //     return ajfs;

            stLoadActor pLoadInfo = null;

            if (m_strAllLoad.TryGetValue(path, out pLoadInfo))
            {
                if (pLoadInfo != null)
                {
                    pLoadInfo.m_iCount++;
                    return(pLoadInfo.pObj);
                }
                return(null);
            }

            AssetBundle ab = IcResources.LoadAssetbundleFromFile(path);

            if (ab != null)
            {
                Object ajfs = ab.LoadAsset(assetName, type);
                if (ajfs != null)
                {
                    stLoadActor pInfo = new stLoadActor();
                    pInfo.assetName    = assetName;
                    pInfo.path         = path;
                    pInfo.pObj         = ajfs;
                    pInfo.m_iCount     = 1;
                    pInfo.pAsset       = ab;
                    m_strAllLoad[path] = pInfo;
                }
                return(ajfs);
            }
            return(null);
        }
コード例 #6
0
        // Where we actuall call WWW to download the assetBundle.\
        //return value: true 需要loaddependency, false 不需要(只有在已经处于错误列表里的时候不需要)
        protected bool LoadAssetBundleInternal(string assetBundleName, int version, ThreadPriority tp, bool isLoadingAssetBundleManifest)
        {
            string error;

            if (m_DownloadingErrors.TryGetValue(assetBundleName, out error))
            {
                return(false);
            }

            // Already loaded.
            LoadedAssetBundle bundle = null;

            m_LoadedAssetBundles.TryGetValue(assetBundleName, out bundle);
            if (bundle != null)
            {
                bundle.m_ReferencedCount++;
                return(true);
            }

            //// @TODO: Do we need to consider the referenced count of WWWs?
            //// In the demo, we never have duplicate WWWs as we wait LoadAssetAsync()/LoadLevelAsync() to be finished before calling another LoadAssetAsync()/LoadLevelAsync().
            //// But in the real case, users can call LoadAssetAsync()/LoadLevelAsync() several times then wait them to be finished which might have duplicate WWWs.
            //if (m_DownloadingWWWs.ContainsKey(assetBundleName) )
            //    return true;

            LoadingWWW www = null;

            m_DownloadingWWWs.TryGetValue(assetBundleName, out www);
            if (www != null)
            {
                if (www.m_www != null && www.m_www.threadPriority < tp)
                {
                    www.m_www.threadPriority = tp;
                }

                www.m_ReferencedCount++;
                return(true);
            }

            //if (!IcConfig.UNCOMPRESS_ASSETBUNDLE)//version 5.0.4 CreateFromFile 会导致失败 "Error while reading AssetBundle header!", 但www不会出该错
            if (false)
            {
                //WWW download = null;
                string url;
                if (version == 0)
                {
                    url = m_BaseDownloadingWWWURL + assetBundleName;
                }
                else
                {
                    url = m_BaseDownloadingWWWURL + assetBundleName;
                }

                //#if UNITY_EDITOR
                //IcLog.Log(string.Format("Start LoadAssetBundleInternal:{0},  ver={1}", assetBundleName, version));
                //#endif

                //// For manifest assetbundle, always download it as we don't have hash for it.
                //if (isLoadingAssetBundleManifest || m_AssetBundleManifest == null)
                //    download = new WWW(url);
                //else
                //{
                //    Hash128 h = m_AssetBundleManifest.GetAssetBundleHash(assetBundleName);
                //    download = WWW.LoadFromCacheOrDownload(url, h, 0);//new WWW(url); //

                //    //download = new WWW(url);
                //}

                //不使用Cache
                WWW download = new WWW(url);
                download.threadPriority = tp;

                m_DownloadingWWWs.Add(assetBundleName, new LoadingWWW(download));
            }
            #region 另外一种加载方式
            else//暂时去掉警告
            {
                string url;
                string assetbundlePath = IcConfig.ASSETS + "/" + assetBundleName;
                if (AssetBundleManager.GetResources().m_mapResourceVer.ContainsKey(assetBundleName))
                {
                    url = m_BaseDownloadingFSURL + assetBundleName;
                    if (!File.Exists(url))
                    {
                        url = IcResources.StreamingLoadFromFilePathURL() + "/" + assetBundleName;
                    }
                }
                else
                {
                    url = IcResources.StreamingLoadFromFilePathURL() + "/" + assetBundleName;
                }

                //IcLog.Log(string.Format("Start LoadAssetBundleInternal:{0},  ver={1}", assetBundleName, version));

                //最快的方式, 但是不压缩的话, 资源接近1G, 占用太大
                AssetBundle ab = AssetBundleManager.GetResources().LoadAssetbundleDirectlyReturnAB(url, assetBundleName, typeof(Object));

                //网上有评测说比 WWW.LoadFromCacheOrDownload还要慢, 且是最占用内存的方式, 不采用
                //AssetBundle ab = IcResources.LoadAssetbundle(url);
                if (ab != null)
                {
                    bundle = new LoadedAssetBundle(ab);
                    m_LoadedAssetBundles.Add(assetBundleName, bundle);
                }
                else
                {
                    Debug.LogError("url is not find:" + url);
                }
            }
            #endregion
            return(true);
        }
コード例 #7
0
        public override void DoStartLoad()//public override void StartLoad()
        {
            if (m_loadedCallback == null)
            {
                return;
            }

            /*
             * if (m_name == "fmz2jx_atk0")
             * {
             *   int iu = 0;
             *   iu++;
             * }*/
            string strNewFile = string.Empty;

            //if (MyTxtResources.GetTmpActorsFile(m_name, out strNewFile))//m_bTestRole)
            //{
            //    string pathOk = IcResources.GetPersistentDataPath_FileStream() + "actors/" + strNewFile;//IcResources.GetPersistentDataPath_WWW() + assetbundlePath;
            //    m_path = pathOk;
            //    LoadAssetbundleDirectly(pathOk, strNewFile, typeof(Object));

            //    return;
            //}
            //IcChangeFiles pChangeFile = MyTxtResources.GetChangeFile(m_name);
            //if (pChangeFile != null)
            //{
            //    m_name = pChangeFile.fileName;
            //    m_dir = pChangeFile.path;
            //}
            stResourcePath resourcePath = AssetBundleManager.GetResources().GetResourcePath(m_dir, m_name, m_extension);

            if (!AssetBundleManager.Instance.m_isLocaAsset)
            {
                if (resourcePath.asset != null)
                {
                    //(assetbundle 已经异步load好了)
                    Loaded(resourcePath.asset, resourcePath.path);
                }
                else if (resourcePath.bAssetbundle)
                {
                    StartCoroutine(LoadAssetBundle(resourcePath.path, resourcePath.ver, !loadLowPriority));
                }
                else
                {
                    if (loadLowPriority)
                    {
                        StartCoroutine(LoadResourceAssetAsync(resourcePath.path));
                    }
                    else
                    {
                        AssetBundleManifest m_AssetBundleManifest = AssetBundleManager.GetAssetBundleManager().m_AssetBundleManifest;
                        if (m_AssetBundleManifest == null)
                        {
                            Object asset = null;
                            string path  = resourcePath.path;
                            asset = Resources.Load(path, typeof(Object));
                            if (asset == null)
                            {
                                Debug.LogError("ResourceLoad Error:" + path);
                            }
                            Loaded(asset, path);
                        }
                        else
                        {
                            StartCoroutine(LoadAssetBundle(resourcePath.AssetPath, resourcePath.ver, !loadLowPriority));
                        }
                    }
                }
            }
            else
            {
                Object obj = null;

                string path = IcResources.GetEditorAssetResourcePath(m_dir, m_name, m_extension);
#if UNITY_EDITOR
                obj = AssetDatabase.LoadAssetAtPath(path, typeof(Object));
#endif

                if (obj == null)
                {
                    //同步
                    path = IcResources.GetEditorResourcePath(m_dir, m_name, m_extension);
                    obj  = Resources.Load(path, typeof(Object));
                }

                Loaded(obj, path);
            }
        }