void CreateSceneBundleCommand(string bundleName, string internalName, GUID scene, List <GUID> bundledScenes, Dictionary <GUID, string> assetToMainFile)
        {
            var fileObjects = m_WriteData.FileToObjects[internalName];

#if !UNITY_2019_1_OR_NEWER
            // ContentBuildInterface.PrepareScene was not returning stable sorted references, causing a indeterminism and loading errors in some cases
            // Add correct sorting here until patch lands to fix the API.
            fileObjects = GetSortedSceneObjectIdentifiers(fileObjects);
#endif


            var command        = CreateWriteCommand(internalName, fileObjects, new LinearPackedIdentifiers(3)); // Start at 3: PreloadData = 1, AssetBundle = 2
            var usageSet       = new BuildUsageTagSet();
            var referenceMap   = new BuildReferenceMap();
            var preloadObjects = new List <ObjectIdentifier>();
            var bundleScenes   = new List <SceneLoadInfo>();
            var dependencyInfo = m_DependencyData.SceneInfo[scene];
            var fileObjectSet  = new HashSet <ObjectIdentifier>(fileObjects);


            usageSet.UnionWith(m_DependencyData.SceneUsage[scene]);
            referenceMap.AddMappings(command.internalName, command.serializeObjects.ToArray());
            foreach (var referencedObject in dependencyInfo.referencedObjects)
            {
                if (fileObjectSet.Contains(referencedObject))
                {
                    continue;
                }
                preloadObjects.Add(referencedObject);
            }
            foreach (var bundledScene in bundledScenes)
            {
                var loadInfo = new SceneLoadInfo();
                loadInfo.asset        = bundledScene;
                loadInfo.internalName = Path.GetFileNameWithoutExtension(assetToMainFile[bundledScene]);
                loadInfo.address      = m_BuildContent.Addresses[bundledScene];
                bundleScenes.Add(loadInfo);
            }


            var operation = new SceneBundleWriteOperation();
            operation.Command        = command;
            operation.UsageSet       = usageSet;
            operation.ReferenceMap   = referenceMap;
            operation.DependencyHash = m_DependencyData.DependencyHash.TryGetValue(scene, out var hash) ? hash : new Hash128();
            operation.Scene          = dependencyInfo.scene;
#if !UNITY_2019_3_OR_NEWER
            operation.ProcessedScene = dependencyInfo.processedScene;
#endif
            operation.PreloadInfo = new PreloadInfo();
            operation.PreloadInfo.preloadObjects = preloadObjects;
            operation.Info              = new SceneBundleInfo();
            operation.Info.bundleName   = bundleName;
            operation.Info.bundleScenes = bundleScenes;


            m_WriteData.WriteOperations.Add(operation);
            m_WriteData.FileToUsageSet.Add(command.internalName, usageSet);
            m_WriteData.FileToReferenceMap.Add(command.internalName, referenceMap);
        }
        public void OneTimeSetUp()
        {
            WriteOperations    = new IWriteOperation[5];
            WriteOperations[0] = new AssetBundleWriteOperation();
#pragma warning disable CS0618 // Type or member is obsolete
            WriteOperations[1] = new RawWriteOperation();
#pragma warning restore CS0618 // Type or member is obsolete
            WriteOperations[2] = new SceneBundleWriteOperation();
            WriteOperations[3] = new SceneDataWriteOperation();
#pragma warning disable CS0618 // Type or member is obsolete
            WriteOperations[4] = new SceneRawWriteOperation();
#pragma warning restore CS0618 // Type or member is obsolete

            var command = new WriteCommand
            {
                fileName     = GUID.Generate().ToString(),
                internalName = GUID.Generate().ToString()
            };
            var usageSet     = new BuildUsageTagSet();
            var referenceMap = new BuildReferenceMap();

            for (int i = 0; i < WriteOperations.Length; i++)
            {
                WriteOperations[i].Command      = command;
                WriteOperations[i].UsageSet     = usageSet;
                WriteOperations[i].ReferenceMap = referenceMap;
            }
        }
Exemple #3
0
        void CreateSceneBundleCommand(string bundleName, string internalName, GUID asset, List <GUID> assets)
        {
            var sbOp = new SceneBundleWriteOperation();

            var fileObjects = m_WriteData.FileToObjects[internalName];

#if !UNITY_2019_1_OR_NEWER
            // ContentBuildInterface.PrepareScene was not returning stable sorted references, causing a indeterminism and loading errors in some cases
            // Add correct sorting here until patch lands to fix the API.
            fileObjects = GetSortedSceneObjectIdentifiers(fileObjects);
#endif
            sbOp.Command = CreateWriteCommand(internalName, fileObjects, new LinearPackedIdentifiers(3)); // Start at 3: PreloadData = 1, AssetBundle = 2

            sbOp.UsageSet = new BuildUsageTagSet();
            m_WriteData.FileToUsageSet.Add(internalName, sbOp.UsageSet);

            sbOp.ReferenceMap = new BuildReferenceMap();
            m_WriteData.FileToReferenceMap.Add(internalName, sbOp.ReferenceMap);

            var sceneInfo = m_DependencyData.SceneInfo[asset];
            sbOp.Scene = sceneInfo.scene;
#if !UNITY_2019_3_OR_NEWER
            sbOp.ProcessedScene = sceneInfo.processedScene;
#endif

            {
                var objectSet = new HashSet <ObjectIdentifier>(m_WriteData.FileToObjects[internalName]);
                sbOp.PreloadInfo = new PreloadInfo {
                    preloadObjects = sceneInfo.referencedObjects.Where(x => !objectSet.Contains(x)).ToList()
                };
            }

            {
                sbOp.Info              = new SceneBundleInfo();
                sbOp.Info.bundleName   = bundleName;
                sbOp.Info.bundleScenes = assets.Select(x => new SceneLoadInfo
                {
                    asset        = x,
                    internalName = Path.GetFileNameWithoutExtension(m_WriteData.AssetToFiles[x][0]),
                    address      = m_BuildContent.Addresses[x]
                }).ToList();
            }

            m_WriteData.WriteOperations.Add(sbOp);
        }
Exemple #4
0
        private List <IWriteOperation> CreateSceneBundleWriteOperations(string bundleName, List <GUID> scenes, BuildDependencyInfo buildInfo)
        {
            // The 'Folder' we mount asset bundles to is the same as the internal file name of the first file in the archive
            var bundleFileName = GenerateInternalFileName(AssetDatabase.GUIDToAssetPath(scenes[0].ToString()));

            var ops           = new List <SceneDataWriteOperation>();
            var sceneLoadInfo = new List <SceneLoadInfo>();
            var dependencies  = new HashSet <string>();

            foreach (var scene in scenes)
            {
                var op = CreateSceneDataWriteOperation(bundleName, bundleFileName, scene, buildInfo);
                ops.Add((SceneDataWriteOperation)op);

                var scenePath = AssetDatabase.GUIDToAssetPath(scene.ToString());
                sceneLoadInfo.Add(new SceneLoadInfo
                {
                    asset        = scene,
                    address      = buildInfo.sceneAddress[scene],
                    internalName = GenerateInternalFileName(scenePath)
                });

                dependencies.UnionWith(buildInfo.assetToBundles[scene]);
            }
            dependencies.Remove(bundleName); // Don't include self as dependency

            // First write op must be SceneBundleWriteOperation
            var bundleOp = new SceneBundleWriteOperation(ops[0]);

            ops[0] = bundleOp;
            foreach (var serializeObj in bundleOp.command.serializeObjects)
            {
                serializeObj.serializationIndex++; // Shift by 1 to account for asset bundle object
            }
            bundleOp.info.bundleName         = bundleName;
            bundleOp.info.bundleScenes       = sceneLoadInfo;
            bundleOp.info.bundleDependencies = dependencies.OrderBy(x => x).ToList();

            return(ops.Cast <IWriteOperation>().ToList());
        }
        public IEnumerator SceneBundleWriteOperation_HashChanges_WhenPrefabDepenencyChanges()
        {
            Scene s = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);

            yield return(null);

            var prefab = AssetDatabase.LoadAssetAtPath <GameObject>(k_CubePath);

            prefab.transform.position = new Vector3(0, 0, 0);
            EditorUtility.SetDirty(prefab);
            AssetDatabase.SaveAssets();
            PrefabUtility.InstantiatePrefab(prefab);

            EditorSceneManager.SaveScene(s, k_ScenePath);

            var op = new SceneBundleWriteOperation
            {
                Command     = new WriteCommand(),
                PreloadInfo = new PreloadInfo(),
#if !UNITY_2019_3_OR_NEWER
                ProcessedScene = k_ScenePath,
#endif
                ReferenceMap   = new BuildReferenceMap(),
                UsageSet       = new BuildUsageTagSet(),
                Info           = new SceneBundleInfo(),
                Scene          = k_ScenePath,
                DependencyHash = AssetDatabase.GetAssetDependencyHash(k_CubePath)
            };
            var cacheVersion1 = op.GetHash128();

            prefab.transform.position = new Vector3(1, 1, 1);
            EditorUtility.SetDirty(prefab);
            AssetDatabase.SaveAssets();
            op.DependencyHash = AssetDatabase.GetAssetDependencyHash(k_CubePath);
            var cacheVersion2 = op.GetHash128();

            Assert.AreNotEqual(cacheVersion1, cacheVersion2);
        }