예제 #1
0
    private static Dictionary <string, Object> assetCache       = new Dictionary <string, Object>();      //缓存所有asset( key= bunleName_assetName )

    //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
    public static void Load()
    {
        //LoadManifest
        AssetBundle manifestBundle = AssetBundle.LoadFromFile(AssetsPath.StreamingAssets + "StreamingAssets");

        manifest = manifestBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest");
        manifestBundle.Unload(false);

        //LoadBundlePathMap
        AssetBundle        bundlePathMapBundle = AssetBundle.LoadFromFile(AssetsPath.StreamingAssets + "base/bundlepathmap.ab");
        BundlePathMapAsset bundlePathMapAsset  = bundlePathMapBundle.LoadAsset <BundlePathMapAsset>("BundlePathMap");

        bundlePathMap = bundlePathMapAsset.OnAfterDeserialize();
        bundlePathMapBundle.Unload(false);
    }
예제 #2
0
    static void MappingBundlePath()
    {
        //标题
        string tips = "建立Bundle路径映射表";

        EditorUtility.DisplayProgressBar(tips, "正在" + tips + "...", 0f);

        //建表
        BundlePathMapAsset          pathMap = ScriptableObject.CreateInstance <BundlePathMapAsset>();
        Dictionary <string, string> dict    = new Dictionary <string, string>();

        //prefabs
        FileInfo[] prefabs = FileUtils.GetFiles(RelativePath.PrefabRepository, "*.prefab", SearchOption.AllDirectories);
        FileInfo[] fbxs    = FileUtils.GetFiles(RelativePath.PrefabRepository, "*.FBX", SearchOption.AllDirectories);

        //uiprefabs
        FileInfo[] uiPrefabs = FileUtils.GetFiles(RelativePath.UIPrefabRepository, "*.prefab", SearchOption.AllDirectories);
        FileInfo[] materials = FileUtils.GetFiles(RelativePath.Materials, "*.mat", SearchOption.AllDirectories);
        FileInfo[] icons     = FileUtils.GetFiles(RelativePath.Icons, "*.png", SearchOption.AllDirectories);
        FileInfo[] sound     = FileUtils.GetFiles(RelativePath.Sound, "*.mp3", SearchOption.AllDirectories);

        List <FileInfo> all = new List <FileInfo>();

        all.AddRange(prefabs);
        all.AddRange(fbxs);
        all.AddRange(uiPrefabs);
        all.AddRange(materials);
        all.AddRange(icons);
        all.AddRange(sound);

        for (int i = 0, l = all.Count; i < l; i++)
        {
            EditorUtility.DisplayProgressBar(tips, "正在" + tips + "...", (float)i / l);
            FileInfo      prefab   = all[i];
            AssetImporter importer = FileUtils.GetImporter(prefab);

            string bundleName = importer.assetBundleName;
            string name       = prefab.Name.Replace(prefab.Extension, "");

            if (dict.ContainsKey(name))
            {
                Debug.LogError("相同的Prefab名:" + name);
                continue;
            }
            dict.Add(name, bundleName);
        }
        pathMap.OnBeforeSerialize(dict);

        string o = "Assets/Repository/BundlePathMap.asset";

        AssetDatabase.CreateAsset(pathMap, o);

        AssetImporter mapImporter = AssetImporter.GetAtPath(o);

        mapImporter.assetBundleName = "base/bundlepathmap.ab";

        EditorUtility.ClearProgressBar();

        if (dict.Count == all.Count)
        {
            Debug.Log(tips + "完成!共映射 " + dict.Count + " 个对象。");
        }
        else
        {
            Debug.Log(tips + "失败!当前映射 " + dict.Count + " 个对象。总共需要映射:" + all.Count);
        }
    }