Пример #1
0
    IEnumerator _Init(string path, string assetName)
    {
        IsLoadAssetBundle = CCosmosEngine.GetConfig("IsLoadAssetBundle").ToInt32() != 0;
        AssetInBundleName = assetName;

        UnityEngine.Object getAsset = null;
        if (!IsLoadAssetBundle)
        {
            string extension = System.IO.Path.GetExtension(path);
            path = path.Substring(0, path.Length - extension.Length);  // remove extensions

            getAsset = Resources.Load <UnityEngine.Object>(path);
            if (getAsset == null)
            {
                CDebug.LogError("Asset is NULL(from Resources Folder): {0}", path);
            }
            OnFinish(getAsset);
        }
        else
        {
            var bundleLoader = CAssetBundleLoader.Load(path);

            while (!bundleLoader.IsFinished)
            {
                if (IsReadyDisposed)   // 中途释放
                {
                    bundleLoader.Release();
                    OnFinish(null);
                    yield break;
                }
                Progress = bundleLoader.Progress;
                yield return(null);
            }
            var assetBundle = bundleLoader.Bundle;

            System.DateTime beginTime = System.DateTime.Now;
            if (AssetInBundleName == null)
            {
                // 经过AddWatch调试,.mainAsset这个getter第一次执行时特别久,要做序列化
                //AssetBundleRequest request = assetBundle.LoadAsync("", typeof(Object));// mainAsset
                //while (!request.isDone)
                //{
                //    yield return null;
                //}
                try
                {
                    CDebug.Assert(getAsset = assetBundle.mainAsset);
                }
                catch
                {
                    CDebug.LogError("[OnAssetBundleLoaded:mainAsset]{0}", path);
                }
            }
            else
            {
                // TODO: 未测试过这几行!~~
                AssetBundleRequest request = assetBundle.LoadAsync(AssetInBundleName, typeof(Object));
                while (!request.isDone)
                {
                    yield return(null);
                }

                getAsset = request.asset;
            }

            CResourceModule.LogLoadTime("AssetFileBridge", path, beginTime);

            if (getAsset == null)
            {
                CDebug.LogError("Asset is NULL: {0}", path);
            }

            bundleLoader.Release();  // 释放Bundle(WebStream)
        }

        if (Application.isEditor)
        {
            if (getAsset != null)
            {
                CResourceLoadObjectDebugger.Create(getAsset.GetType().Name, Url, getAsset as UnityEngine.Object);
            }
        }

        if (getAsset != null)
        {
            // 更名~ 注明来源asset bundle 带有类型
            getAsset.name = string.Format("{0}~{1}", getAsset, Url);
        }
        OnFinish(getAsset);
    }
Пример #2
0
    IEnumerator _Init(string path, string assetName)
    {
        IsLoadAssetBundle = CCosmosEngine.GetConfig("IsLoadAssetBundle").ToInt32() != 0;
        AssetInBundleName = assetName;

        UnityEngine.Object getAsset = null;
        if (!IsLoadAssetBundle)
        {
            string extension = System.IO.Path.GetExtension(path);
            path = path.Substring(0, path.Length - extension.Length);  // remove extensions

            getAsset = Resources.Load<UnityEngine.Object>(path);
            if (getAsset == null)
            {
                CDebug.LogError("Asset is NULL(from Resources Folder): {0}", path);
            }
            OnFinish(getAsset);
        }
        else
        {
            _bundleLoader = CAssetBundleLoader.Load(path);

            while (!_bundleLoader.IsFinished)
            {
                if (IsReadyDisposed)   // 中途释放
                {
                    _bundleLoader.Release();
                    OnFinish(null);
                    yield break;
                }
                yield return null;
            }

            if (!_bundleLoader.IsOk)
            {
                CDebug.LogError("[CAssetFileLoader]Load BundleLoader Failed(Error) when Finished: {0}", path);
                _bundleLoader.Release();
                OnFinish(null);
                yield break;
            }

            var assetBundle = _bundleLoader.Bundle;

            System.DateTime beginTime = System.DateTime.Now;
            if (AssetInBundleName == null)
            {
                // 经过AddWatch调试,.mainAsset这个getter第一次执行时特别久,要做序列化
                //AssetBundleRequest request = assetBundle.LoadAsync("", typeof(Object));// mainAsset
                //while (!request.isDone)
                //{
                //    yield return null;
                //}
                try
                {
                    CDebug.Assert(getAsset = assetBundle.mainAsset);
                }
                catch
                {
                    CDebug.LogError("[OnAssetBundleLoaded:mainAsset]{0}", path);
                }
            }
            else
            {
                // TODO: 未测试过这几行!~~
                AssetBundleRequest request = assetBundle.LoadAsync(AssetInBundleName, typeof(Object));
                while (!request.isDone)
                {
                    yield return null;
                }

                getAsset = request.asset;
            }

            CResourceModule.LogLoadTime("AssetFileBridge", path, beginTime);

            if (getAsset == null)
            {
                CDebug.LogError("Asset is NULL: {0}", path);
            }

            _bundleLoader.Release();  // 释放Bundle(WebStream)
        }

        if (Application.isEditor)
        {
            if (getAsset != null)
                CResourceLoadObjectDebugger.Create(getAsset.GetType().Name, Url, getAsset as UnityEngine.Object);
        }

        if (getAsset != null)
        {
            // 更名~ 注明来源asset bundle 带有类型
            getAsset.name = string.Format("{0}~{1}", getAsset, Url);
        }
        OnFinish(getAsset);
    }