Net-Game/Library/PackageCache/com.unity.2d.common@bb1fc9b3d81b/Samples~/SpriteAtlas/FromAssetBundle/Editor/CreateAssetBundle.cs
2025-03-28 08:33:16 -04:00

35 lines
991 B
C#

using UnityEngine;
using UnityEditor;
using System.IO;
#if UNITY_EDITOR
// Ensure class initializer is called whenever scripts recompile
[InitializeOnLoad]
internal static class CreateAssetBundle
{
// Register an event handler when the class is initialized
static CreateAssetBundle()
{
EditorApplication.playModeStateChanged += PlayModeStateChange;
}
static void PlayModeStateChange(PlayModeStateChange state)
{
if (state == UnityEditor.PlayModeStateChange.ExitingEditMode)
CreateAssetBundles();
}
static void CreateAssetBundles()
{
#if ASSETBUNDLE_ENABLED
string assetBundleDirectory = "Assets/StreamingAssets";
if (!Directory.Exists(Application.streamingAssetsPath))
{
Directory.CreateDirectory(assetBundleDirectory);
}
BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);
#endif
}
}
#endif