Asset Bundles problem

Hi, I tried this script from the docs (here: Unity - Scripting API: BuildPipeline.BuildAssetBundle) and it doesn’t work:

problem 1:

// C# Example
// Builds an asset bundle from the selected folder in the project view.
// Bare in mind that this script doesnt track dependencies nor is recursive
    
using UnityEngine;
using UnityEditor;
using System.IO;

public class BuildAssetBundlesFromDirectory {
    [@MenuItem("Asset/Build AssetBundles From Directory of Files")]
    static void ExportAssetBundles () {
        // Get the selected directory
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        Debug.Log("Selected Folder: " + path);
        if (path.Length != 0) {
            path = path.Replace("Assets/", "");
            string [] fileEntries = Directory.GetFiles(Application.dataPath+"/"+path);
            foreach(string fileName in fileEntries) {
                string filePath = fileName.Replace("
", "/");
                int index = filePath.LastIndexOf("/");
                filePath = filePath.Substring(index);
                Debug.Log(filePath);
                string localPath = "Assets/" + path;
                if (index > 0)
                    localPath += filePath;
                Object t = AssetDatabase.LoadMainAssetAtPath(localPath);
                if (t != null) {
                    Debug.Log(t.name);
                    string bundlePath = "Assets/" + path + "/" + t.name + ".unity3d";
                    Debug.Log("Building bundle at: " + bundlePath);
                    // Build the resource file from the active selection.
                    BuildPipeline.BuildAssetBundle
            (t, null, bundlePath, BuildAssetBundleOptions.CompleteAssets);
                }

            }
        }
    }
}

Error is “Assets/Editor/rBuildBundle.cs(20,0): error CS1010: Newline in constant”. When I remove the newline it throws another error. This script is just broken in the examples. Moving on there is another problem

problem 2:

I have unity convert all my .wav files to gapless looping mp3s, however in an asset bundle it saves them as .wav? what gives. How the hell am I supposed to get asset bundles working when it behaves like this?

I have 37 samples per folder, each is an mp3 which uses very specific unity settings like 112 compression, decompress on load, load type and gapless looping. Only with those settings does it sound right.

My big problem is I have 25 of these folders for over 900 samples, and 4 folders must be loaded at any one time for this music app, so I need to load and unload samples on the fly in iOS depending on what instrument is picked by the user. This is a huge problem and I’m beginning to think unity was the wrong choice for this job thanks to how asset bundles are working.

Any advice is very much appreciated.

The “newline in constant” error is due to this part:-

string filePath = fileName.Replace("
", "/");

…where the opening and closing quote marks are on separate lines. This was probably supposed to say

string filePath = fileName.Replace("\\", "/");

…which means it should replace backslashes with forward slashes. You need to use “\” because the single backslash introduces an escape sequence - this is most likely the cause of the error in the original example.

Thanks andeeee, hopefully docs will get a small edit to fix that :slight_smile: