Sending an Email in a Unity Game for Windows Phone

Creating a game using Unity can be tricky.  Even though scripting in C# is allowed, Mono does not provide the complete experience that .Net does.  Thus, I frequently find that some tasks have to be outside the Unity game project.  One such task is sending an email.

Here’s a little bit of background.  A consumer of a game (Craps Alone) I helped produce for High Iron Software found a bug that completely crashed the game and prevented resumption without a complete re-installation.  While I was certain the bug was related to the serialization process used for saving game data, I could not reproduce the issue.  Therefore, I could only solve the symptom (the inability to relaunch the game), but not the root problem.  To help me figure out where exactly the error was occurring, I decided that when an exception occurs, the app should prompt the user to send an email to support with the details of the exception.  While this is easy enough on the Windows Phone side, Unity’s use of Mono does not make things straightforward.  I’ve found the solution to this and similar issues lies in creating an EventHandler and subscribing to it in the deployed Visual Studio solution.

Within my Unity project, I have a static class that acts as a mediator between my Unity scripts and the XAML/C# Windows Phone 8/8.1 framework.

public static class WindowsStatics
{
    public static event EventHandler OpenEmail;
 
    public static void FireOpenEmail(EmailContent content)
    {
        if (OpenEmail != null)
        {
            OpenEmail(content, null);
        }
    }
}

Originally, I planned on automatically copying the exception stacktrace to the user’s clipboard for pasting within the email.  However, I quickly realized the email recipient, subject, and body can be auto-populated.  When an exception is thrown, I create an EmailContent object, which possesses strings for “To”, “Subject” and “Body”.  Now, in MainPage.xaml.cs in the Windows Phone app project, I subscribe to the event.

// Constructor
public MainPage()
{
    WindowsStatics.OpenEmail += WindowsStatics_OpenEmail;
}

public void WindowsStatics_OpenEmail(object sender, EventArgs e)
{
    var content = (EmailContent)sender;
    var ect = new Microsoft.Phone.Tasks.EmailComposeTask 
              {
                  To = content.To,
                  Subject = content.Subject,
                  Body = content.Body 
              };
    ect.Show();
}

When Show() is called on an EmailComposeTask, the Mail app is launched and automatically populates the given fields.  All the user has to do is touch the send button.

Now, the creating an email within my Unity game is simple.  Constructing a string of an exception’s stacktrace is as easy as calling ToString().

void SendEmailForException(Exception e)
{
    var emailContent = new EmailContent
    {
        To = "support@highironsoftware.com",
        Subject = "Error Report: An exception was thrown during state restore",
        Body = e.ToString()
    };

    WindowsStatics.FireOpenEmail(emailContent);
}

Leave a Reply

Your email address will not be published. Required fields are marked *