Unityunity_crashreporting_bugsplat_small

10 minute read

Summary

BugSplat supports both Windows Native and Cross-Platform .NET error reporting for Unity. Our native support captures both native C++ and mono based .NET call stacks with function names and line numbers. Our cross-platform .NET support captures exceptions on Windows, UWP, OS X and Linux. Combining the two provides powerful insight into the stability of your application.

Prerequisites

Please make sure you have completed the following checklist:

Windows Native Crash Support


BugSplat collects Unity crash files in order to provide both Windows Native and .NET call stacks. This is especially valuable for games that utilize C++ libraries and plugins. We have provided a sample application MyUnityCrasher that demonstrates a working BugSplatUnity integration. Please note that collection of crashes that occur when running inside the Unity editor is not supported at this time, but we plan to add support in a future release.

In your Assets/Plugins directory, create two subfolders: Assets/Plugins/x86 and Assets/Plugins/x86_64. Copy the appropriate version of BugSplatUnity.dll into each of these folders:

Unity, crash report, crash reporting

Copy BugSplat/samples/MyUnityCrasher/Assets/Code/BugSplat.cs to your Assets directory:

Unity, crash report, crash reporting

At your application’s entry point add a call to BugSplat.Init and pass it your database, company name, product name, product version, user description and flag. If flag is set to 0 the BugSplat dialog will be shown at crash time. If flag is set to MDSF_NONINTERACTIVE no dialog will be shown at crash time.

Code/Main.cs

public class Main: MonoBehavior
{
  ...
  void Start()
  {
    BugSplat.Init(Application.companyName, "fred", Application.productName, Application.version, "Test description", 0);
    ...
  }
  ...
}

Additionally you can use BugSplat.AddAdditionalFile, BugSplat.RemoveAdditionalFile, BugSplat.SetDefaultUserDescription, BugSplat.SetDefaultUserEmail and BugSplat.SetDefaultUserName to further customize your BugSplat configuration:

Code/Main.cs

public class Main: MonoBehavior
{
  static readonly string TempDir = Path.GetTempPath();
  void Start()
  {
    ...
    var additionalFilePath = Path.Combine(TempDir, "additionalFile.txt");
    File.WriteAllText(additionalFilePath, "Hello world!");
    BugSplat.AddAdditionalFile(additionalFilePath);
    BugSplat.SetDefaultUserDescription("BugSplat rocks!");
    BugSplat.SetDefaultUserEmail("fred@bedrock.com");
    BugSplat.SetDefaultUserName("Fred Flintstone");
  }
}

Add a call to BugSplat.NativeCrash so that you can test BugSplat. Remove this call after you have successfully integrated BugSplat:

Code/Main.cs

public class Main: MonoBehavior
{
  ... 
  void Update()
  {
    if (Input.GetKeyDown(KeyCode.M))
    {
      BugSplat.NativeCrash();
    }
  }
}

Create a post build step if you don’t already have one. Add a method that copies BugSplat to your application’s output directory. NOTE: the build script must be inside a folder named “Editor” otherwise Unity will not resolve the Unity.Editor reference correctly. Please refer to the MyUnityCrasher sample for full implementations of CopyPluginSymbols, CopyRuntimeSymbols and CopyBugSplat:

Code/Editor/PostBuild.cs

public class BuildPostprocessor
{
  const string UNITY_ASSEMBLY_FILENAME = "UnityEngine.dll";
  const string UNITY_ASSEMBLY_MDB_FILENAME = UNITY_ASSEMBLY_FILENAME + ".mdb";  
  const string MAIN_ASSEMBLY_FILENAME  = "Assembly-CSharp.dll";
  const string MAIN_ASSEMBLY_MDB_NAME_FILENAME  = MAIN_ASSEMBLY_FILENAME + ".mdb";

  static string _platform;

  [PostProcessBuild(1)]
  public static void OnPostprocessBuild(BuildTarget target, stringpathToBuiltProject)
  {
    switch(target)
    {
      case BuildTarget.StandaloneWindows64:  _platform = "x86_64"; break;  
      case BuildTarget.StandaloneWindows:  _platform = "x86"; break;  
      default: _platform = string.Empty; break;
    }

    if (!string.IsNullOrEmpty(_platform))
    {
      var builtProjectDir = Path.GetDirectoryName(pathToBuiltProject);  
      var projectDir = Path.GetDirectoryName(Application.dataPath);  
      var tempSymbolsDir = Path.Combine(builtProjectDir, "Symbols");
      var bugSplatDir = Path.Combine(projectDir, @"..\..\bin");

      if (!Directory.Exists(tempSymbolsDir)) 
      {
        Directory.CreateDirectory(tempSymbolsDir); 
      }
    
      CopyPluginSymbols(projectDir, tempSymbolsDir);  
      CopyRuntimeSymbols(builtProjectDir, tempSymbolsDir);  
      CopyBugSplat(builtProjectDir, bugSplatDir);  
      RunSendPdbs(builtProjectDir, bugSplatDir);

      Directory.Delete(tempSymbolsDir, true);
    }
  }
  ...
}

In order to get good call stacks you will need to upload symbols for each unique build. Add another method to your post build step that runs SendPDBs.exe. If you haven’t made and changes to your plugins you can use Ctrl+C to skip the SendPDBs dialog. This step can also be added to the end of your build pipeline instead:

Code/Editor/PostBuild.cs

public class BuildPostprocessor
{
  ...
  static void RunSendPdbs(string builtProjectDir, string bugSplatDir)
  {
    var sendPdbsPath = Path.Combine(bugSplatDir, "SendPdbs.exe");
    var startInfo = new ProcessStartInfo(sendPdbsPath); 
    startInfo.Arguments = "/u Fred"
      + " /p Flintstone"
      + " /b Fred"
      + " /a " + Application.productName
      + " /v " + Application.version
      + " /d \"" + builtProjectDir + "\""  
      + " /f \"Symbols/*;*.exe;*.pdb";
    using (var process = Process.Start(startInfo))
    {
      process.WaitForExit();
    } 
  }
}

Build your game with the “Copy PDB files” option selected:

Unity, crash report, crash reporting Unity, crash report, crash reporting

Navigate to the Crashes page on BugSplat. You should see a new crash report. Clicking the crash’s ID will redirect you to the Individual Crash page which will show you detailed information about the crash:

Unity, crash report, crash reporting

Notice the Individual Crash page contains the native call stack from the dump file generated by Unity. To see the mixed-mode call stack from output_log.txt, click the Calculate Details button:

Unity, crash report, crash reporting

That’s it! Your game is configured to post native crash reports to BugSplat. Good luck!

Cross-Platform .NET


The BugSplatUnity.unitypackage file contains everything you need to integrate BugSplat into your. Once you've downloaded BugSplatUnity SDK, double-click the unitypackage file to launch Unity. You will be prompted to create or select a project. Once you've selected a project, click "Import" to import BugSplat. After the import is complete, the tool will be ready to use.

(Note: At least for our team, Mac users have to manually import the package contents.)

Exploring the BugSplat Sample

To get a feel of the BugSplat service before using it in your application, try out the debug scene embedded as a sample in the BugSplat Report Tool.

Double-click the debug scene icon found in the Projects tab at Assets > bugsplat > report > scene > debug.

Unity, crash report, crash reporting

Hit the Play button and you will see our example running as shown below. The scene consists of three test buttons and an animated cube.

The Prompted Exception button will create an interactive crash report dialog that allows the user to provide information and confirm the crash report should be sent to BugSplat.

The Custom Exception button will create a report using a custom Exception (i.e., created using throw new Exception(“msg”).

The Null Reference Exception button will force a Null Reference Exception (i.e., a common runtime exception in Unity).

Unity, crash report, crash reporting

By default, crashes are posted to the BugSplat database "Fred". Login to BugSplat with the username Fred and password Flintstone and navigate to the Crashes page to view these crash reports.

If you would like to post crashes to your own database, select the app GameObject and in the Inspector expand the Reporter field. Provide your own values for Database, App, Version and the optional Key parameters.

Unity, crash report, crash reporting

Integrating With Your Game

Follow the steps below to create a Reporter instance that will handle the detection of Errors and also allow users to send their own reports to the BugSplat server.

  1. Create an empty script or modify an existing script.
  2. Add using BugSplat; to add the BugSplat namespace to your script.
  3. Add public Reporter reporter; to add a Reporter as a public property.
  4. On the Start or Awake method call reporter.Initialize(gameObject); to configure BugSplat to start watching for Exceptions.
  5. Unity, crash report, crash reporting
  6. Add your script to a GameObject and check that Reporter is shown in the Inspector. Unity, crash report, crash reporting
  7. Configure your BugSplat integration:
  8. FieldDescription
    DatabaseTarget database where reports will be sent
    AppApp name string
    VersionVersion string of the app
    QuietFlag that allows ReportComponent to emit debug logs so the user can confirm things are working
    Previous*Last sent report
    Generate ScreenshotsFlag that allows the capture of screenshots before a Report is generated
    Generate Log FileFlag that allows the creation of a text file with all logs before the Report
    PromptFlag that allows the use of a dialog before sending a Report
    IgnoredNumber of ignored repeated reports
    LogsList of detected Unity logs
    IgnoredNumber of ignored repeated reports
    CountNumber of Reports sent

Exporting

Mac OS X
  1. File > Build Settings...
  2. Click PC, Mac & Linux Standalone
  3. Optional: Click Development build to enable line numbers
  4. Unity, crash report, crash reporting
  5. Click Build and Run
Windows 7-10
  1. File > Build Settings...
  2. Add Open Scenes
  3. Click PC, Mac & Linux Standalone
  4. Optional: Click Copy PDB files to enable line numbers
  5. Unity, crash report, crash reporting
  6. Click Build and Run
Universal Windows Platform
  1. File > Build Settings...
  2. Add Open Scenes
  3. Use the following settings to get function names and line numbers
  4. Unity, crash report, crash reporting
  5. Click Build
  6. Select a folder for the Windows Store Visual Studio project to be saved in
  7. Open the newly created project in Visual Studio
  8. Add the Internet (Client) permission to the Package.appxmanifest file
  9. Unity, crash report, crash reporting Unity, crash report, crash reporting Unity, crash report, crash reporting
  10. Run your app with Visual Studio and click continue when it catches an exception