cglogo

Unity Snippet: Custom Build Menu

11 June 2023

Ever wanted to build for Windows, Linux and MacOS but tired of waiting for Unity to change platform whenever you change that pesky dropdown? I certainly have, but you don’t have to!

The class below creates a menu called “Capable Builder” with 4 options, build your app for all platforms, or build it for just Windows, Linux or MacOS – All you need to do to get it up and running yourself is to insert all the levels you want to build in the GetLevels function (if you have more than one) and pop it into your project.

using UnityEditor;
using UnityEngine;

public class CapableBuilder : MonoBehaviour
{
    static string[] GetLevels()
    {
        return new string[] {};
    }

    static BuildOptions GetBuildOptions()
    {
        return BuildOptions.None;
    }
    
    [MenuItem("Capable Builder/Build All", false, 1)]
    static void BuildAll()
    {
        BuildLinux();
        BuildMac();
        BuildWindows();
    }

    [MenuItem("Capable Builder/Build Windows", false, 101)]
    static void BuildWindows()
    {
        BuildPipeline.BuildPlayer(
            GetLevels(),
            "./Builds/Windows/MoRBS.exe",
            BuildTarget.StandaloneWindows64,
            GetBuildOptions()
        );
    } 
    
    [MenuItem("Capable Builder/Build Linux", false, 102)]
    static void BuildLinux()
    {
        BuildPipeline.BuildPlayer(
            GetLevels(),
            "./Builds/Linux/MoRBS",
            BuildTarget.StandaloneLinux64,
            GetBuildOptions()
        );
    }
    
    [MenuItem("Capable Builder/Build Mac", false, 103)]
    static void BuildMac()
    {
        BuildPipeline.BuildPlayer(
            GetLevels(),
            "./Builds/Mac/MoRBS",
            BuildTarget.StandaloneOSX,
            GetBuildOptions()
        );
    }
}

Share to socials