public LateReferenceProcessor(IList <string> assetPaths) { var objSet = new HashSet <UnityEngine.Object>(); AssetId.CurrentGenerator = obj => { string id = AssetId.DefaultGenerator(obj); if (!string.IsNullOrEmpty(id)) { objSet.Add(obj); } return(id); }; foreach (string assetPath in ScanningUtils.ItemsProcessor(assetPaths, "Scanning assets for late references", p => Path.GetFileName(p))) { var asset = AssetDatabase.LoadMainAssetAtPath(assetPath); ProcessObject(asset); } AssetId.CurrentGenerator = AssetId.DefaultGenerator; _referencedObjects = objSet.ToList(); }
public static string[] CollectBuildAssets() { string oldScene = EditorApplication.currentScene; try { var buildAssets = new HashSet <string>(); var scenes = CollectBuildScenes(); foreach (var scene in ScanningUtils.ItemsProcessor(scenes, "Collecting build assets", p => Path.GetFileName(p))) { EditorApplication.OpenScene(scene); buildAssets.Add(scene); UnityEngine.Debug.Log("current scene: " + scene); buildAssets.UnionWith(ScanningUtils.ScanCurrentSceneAssets().Select(o => AssetDatabase.GetAssetPath(o))); } buildAssets.Remove(""); return(buildAssets.OrderBy(p => p).ToArray()); } finally { EditorApplication.OpenScene(oldScene); } }
// Note: Hashes are calculated for source files since unity seems to // serialize material maps in a way that yields a random order private void ComputeBuildAssetsHashes() { _buildAssetsHashes = new byte[_buildAssets.Length][]; int i = 0; foreach (var assetPath in ScanningUtils.ItemsProcessor(_buildAssets, "Computing assets hashes", p => Path.GetFileName(p))) { _buildAssetsHashes[i] = BuilderCache.Instance.GetHashForAsset(assetPath); i++; } }
private void MapBundles() { var bundlesMap = new Dictionary <string, AssetBundleBuildInfo>(); foreach (string assetPath in ScanningUtils.ItemsProcessor(_buildAssets, "Mapping asset to bundles", p => Path.GetFileName(p))) { string matchAssetPath = assetPath.ToLower().Substring("assets/".Length); var rule = _bundlesConfig.MatchRules(matchAssetPath); if (rule != null && rule.Type == AssetBundlesConfig.RuleType.Include && !string.IsNullOrEmpty(rule.BundleName)) { bool isSceneAsset = Path.GetExtension(assetPath) == ".unity"; string bundleName = rule.GetReplacedBundleName(matchAssetPath); AssetBundleBuildInfo bundleInfo; if (!bundlesMap.TryGetValue(bundleName, out bundleInfo)) { bundleInfo = new AssetBundleBuildInfo(bundleName, rule, isSceneAsset); bundlesMap.Add(bundleName, bundleInfo); } if (isSceneAsset) { if (!bundleInfo.IsScene) { throw new UnityException("Scenes can only be added to scene bundles"); } } else { var importer = AssetImporter.GetAtPath(assetPath); if (importer != null) { AudioImporter audioImporter = importer as AudioImporter; if (audioImporter != null) { //if (audioImporter.loadType == AudioImporterLoadType.StreamFromDisc) // throw new UnityException("Can't add streamed audio clips to asset bundles: " + assetPath); } } } bundleInfo.AssetsPaths.Add(assetPath); } } _bundles = bundlesMap.Values.OrderBy(b => b.Order).ThenBy(b => b.Name).ToList(); }
private void CreateLateResources(IList <UnityEngine.Object> objects) { const string lateResourcesDirectory = "Assets/Resources/Late"; BuildUtils.PrepareCleanDirectory(lateResourcesDirectory); foreach (var obj in ScanningUtils.ItemsProcessor(objects, "Creating late resources", p => AssetDatabase.GetAssetPath(p))) { var lateResource = ScriptableObject.CreateInstance <LateResource>(); lateResource.Target = obj; AssetDatabase.CreateAsset(lateResource, lateResourcesDirectory + "/" + AssetId.FromObject(obj) + ".asset"); } }
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(); }