コード例 #1
0
        static void BuildAssetBundleTable(AssetBundleEncryptor encryptor, string tableDirPath, List <string> tableFilePaths, BuildTarget buildTarget)
        {
            AssetBundleBuild[] buildArray = tableFilePaths
                                            .Select(tableFilePath =>
            {
                string assetBundleName = GetTableAssetBundleName(tableDirPath, tableFilePath);
                var tableBuild         = new AssetBundleBuild()
                {
                    assetBundleName = assetBundleName,
                    assetNames      = new string[] { tableFilePath },
                };
                return(tableBuild);
            })
                                            .ToArray();

            BuildPipeline.BuildAssetBundles(
                encryptor.PlaneAssetBundleDirPath,
                buildArray,
                BuildAssetBundleOptions.ChunkBasedCompression,
                buildTarget);

            foreach (string tableFilePath in tableFilePaths)
            {
                string assetBundleName = GetTableAssetBundleName(tableDirPath, tableFilePath);
                encryptor.Encrypt(assetBundleName, true);
            }
        }
コード例 #2
0
        static void CreateTableByAssetEntryRecords(
            AssetBundleEncryptor encryptor,
            AssetBundleManifest manifest,
            string tableFilePath,
            BuildTarget buildTarget,
            IEnumerable <AssetEntryRecord> assetEntryRecords)
        {
            string planeAssetBundleDirPath = encryptor.PlaneAssetBundleDirPath;

            HashSet <string> checkedAssetBundleNames = new HashSet <string>(
                assetEntryRecords
                .Select(assetEntryRecord => assetEntryRecord.AssetBundleName)
                .Distinct());
            Queue <string> checkingAssetBundleNameQueue = new Queue <string>(checkedAssetBundleNames);

            while (!checkingAssetBundleNameQueue.IsEmpty())
            {
                string assetBundleName = checkingAssetBundleNameQueue.Dequeue();

                foreach (string dependency in manifest.GetDirectDependencies(assetBundleName))
                {
                    if (checkedAssetBundleNames.Contains(dependency))
                    {
                        continue;
                    }

                    checkedAssetBundleNames.Add(dependency);
                    checkingAssetBundleNameQueue.Enqueue(dependency);
                }
            }

            IEnumerable <AssetBundleRecord> assetBundleRecords = checkedAssetBundleNames
                                                                 .Select(assetBundleName =>
            {
                string crcFilePath = GetCrcFilePath(planeAssetBundleDirPath, assetBundleName);
                uint crc           = 0;
                var isSucceeded    = BuildPipeline.GetCRCForAssetBundle(crcFilePath, out crc);
                if (!isSucceeded)
                {
                    Debug.LogError($"cannot get crc: AssetBundelName = {assetBundleName}");
                }

                string encryptedAssetBundleFilePath = encryptor.Encrypt(assetBundleName, true);
                //string encryptedAssetBundleFilePath = encryptor.Encrypt(assetBundleName, false);
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(encryptedAssetBundleFilePath);
                long fileSizeBytes          = fileInfo.Length;

                return(new AssetBundleRecord(
                           assetBundleName,
                           manifest.GetDirectDependencies(assetBundleName),
                           crc,
                           fileSizeBytes));
            });

            DateTime current  = DateTime.Now;
            long     version  = long.Parse(current.ToString("yyyyMMddHHmmss"));
            string   platform = buildTarget.ToString();

            var table = AssetBundleTable.Create(version, platform, assetBundleRecords, assetEntryRecords);

            string tableDirName = Path.GetDirectoryName(tableFilePath);

            if (!Directory.Exists(tableDirName))
            {
                Directory.CreateDirectory(tableDirName);
            }

            //var json = JsonUtility.ToJson(table);
            //using (StreamWriter writer = new StreamWriter(outputPath + ".json"))
            //{
            //    writer.Write(json);
            //}

            AssetDatabase.CreateAsset(table, tableFilePath);
        }