In Unity, to exclude a specific folder from the build, you can use the following approaches:
1. Using Editor Scripts
You can create an editor script to exclude specific folders or assets from the build. Here is a simple example:
- Create a folder named
Editor in your Unity project if it doesn't already exist.
- Inside the
Editor folder, create a C# script, for example: ExcludeFromBuild.cs.
using UnityEditor;
using UnityEngine;
public static class ExcludeFromBuild
{
[MenuItem("Build/Exclude folders from Build")]
public static void ExcludeFolders()
{
string[] foldersToExclude = new string[]
{
"Assets/FolderToExclude1",
"Assets/FolderToExclude2"
};
foreach (string folder in foldersToExclude)
{
string[] guids = AssetDatabase.FindAssets("", new[] { folder });
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
AssetDatabase.SetLabels(AssetDatabase.LoadMainAssetAtPath(path), new string[] { "ExcludeFromBuild" });
}
}
Debug.Log("Folders excluded from build.");
}
}
- To apply the exclusion, go to the Unity Editor menu:
Build -> Exclude folders from Build.
- The script above adds a label to all assets in the specified folders. You need to add filters for these labels in your Asset Bundles or build process to ensure they are not included.
2. Using Asset Bundles
Asset Bundles allow you to package assets and load them dynamically, thus excluding them from the initial build:
- Tag the assets in the folders you want to exclude by creating an AssetBundle.
- Use
BuildPipeline.BuildAssetBundles to build these Asset Bundles separately.
3. AssetBundle Manager Tool
Unity's AssetBundle Manager can help you manage Asset Bundles more easily. You can specify which assets are included in which Asset Bundles and exclude those Asset Bundles from your main build.
4. Conditional Compilation
You can also use script defines and conditional compilation to exclude specific folders based on the build configuration.
5. Using .meta File Labels
You can add particular labels to the .meta files of the folders you want to exclude and then create custom build scripts to exclude assets with specific labels.
Example
Excluding assets with a custom label during the build process:
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
public class ExcludeLabelProcessor : IPreprocessBuildWithReport
{
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
string[] assetPaths = AssetDatabase.GetAllAssetPaths();
foreach (string assetPath in assetPaths)
{
var labels = AssetDatabase.GetLabels(AssetDatabase.LoadMainAssetAtPath(assetPath));
if (System.Array.Exists(labels, label => label == "ExcludeFromBuild"))
{
AssetDatabase.DeleteAsset(assetPath);
}
}
}
}
You can combine the above techniques as needed to best fit your specific requirements.