Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by markashburner, Jul 20, 2019.

  1. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class BundleBuilder : Editor
    7. {
    8.   [MenuItem("Assets/ Build AssetBundles")]
    9.   static void BuildAllAssetBundles()
    10.     {
    11.         BuildPipeline.BuildAssetBundles(@"C:\Projects\AssetBundles", BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
    12.     }
    13. }
    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:


    Code (CSharp):
    1.  foreach (Node node in nodeGraph.nodes)
    2.             {
    3.                 SavingNode save = node as SavingNode;
    4.  
    5.                 if (save != null)
    6.                 {
    7.                     save.filename = planetName;
    8.                     save.SerializeComputeShader();
    9. #if UNITY_EDITOR
    10.              //       UnityEditor.AssetDatabase.Refresh();
    11. #endif
    12.                  
    13.                  
    14.                     Debug.Log("GenerateTextures");
    15.  
    16.                     computeShader = (ComputeShader)Resources.Load(planetName);
    17.  
    18.  
    19.                 }
    20.  
    21.             }
    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.

    Code (CSharp):
    1. public void SerializeComputeShader()
    2.     {
    3.         File.WriteAllText(Application.dataPath + "/Planetary Terrain/Noise Modules/ComputeShaders/Resources/" + filename + ".compute", ComputeShaderGenerator.GenerateComputeShader(GetInputValue<ModuleWrapper>("input", null).m));
    4.     }
    So is it possible to generate an Asset bundle dynamically through script from generated objects that have been produced through scripting?

    Thanks
     
  2. BBO_Lagoon

    BBO_Lagoon

    Joined:
    Mar 2, 2017
    Posts:
    199
    Hi, You can edit the .meta file and set assetBundleName / assetBundleVariant values.

    Here an exemple :
    Code (CSharp):
    1. public static void SetAssetBundle( string filepath, string assetBundleName = "", string assetBundleVariant = "" )
    2. {
    3.     var metaFilePath = filepath + ".meta";
    4.     var lines        = File.ReadAllLines( metaFilePath );
    5.     var name         = "assetBundleName: " + assetBundleName;
    6.     var variant      = "assetBundleVariant: " + assetBundleVariant;
    7.     for( var i = 0; i < lines.Length; i++ )
    8.     {
    9.         lines[i] = Regex.Replace( lines[i], @"assetBundleName: \S*", name );
    10.         lines[i] = Regex.Replace( lines[i], @"assetBundleVariant: \S*", variant );
    11.     }
    12.     File.WriteAllLines(metaFilePath , lines);
    13. }