Skip to content

Commit 686ee20

Browse files
authored
chore: Updated Samples (#2256)
1 parent f21af55 commit 686ee20

21 files changed

+4239
-1567
lines changed

samples/unity-of-bugs/Assets/Scenes/1_Bugfarm.unity renamed to samples/unity-of-bugs/Assets/Scenes/1_BugFarm.unity

Lines changed: 393 additions & 637 deletions
Large diffs are not rendered by default.

samples/unity-of-bugs/Assets/Scenes/2_NativeSupport.unity

Lines changed: 183 additions & 62 deletions
Large diffs are not rendered by default.

samples/unity-of-bugs/Assets/Scenes/3_AdditionalSamples.unity

Lines changed: 127 additions & 38 deletions
Large diffs are not rendered by default.

samples/unity-of-bugs/Assets/Scenes/4_ThreadedSamples.unity

Lines changed: 295 additions & 645 deletions
Large diffs are not rendered by default.

samples/unity-of-bugs/Assets/Scenes/5_ForceCrashSamples.unity

Lines changed: 3011 additions & 0 deletions
Large diffs are not rendered by default.

samples/unity-of-bugs/Assets/Scenes/5_ForceCrashSamples.unity.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using System.Collections;
2-
using System.Threading;
1+
using System.Threading;
32
using Sentry;
43
using Sentry.Unity;
54
using UnityEngine;
65

7-
public class AdditionalButtons : MonoBehaviour
6+
public class AdditionalSampleButtons : MonoBehaviour
87
{
98
public void SetUser()
109
{
10+
// Setting the user on the scope makes sure the user is set on the context of all future events
1111
SentrySdk.ConfigureScope(s =>
1212
{
1313
s.User = new SentryUser
@@ -20,7 +20,7 @@ public void SetUser()
2020
Debug.Log("User set: ant");
2121
}
2222

23-
class PlayerCharacter
23+
private class PlayerCharacter
2424
{
2525
public string Name { get; set; }
2626
public int Age { get; set; }
@@ -29,7 +29,9 @@ class PlayerCharacter
2929

3030
public void CaptureMessageWithContext()
3131
{
32-
SentrySdk.ConfigureScope(scope =>
32+
// The context is freely customizable and allows you to add data specific to your game.
33+
// The SDKs capture methods provide an optional scope that is only getting applied for that one specific event
34+
SentrySdk.CaptureMessage("Capturing with player character context.", scope =>
3335
{
3436
scope.Contexts["character"] = new PlayerCharacter
3537
{
@@ -38,30 +40,14 @@ public void CaptureMessageWithContext()
3840
AttackType = "melee"
3941
};
4042
});
41-
42-
SentrySdk.CaptureMessage("Capturing with player character context.");
43-
SentrySdk.ConfigureScope(scope => scope.Contexts = null);
4443
}
4544

46-
public void CaptureMessageWithScreenshot() => StartCoroutine(CaptureScreenshot());
47-
4845
public void ApplicationNotResponding()
4946
{
5047
Debug.Log("Running Thread.Sleep() on the UI thread to trigger an ANR event.");
5148
Thread.Sleep(6 * 1000); // ANR detection currently defaults to 5 seconds
5249
Debug.Log("Thread.Sleep() finished.");
5350
}
5451

55-
private IEnumerator CaptureScreenshot()
56-
{
57-
yield return new WaitForEndOfFrame();
58-
SentrySdk.ConfigureScope(s =>
59-
{
60-
var screenshot = ScreenCapture.CaptureScreenshotAsTexture();
61-
s.AddAttachment(screenshot.EncodeToJPG(), "screenshot.jpg");
62-
});
63-
64-
SentrySdk.CaptureMessage("Captured a message with a screenshot attachment");
65-
SentrySdk.ConfigureScope(scope => scope.ClearAttachments());
66-
}
52+
public void Assert() => UnityEngine.Assertions.Assert.IsTrue(false);
6753
}
Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,98 @@
11
using System;
22
using System.Runtime.CompilerServices;
3-
using Sentry;
43
using Sentry.Unity;
54
using UnityEngine;
6-
using UnityEngine.Assertions;
75

86
public class BugFarmButtons : MonoBehaviour
97
{
108
private void Awake()
119
{
12-
Debug.Log("Sample 🐛");
10+
Debug.Log("The 🐛s awaken!");
1311
}
1412

1513
private void Start()
1614
{
17-
Debug.Log("Sample Start 🦋");
15+
// Log messages are getting captured as breadcrumbs
16+
Debug.Log("Starting the 🦋-Farm");
1817
Debug.LogWarning("Here come the bugs 🐞🦋🐛🐜🕷!");
1918
}
2019

21-
public void AssertFalse() => Assert.AreEqual(true, false);
22-
23-
[MethodImpl(MethodImplOptions.NoInlining)]
24-
public void ThrowNull() => throw new NullReferenceException();
20+
public void ThrowUnhandledException()
21+
{
22+
Debug.Log("Throwing a unhandled 🕷 exception!");
23+
DoSomeWorkHere();
24+
}
2525

26-
public void ThrowExceptionAndCatch()
26+
public void ThrowExceptionButCatch()
2727
{
28-
Debug.Log("Throwing an instance of 🐛 CustomException!");
28+
Debug.Log("Throwing an exception but catching it! 🐜");
2929

3030
try
3131
{
32-
throw new CustomException("Custom bugs 🐛🐛🐛🐛.");
32+
DoSomeWorkHere();
3333
}
3434
catch (Exception e)
3535
{
3636
SentrySdk.CaptureException(e);
3737
}
3838
}
3939

40-
public void ThrowNullAndCatch()
40+
private void DoSomeWorkHere()
4141
{
42-
Debug.Log("Throwing 'null' and catching 🐜🐜🐜 it!");
43-
44-
try
42+
if (CheckSomeFakeWork())
4543
{
46-
ThrowNull();
47-
}
48-
catch (Exception e)
49-
{
50-
SentrySdk.CaptureException(e);
44+
DoSomeWorkThere();
5145
}
5246
}
5347

54-
public void CaptureMessage() => SentrySdk.CaptureMessage("🕷️🕷️🕷️ Spider message 🕷️🕷️🕷️🕷️");
55-
56-
// IL2CPP inlines this anyway :( - so we're adding some fake work to prevent the compiler from optimizing too much
57-
[MethodImpl(MethodImplOptions.NoInlining)]
58-
private void StackTraceExampleB()
48+
private void DoSomeWorkThere()
5949
{
60-
var someWork = DateTime.Now.ToString();
61-
if (someWork.Length > 0) // This condition will always be true but compiler can't be certain
50+
if (CheckSomeFakeWork())
6251
{
63-
throw new InvalidOperationException("Exception from a lady beetle 🐞");
52+
throw new CustomException("Exception from an exceptional lady beetle 🐞!");
6453
}
6554
}
6655

67-
// IL2CPP inlines this anyway :( - so we're adding some fake work to prevent the compiler from optimizing too much
68-
[MethodImpl(MethodImplOptions.NoInlining)]
69-
public void StackTraceExampleA()
56+
public void CaptureMessage()
7057
{
71-
var someWork = DateTime.Now.ToString();
72-
if (someWork.Length > 0) // This condition will always be true but compiler can't be certain
58+
if (CheckSomeFakeWork())
7359
{
74-
StackTraceExampleB();
60+
// Messages do not have a stacktrace attached by default. This is an opt-in feature.
61+
// Note: That stack traces generated for message events are provided without line numbers. See known limitations
62+
// https://docs.sentry.io/platforms/unity/troubleshooting/known-limitations/#line-numbers-missing-in-events-captured-through-debuglogerror-or-sentrysdkcapturemessage
63+
SentrySdk.CaptureMessage("🕷️🕷️🕷️ Spider message 🕷️🕷️🕷️🕷️");
7564
}
7665
}
7766

78-
// IL2CPP inlines this anyway :( - so we're adding some fake work to prevent the compiler from optimizing too much
79-
[MethodImpl(MethodImplOptions.NoInlining)]
8067
public void LogError()
8168
{
82-
var someWork = DateTime.Now.ToString();
83-
if (someWork.Length > 0) // This condition will always be true but compiler can't be certain
69+
if (CheckSomeFakeWork())
8470
{
71+
// Error logs get captured as messages and do not have a stacktrace attached by default. This is an opt-in feature.
72+
// Note: That stack traces generated for message events are provided without line numbers. See known limitations
73+
// https://docs.sentry.io/platforms/unity/troubleshooting/known-limitations/#line-numbers-missing-in-events-captured-through-debuglogerror-or-sentrysdkcapturemessage
8574
Debug.LogError("Debug.LogError() called");
8675
}
8776
}
88-
}
8977

90-
public class CustomException : Exception
91-
{
92-
public CustomException(string message) : base(message)
78+
public void LogException()
79+
{
80+
if (CheckSomeFakeWork())
81+
{
82+
// Error logs get captured as messages and do not have a stacktrace attached by default. This is an opt-in feature.
83+
Debug.LogException(new NullReferenceException("Some bugs are harder to catch than others. 🦋"));
84+
}
85+
}
86+
87+
// NoInlining ends up being inlined through L2CPP anyway. :(
88+
// We're checking some fake work here to prevent too aggressive optimization. That way, we can show off some proper
89+
// stack traces that are closer to real-world bugs and events.
90+
[MethodImpl(MethodImplOptions.NoInlining)]
91+
private static bool CheckSomeFakeWork() => DateTime.Now.Ticks > 0; // Always true but not optimizable
92+
93+
private class CustomException : Exception
9394
{
95+
public CustomException(string message) : base(message)
96+
{ }
9497
}
9598
}

0 commit comments

Comments
 (0)