Example #1
0
    /// <summary>
    /// 手动从Bundle中加载资源唯一入口
    /// </summary>
    /// <param name="path"></param>
    /// <param name="name"></param>
    /// <param name="type"></param>
    /// <param name="onLoaded"></param>
    /// <param name="async"></param>
    private void LoadAssetFromBundle(string path, string assetName, System.Type type, LoadedCallback onLoaded, bool async, AssetType assetType = AssetType.GameObject)
    {
        if (AppConst.UseAssetBundle)
        {
            Util.Log(string.Format("LoadAssetFromBundle, bundle:{0}, asset:{1}", path, assetName));

            LoadAssetBundle(path, (data) => {
                LoadedBundle abCache = data as LoadedBundle;
                object asset         = null;
                if (abCache != null)
                {
                    string abName = FileHelper.CheckBundleName(path);
                    var cache     = LoadedBundleCtrl.Instance.UnReferenceLoadedBundle(abName);
                    if (cache != null && 0 == cache.ReferencedCount)
                    {
                        cache.ReferencedCount = 1;

                        if (!m_handLoad.Contains(abName))
                        {
                            m_handLoad.Add(abName);
                        }
                    }

                    if (!string.IsNullOrEmpty(assetName))
                    {
                        asset = abCache.LoadAsset(assetName, type);
                        if (type != null && type == typeof(GameObject))
                        {
                            abCache.UnloadSelf();
                        }
                    }
                    else
                    {
                        asset = abCache.Bundle;
                    }
                }

                if (asset == null)
                {
                    Util.LogError(string.Format("LoadAssetFromBundle, path:{0},name:{1}, asset is null", path, assetName));
                }

                if (onLoaded != null)
                {
                    onLoaded(asset);
                }
            }, async);
        }
        else
        {
            LoadAsset(path, onLoaded, type, false, assetType);
        }
    }
Example #2
0
    // 加载资源清单
    void LoadManifest(ManifestType mType)
    {
        if (m_manifests.ContainsKey(mType) && m_manifests[mType] != null)
        {
            DestroyImmediate(m_manifests[mType], true);
            m_manifests.Remove(mType);
        }

        string manifestName = mType.ToString();

        Util.Log("LoadManifest manifestName " + manifestName);

        m_manifestNames.Add(manifestName);
        string strFullPath = FileHelper.SearchFilePath(GetManifestType(manifestName).ToString(), manifestName);

        Util.Log("strFullPath: " + strFullPath);

        BundleLoader bLoader = LoaderPool.Instance.GetLoader <BundleLoader>();

        bLoader.Init(strFullPath, manifestName, delegate(object data) {
            LoadedBundle ab = data as LoadedBundle;
            if (ab != null)
            {
                m_manifests[mType]   = ab.LoadAsset <AssetBundleManifest>("AssetBundleManifest");
                m_bundleHeads[mType] = mType.ToString().ToLower().Split('_')[0];

                Util.Log("manifest ab in not null over----");
            }
            else
            {
                Util.LogError("manifest ab is null---------");
            }

            LoadedBundleCtrl.Instance.UnReferenceLoadedBundle(manifestName);  // 不走统一接口是因为manifest文件没有后缀
            LoadedBundleCtrl.Instance.HandRemoveBundle(manifestName);

            if (m_manifests[mType] != null)
            {
                string[] bundles = m_manifests[mType].GetAllAssetBundles();

                for (int i = 0; i < bundles.Length; ++i)
                {
                    if (m_bundleNames.Contains(bundles[i]))
                    {
                        Util.LogError(string.Format("别的清单已同名的包名,{0} {1}", manifestName, bundles[i]));
                    }
                    m_bundleNames.Add(bundles[i]);
                }
            }
        }, false, null, true);
    }