Skip to content

Instantly share code, notes, and snippets.

@korchoon
Last active April 1, 2020 12:10
Show Gist options
  • Select an option

  • Save korchoon/4a0891ebe52f5cf2c607b99fd117937e to your computer and use it in GitHub Desktop.

Select an option

Save korchoon/4a0891ebe52f5cf2c607b99fd117937e to your computer and use it in GitHub Desktop.
#line hidden
[Conditional("UNITY_EDITOR")]
void Assert(bool value)
{
if (value) return;
var msg = CallerSrcLine(1);
UnityEngine.Debug.LogError(msg);
throw new Exception(msg);
}
#line default
string CallerSrcLine(int skipFrames = 0)
{
var trace = new StackTrace(1 + skipFrames, true);
var fr = trace.GetFrame(0);
var line = fr.GetFileLineNumber() - 1;
var fileName = fr.GetFileName();
if (!File.Exists(fileName))
return string.Empty;
var lines = File.ReadAllLines(fileName);
return lines[line].Trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment