Is it possible to create an Asset Bundle dynamically from script?

Hi

I am generating ComputeShaders dynamically through script and when I generate a compute shader while playing the actual application I have to use.

#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif

Now this obviously doesn’t work for builds, so I have been looking into Asset Bundles…Now I know you can build an Asset bundle through this simple script here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class BundleBuilder : Editor
{
  [MenuItem("Assets/ Build AssetBundles")]
  static void BuildAllAssetBundles()
    {
        BuildPipeline.BuildAssetBundles(@"C:\Projects\AssetBundles", BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
    }
}

But as I am generating a compute shader on the fly…is it possible to automatically tag the ComputeShaders that has been generated to fall under an Asset Bundle label in the inspector? I know how to do it manually through the editor but how do I set the Asset Bundle label from script when the object has been generated?

This is how I am generating the object through script:

 foreach (Node node in nodeGraph.nodes)
            {
                SavingNode save = node as SavingNode;

                if (save != null)
                {
                    save.filename = planetName;
                    save.SerializeComputeShader();
#if UNITY_EDITOR
             //       UnityEditor.AssetDatabase.Refresh();
#endif
                 
                  
                    Debug.Log("GenerateTextures");

                    computeShader = (ComputeShader)Resources.Load(planetName);


                }

            }

To generate the object I use the File.WriteAllText and Application.dataPath but this only loads the object from the Unity Editor and NOT in the game builds.

public void SerializeComputeShader()
    {
        File.WriteAllText(Application.dataPath + "/Planetary Terrain/Noise Modules/ComputeShaders/Resources/" + filename + ".compute", ComputeShaderGenerator.GenerateComputeShader(GetInputValue<ModuleWrapper>("input", null).m));
    }

So is it possible to generate an Asset bundle dynamically through script from generated objects that have been produced through scripting?

Thanks

Hi, You can edit the .meta file and set assetBundleName / assetBundleVariant values.

Here an exemple :

public static void SetAssetBundle( string filepath, string assetBundleName = "", string assetBundleVariant = "" )
{
    var metaFilePath = filepath + ".meta";
    var lines        = File.ReadAllLines( metaFilePath );
    var name         = "assetBundleName: " + assetBundleName;
    var variant      = "assetBundleVariant: " + assetBundleVariant;
    for( var i = 0; i < lines.Length; i++ )
    {
        lines[i] = Regex.Replace( lines[i], @"assetBundleName: \S*", name );
        lines[i] = Regex.Replace( lines[i], @"assetBundleVariant: \S*", variant );
    }
    File.WriteAllLines(metaFilePath , lines);
}