Snippets

Alex Darby so, you want to debug the unity editor in batch mode?

Created by Alex Darby last modified
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH 
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// add this code to an "Editor" folder in your Unity project and run it like this:
//
// <fullPathToUnity.exe> -buildTarget <yourBuildTarget> -nographics -quit -batchmode -projectPath <projectPath> -logFile ./Log.txt -executeMethod UnityCommandLineEntryPoint.Build
//
// you'll need to look up how to do "Attach to Process" with your debugger of choice but it's not hard, you can find the right onw with trial and error
//
public static class UnityCommandLineEntryPoint
{
    public static void Build()
    {
        if( CommandLineContainsFlag( GetCommandLine(), "-WaitForDebugger" ) )
        {
            var process = System.Diagnostics.Process.GetCurrentProcess();
            while( ! System.Diagnostics.Debugger.IsAttached )
            {
                // 
                // when you see this text in the unity logfile put breakpoint after the loop 
                // and use your debugger's "Attach to Process" functionality to attach to the running process (it's usually called "Unity" or "Unity:<yourProjectName>")
                //
                System.Console.WriteLine( $"{process.ProcessName}.{process.Id}: waiting for debugger..." );
                System.Threading.Thread.Sleep( 5000 );
            }
            
            int putABreakpointHereAndAttachTheDebugger = 0;
        }
        
        //
        // rest of your batch mode editor code goes after this
        //        
    }
    
    public static string[] GetCommandLine()
    {
        return Environment.GetCommandLineArgs();
    }

    /// <summary>
    /// checks command line for a parameter and returns its value 
    /// param values are assumed to be the token following the parameter name
    /// e.g. 
    ///		 -BuildFolder C:\Builds\Game
    /// 	|<---------->|					= CmdParamName 
    /// 				 |<------------>|	= outCmdParamValue
    /// </summary>
    /// <param name="CmdLineTokens">string array of command line tokens - cf.GetCommandLine()</param>
    /// <param name="CmdParamName">case insensitive param name to look for including any expected marker character e.g. "-buildfolder" </param>
    /// <param name="outCmdParamValue"> out param which will contain the exact string provided on the command line, or null if not found</param>
    /// <returns> true if CmdParamName and outCmdParamValue was set, else false</returns>
    public static bool TryGetCommandLineParamValue( string[] CmdLineTokens, string CmdParamName, out string outCmdParamValue )
    {
        string strParamNameLower = CmdParamName.ToLower();

        for( int i = 0; i < CmdLineTokens.Length; ++i )
        {
            if( ( strParamNameLower == CmdLineTokens[i].ToLower() )
               && ( ( i + 1 ) < CmdLineTokens.Length ) )
            {
                outCmdParamValue = CmdLineTokens[i + 1];
                return true;
            }
        }

        outCmdParamValue = null;
        return false;
    }

    /// <summary>
    /// checks command line for a flag and returns treu if found
    /// </summary>
    /// <param name="CmdLineTokens">string array of command line tokens - cf.GetCommandLine()</param>
    /// <param name="CmdFlagName">case insensitive param name to look for including any expected marker character e.g. "-buildfolder" </param>
    /// <returns>true if found, else false</returns>
    public static bool CommandLineContainsFlag( string[] CmdLineTokens, string CmdFlagName )
    {
        string strFlagNameLower = CmdFlagName.ToLower();

        for( int i = 0; i < CmdLineTokens.Length; ++i )
        {
            if( strFlagNameLower == CmdLineTokens[i].ToLower() )
            {
                return true;
            }
        }

        return false;
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.