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

Question Build asset bundles when play mode button is clicked

Discussion in 'Editor & General Support' started by kurifodo, Nov 13, 2020.

  1. kurifodo

    kurifodo

    Joined:
    Jan 17, 2019
    Posts:
    33
    Hi,

    I'm using Asset Bundles and the standard Unity build pipeline. I've use the following editor script to kickoff asset bundle build for my project.

    Code (CSharp):
    1. [MenuItem( "Assets/Asset Bundles/Build Windows x64" )]
    2. static void BuildWindows64()
    3. {
    4.     // code to build omitted
    5. }
    My current workflow requires me to manually build assets via the menu item above before clicking play button. I'd like to know if I can simplify this workflow. Ideally, when I click the play button, the build would automatically occur for asset bundles and then begin playing. How can this be achieved?

    Thanks for your help.
     
  2. kurifodo

    kurifodo

    Joined:
    Jan 17, 2019
    Posts:
    33
  3. kurifodo

    kurifodo

    Joined:
    Jan 17, 2019
    Posts:
    33
  4. kurifodo

    kurifodo

    Joined:
    Jan 17, 2019
    Posts:
    33
  5. kurifodo

    kurifodo

    Joined:
    Jan 17, 2019
    Posts:
    33
  6. kurifodo

    kurifodo

    Joined:
    Jan 17, 2019
    Posts:
    33
  7. kurifodo

    kurifodo

    Joined:
    Jan 17, 2019
    Posts:
    33
  8. JolanHuyvaertDieKeure

    JolanHuyvaertDieKeure

    Joined:
    Mar 1, 2021
    Posts:
    9
  9. phisn

    phisn

    Joined:
    Jul 9, 2020
    Posts:
    1
    I did also have the same problem. I solved it with this script.

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.IO;
    6. using System.Linq;
    7. using UnityEditor;
    8. using UnityEngine;
    9.  
    10. [InitializeOnLoad]
    11. public static class AssetBundleBuilder
    12. {
    13.     static AssetBundleBuilder()
    14.     {
    15.         var expectedWriteTime = AssetDatabase.GetAllAssetBundleNames()
    16.             .SelectMany(AssetDatabase.GetAssetPathsFromAssetBundle)
    17.             .Select(File.GetLastWriteTime)
    18.             .Max();
    19.        
    20.         var lastWriteTime = File.GetLastWriteTime("Assets/AssetBundles");
    21.        
    22.         Debug.Log($"Expected write time: {expectedWriteTime}" +
    23.                   $"\nLast write time: {lastWriteTime}" +
    24.                   $"\nDifference: {lastWriteTime - expectedWriteTime}");
    25.  
    26.         if (lastWriteTime < expectedWriteTime)
    27.         {
    28.             Debug.Log("Building asset bundles");
    29.  
    30.             if (Directory.Exists("Assets/AssetBundles") == false)
    31.             {
    32.                 Directory.CreateDirectory("Assets/AssetBundles");
    33.             }
    34.        
    35.             BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows64);
    36.         }
    37.     }
    38. }
    39.  
    In my use case, the building of AssetBundles does not take a long time, so you might need to add some sort of min time since the last change.
     
  10. kurifodo

    kurifodo

    Joined:
    Jan 17, 2019
    Posts:
    33
    I found the proper attribute to use for running code on entering play mode from the editor: https://docs.unity3d.com/ScriptReference/InitializeOnEnterPlayModeAttribute.html

    Thus, combining the below code with the code in my original post above, I build asset bundles like so. The asset bundles will not be built if they have already been built (no changes since last build) -- in other words, no time is lost by doing this each time I enter play mode.

    Code (CSharp):
    1. [InitializeOnEnterPlayMode]
    2. static void OnEnterPlaymodeInEditor( EnterPlayModeOptions options )
    3. {
    4.     Debug.Log( "Ensuring asset bundles are built" );
    5.     AssetBundleBuilder.BuildWindows64();
    6. }
     
  11. alessandrobraga

    alessandrobraga

    Joined:
    Aug 28, 2022
    Posts:
    11
    How can you combine the two codes? Sorry but I'm new with Unity and C#