LoginSignup
0
0

【Unity】Debug.Log で StackTrace を活用してみる。(そのメソッドの実行元を知る)

Last updated at Posted at 2024-04-10

この記事は特定のメソッドが
様々な場所から実行されている場合に
実行元を辿る事が出来るログを出す方法を
提案する記事です。

サンプルコードを載せて
その解説をします。


サンプルコード

public class LogTest
{
    int count = 23;
    string text = "Hellow World!!";

    public void ShowLog()
    {
        try
        {
            throw new System.Exception();
        }
        catch (System.Exception e)
        {
            Debug.Log("count: " + count + ", text: " + text + ", Hierarchy is \n" + e.StackTrace);
        }
    }
}

では解説していきます。

実行元を特定するのは
e.StackTrace の場所です。

例えば次のようなログが出力されます。

出力されるログ
count: 23, text: Hellow World!!, Hierarchy is 
  at LogTest.ShowLog () [0x00000] in C:\Users\User\Test4\Assets\LogTest.cs:10 
UnityEngine.Debug:Log (object)
LogTest:ShowLog () (at Assets/LogTest.cs:14)
ClassA:Start() (at Assets/ClassA.cs:12)

このログを見ると
ClassA の Start メソッドから
実行されていることが分かります。

ClassA の Start メソッドが
他の場所から実行されているならば
その情報も知ることができます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0