Search Unity

Question Creating Asset Bundle in Play Mode

Discussion in 'Scripting' started by alessandrobraga, Nov 17, 2022.

  1. alessandrobraga

    alessandrobraga

    Joined:
    Aug 28, 2022
    Posts:
    11
    Hi everyone, I'm kinda new with Unity and C# coding, and I'm trying to create and load asset bundle at runtime: ideally what I'm trying to do is to press a button in playmode and start the Asset Bundles building. The problem is that the Console reminds me that UnityEditor scripts can run only in Edit Mode. There's a way to do these kind of scripts in play mode instead.
    Sorry for my bad english.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,938
    For what reason do you want to build an asset bundle during play mode?

    During runtime the games asset files are immutable.
     
  3. alessandrobraga

    alessandrobraga

    Joined:
    Aug 28, 2022
    Posts:
    11
    What I'm trying to do is convert in asset bundles some FBX files in order to load them at run time in my app. The point is that those FBX come from a database. So this app has to be able to import at runtime these fbx files from that databse. The problem is FBX files, from what i know, can't be imported in Unity at runtime. I don't know if it's clearer.
    Moreover, the assetbundle has to be loaded from onedrive, so they would be saved in a onedrive folder and not in the assets.
     
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Then look for a runtime importer, since indeed Unity can't import in play mode by default. So creating an asset bundle in play mode is not going to help you.
     
  5. alessandrobraga

    alessandrobraga

    Joined:
    Aug 28, 2022
    Posts:
    11
    Actually the import phase shouldn't be a problem, because i can do a web request to the server in order to upload the assetbundle that i need. My problem is to make both the creating (the assetbundle wouldn't be saved in the assets but in an external folder) and the uploading process at once: just clicking a button in the app.
    So i was wondering if there is a way to run an Editor Script (the assetbundle creating script) in play mode. In other words, there is a way to run the script below in play mode (it's from this page: https://docs.unity3d.com/Manual/AssetBundles-Workflow.html).
    Code (CSharp):
    1. using UnityEditor;
    2. using System.IO;
    3.  
    4. public class CreateAssetBundles
    5. {
    6.     [MenuItem("Assets/Build AssetBundles")]
    7.     static void BuildAllAssetBundles()
    8.     {
    9.         string assetBundleDirectory = "Folder Path";
    10.         if(!Directory.Exists(assetBundleDirectory))
    11.         {
    12.             Directory.CreateDirectory(assetBundleDirectory);
    13.         }
    14.         BuildPipeline.BuildAssetBundles(assetBundleDirectory,
    15.                                         BuildAssetBundleOptions.None,
    16.                                         BuildTarget.StandaloneWindows);
    17.     }
    18. }
     
  6. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I'm not sure how that would help in importing a fbx in play mode.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Not by Unity, but you can do it with third party assets from the Asset Store:

    Screen Shot 2022-11-17 at 7.00.25 AM.png
     
  8. alessandrobraga

    alessandrobraga

    Joined:
    Aug 28, 2022
    Posts:
    11
    Thanks for sharing and I appreciate your support, but I think that there's a misunderstanding. What I need to know is if it is possible, in some way, to run a UnityEditor script (the CreateAssetBundles script) in playmode.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    No.

    That's why we offered alternate solutions based on your description of what you are attempting to do.
     
  10. alessandrobraga

    alessandrobraga

    Joined:
    Aug 28, 2022
    Posts:
    11
    Ok, sorry if I insisted. Thanks.