Exemple #1
0
    private void WriteBundlesDatabase()
    {
        Console.WriteLine("Writing asset bundles database...");


        _bundlesDatabaseBuilder.BuildAssetBundle(Path.Combine(_bundlesPath, "index"), _params.Target);

        string infoPath = Path.Combine(_params.BundlesLocation, _bundlesDatabaseBuilder.Database.Id + ".txt");

        using (StreamWriter writer = File.CreateText(infoPath))
        {
            writer.WriteLine("[" + PlayerSettings.companyName + " - " + PlayerSettings.productName + "]");
            writer.WriteLine("Id: " + _bundlesDatabaseBuilder.Database.Id);
            writer.WriteLine("Version: " + CachingUtils.GetVersionFromId(_bundlesDatabaseBuilder.Database.Id));
            writer.WriteLine("Hash: " + BuildUtils.ToHexString(_bundlesDatabaseBuilder.Database.Hash));
            writer.WriteLine("Size: " + _bundlesDatabaseBuilder.Database.Size);
            writer.WriteLine("Created: " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss (K)"));
            writer.WriteLine("App Version: " + PlayerSettings.bundleVersion);
            writer.WriteLine("Platform: " + _params.Target.ToString());
            writer.WriteLine("Subtarget: " + (_params.Target == BuildTarget.Android ? _params.AndroidSubtarget.ToString() : "Generic"));

            writer.WriteLine();

            foreach (var bundleInfo in _bundles)
            {
                writer.WriteLine("[" + bundleInfo.Name + "]");
                writer.WriteLine("Filename: " + bundleInfo.Data.Filename);
                writer.WriteLine("Size: " + bundleInfo.Data.Size);
                writer.WriteLine("Hash: " + BuildUtils.ToHexString(bundleInfo.Data.Hash));
                writer.WriteLine("Cache Name: " + bundleInfo.Data.CacheName);
                writer.WriteLine("Version: " + CachingUtils.GetVersionFromHash(bundleInfo.Data.Hash));

                if (bundleInfo.IsScene)
                {
                    writer.WriteLine("Scenes:");
                    foreach (var scene in bundleInfo.AssetsPaths)
                    {
                        writer.WriteLine("\t" + scene + "\t(" + BuildUtils.ToHexString(GetHashForBuildAsset(scene)) + ")");
                    }
                }

                writer.WriteLine("Bundle Assets:");
                foreach (var assetPath in bundleInfo.AssetsPaths)
                {
                    writer.WriteLine("\t" + assetPath + "\t" + GetAssetId(assetPath) + "\t(" + BuildUtils.ToHexString(GetHashForBuildAsset(assetPath)) + ")");
                }


                writer.WriteLine("Dependencies Assets:");
                var assetPaths = BuildUtils.GetAssetPathsForObjects(bundleInfo.Dependencies);
                foreach (var assetPath in assetPaths)
                {
                    writer.WriteLine("\t" + assetPath + "\t(" + BuildUtils.ToHexString(GetHashForBuildAsset(assetPath)) + ")");
                }
                writer.WriteLine();
            }
        }
    }
Exemple #2
0
    private void PackBundles()
    {
        var referencedObjects = new HashSet <UnityEngine.Object>(_lateProcessor.ReferencedObjects);
        var packedObjects     = new HashSet <UnityEngine.Object>();

        var tempPath = Path.Combine(Path.GetTempPath(), "bundles_" + DateTime.UtcNow.Ticks.ToString());

        BuildUtils.PrepareCleanDirectory(tempPath);

        PushAssetDependencies();
        {
            foreach (var bundleInfo in ScanningUtils.ItemsProcessor(_bundles, "Packing asset bundles", p => Path.GetFileName(p.Name), true))
            {
                Console.WriteLine("Building bundle " + bundleInfo.Name + "...");

                string bundlePath = Path.Combine(tempPath, bundleInfo.Name);                //BuildUtils.GetPathHashString(bundleInfo.Name));

                if (bundleInfo.IsScene)
                {
                    var scenePaths = bundleInfo.AssetsPaths.ToArray();

                    PushAssetDependencies();
                    {
                        string buildResult = BuildPipeline.BuildStreamedSceneAssetBundle(scenePaths,
                                                                                         bundlePath,
                                                                                         _params.Target);
                        if (!String.IsNullOrEmpty(buildResult))
                        {
                            throw new UnityException(buildResult);
                        }
                    }
                    PopAssetDependencies();

                    // Find which assets were packed
                    var packedDependencies = new HashSet <UnityEngine.Object>();
                    foreach (var scenePath in scenePaths)
                    {
                        Debug.Log(scenePath);
                        EditorApplication.OpenScene(scenePath);
                        packedDependencies.UnionWith(ScanningUtils.ScanCurrentSceneAssets());
                        packedDependencies.ExceptWith(packedObjects);
                    }

                    bundleInfo.Dependencies = packedDependencies.ToList();

                    var hash = GetHashForBuildAssets(scenePaths.Concat(BuildUtils.GetAssetPathsForObjects(packedDependencies)));


                    int bundleLevel = _bundleLevelDataBase.GetBundleLevel(bundleInfo.Name);
                    bundleInfo.Data = _bundlesDatabaseBuilder.AddSceneBundle(bundleInfo.Name, hash, bundleLevel,
                                                                             scenePaths.Select(p => Path.GetFileNameWithoutExtension(p)).ToArray(),
                                                                             bundlePath);
                }
                else
                {
                    // Add only the main asset + any late referenced asset

                    var mainObjects     = bundleInfo.AssetsPaths.Select(p => AssetDatabase.LoadMainAssetAtPath(p));
                    var representations = bundleInfo.AssetsPaths.SelectMany(p => AssetDatabase.LoadAllAssetRepresentationsAtPath(p));
                    var objects         = mainObjects.Concat(representations.Where(o => referencedObjects.Contains(o))).ToArray();

                    var assetIds = objects.Select(o => AssetId.FromObject(o)).ToArray();


                    foreach (var obj in objects)
                    {
                        string assetPath = AssetDatabase.GetAssetPath(obj);
                        string guid      = AssetDatabase.AssetPathToGUID(assetPath);
                        Console.WriteLine("path: " + assetPath + "   guid:" + guid);
                    }


                    if (bundleInfo.Isolate)
                    {
                        PushAssetDependencies();
                    }
                    {
                        if (!BuildPipeline.BuildAssetBundleExplicitAssetNames(objects, assetIds, bundlePath,
                                                                              BuildAssetBundleOptions.CompleteAssets |
                                                                              BuildAssetBundleOptions.CollectDependencies |
                                                                              BuildAssetBundleOptions.DeterministicAssetBundle,
                                                                              _params.Target))
                        {
                            throw new UnityException("Error building bundle " + bundleInfo.Name);
                        }
                    }
                    if (bundleInfo.Isolate)
                    {
                        PopAssetDependencies();
                    }

                    // Find which assets were packed
                    var packedDependencies = new HashSet <UnityEngine.Object>(EditorUtility.CollectDependencies(objects.ToArray()));
                    packedDependencies.ExceptWith(packedObjects);
                    bundleInfo.Dependencies = packedDependencies.ToList();
                    if (!bundleInfo.Isolate)
                    {
                        packedObjects.UnionWith(packedDependencies);
                    }

                    var hash        = GetHashForBuildAssets(BuildUtils.GetAssetPathsForObjects(packedDependencies));
                    int bundleLevel = _bundleLevelDataBase.GetBundleLevel(bundleInfo.Name);
                    bundleInfo.Data = _bundlesDatabaseBuilder.AddBundle(bundleInfo.Name, hash, bundleLevel, bundlePath, assetIds.ToList());

                    foreach (var obj in objects)
                    {
                        referencedObjects.Remove(obj);
                    }
                }
            }
        }
        PopAssetDependencies();

        // Move to right destination
        Directory.CreateDirectory(_params.BundlesLocation);
        _bundlesPath = Path.Combine(_params.BundlesLocation, _bundlesDatabaseBuilder.Database.Id);
        FileUtil.DeleteFileOrDirectory(_bundlesPath);
        FileUtil.MoveFileOrDirectory(tempPath, _bundlesPath);

        // To create late reference for any late object not added to bundles
        _objectsToLateReference = referencedObjects.ToList();
    }