Esempio n. 1
0
    // 根据资源名获取AssetBundle的名字
    public string LookupAssetBundleName(string assetName)
    {
        string normalizeName = assetName.ToLower();
        string bundlePath;

        if (!file2AssetBundleMap.TryGetValue(normalizeName, out bundlePath))
        {
            bundlePath = packRules.FindAssetBundlePath(normalizeName);
            file2AssetBundleMap[normalizeName] = bundlePath;
        }

        return(bundlePath);
    }
Esempio n. 2
0
    static void GenerateAB(BuildTarget target)
    {
        DateTime startTime = DateTime.Now;
        DateTime stepTime  = DateTime.Now;

        string path = GetDirStringByBuildTarget(target);

        Debug.Log("Generate AssetBundle start");


        #region Step 1: Clear AssetBundle Directory
        if (!Directory.Exists(AB_TARGET_PATH))
        {
            Directory.CreateDirectory(AB_TARGET_PATH);
        }

        string assetBundlePath = AB_TARGET_PATH + "/" + path;
        if (!Directory.Exists(assetBundlePath))
        {
            Directory.CreateDirectory(assetBundlePath);
        }

        Paths.Clear();
        Files.Clear();
        #endregion

        #region Step 2: Collect All Resource Files
        Debug.Log("Collect all files");
        stepTime = DateTime.Now;
        List <AssetBundleBuild>             builds       = new List <AssetBundleBuild>();
        Dictionary <string, List <string> > buildDetails = new Dictionary <string, List <string> >();

        FindAllFiles(Paths, Files, Application.dataPath);
        for (int i = 0; i < Files.Count; ++i)
        {
            AssetBundleBuild build = new AssetBundleBuild();
            build.assetBundleName = PackRules.FindAssetBundlePath(Files[i].ToLower());
            build.assetNames      = new string[] { Files[i] };
            builds.Add(build);

            string abPath = assetBundlePath.ToLower() + "/" + build.assetBundleName;
            if (!buildDetails.ContainsKey(abPath))
            {
                List <string> filenames = new List <string>();

                filenames.Add(Files[i]);
                buildDetails.Add(abPath, filenames);
            }
        }
        #endregion

        #region Step 3: Process Lua Scripts
        stepTime = DateTime.Now;
        string bytecodePath = Application.dataPath + "/" + PackRules.LuaBytecodePath;

        // 重新建立 lua bytecode 目录
        if (Directory.Exists(bytecodePath))
        {
            Directory.Delete(bytecodePath, true);
        }
        Directory.CreateDirectory(bytecodePath);

        HandleLuaScripts(bytecodePath, builds, buildDetails, assetBundlePath);
        #endregion

        #region Step 4: Build AssetBundle
        stepTime = DateTime.Now;

        BuildAssetBundleOptions options = BuildAssetBundleOptions.DeterministicAssetBundle;
        options |= BuildAssetBundleOptions.ChunkBasedCompression;

        BuildPipeline.BuildAssetBundles(assetBundlePath, builds.ToArray(), options, target);
        #endregion

        #region Step 5: Generate BuildGraph File
        //TODO:
        #endregion

        #region Step 6: Generate Checksum File
        //TODO:
        #endregion

        CleanLuaBytecode(bytecodePath);

        string timeSpent = (DateTime.Now - startTime).ToString();
        Debug.Log("Generate AssetrBundle end. Time spent: " + timeSpent);

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }