Stack traces currently only show coroutine functions as “MoveNext” which is not what i want. Here’s my test code:
using UnityEngine;
using System.Collections;
using System.Diagnostics;
public class TestCoroutineStackTrace : MonoBehaviour {
// Use this for initialization
IEnumerator Start () {
TestStackTrace();
yield return null;
}
void TestStackTrace() {
StackTrace theTrace = new StackTrace();
foreach(StackFrame f in theTrace.GetFrames()) {
UnityEngine.Debug.Log(f.GetMethod().Name);
}
}
}
Output:
TestStackTrace
UnityEngine.Debug:Log(Object)
MoveNext
UnityEngine.Debug:Log(Object)
I’m not at all familiar with System.Diagnostics (so it may contain a way to do this), but you might be able to use Unity’s built-in StackTraceUtility class. Since Unity’s stack traces always put coroutines inside of < and > characters, you can get the current coroutine using something like this:
private IEnumerator TestCoroutine()
{
string stackTrace = StackTraceUtility.ExtractStackTrace();
string coroutineName = stackTrace.Substring(stackTrace.IndexOf('<') + 1, stackTrace.IndexOf('>') - 1);
Debug.Log(coroutineName);
yield break;
}
I don’t know what your ultimate goal is (and maybe you really need to use StackFrame for some reason), but this should get you the coroutine name.
Here’s how to do it using .net’s System.Diagnostics:
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
string fullName = st.GetFrame(1).GetMethod().ReflectedType.Name;
string coroutineMethodName = fullName.Substring(fullName.IndexOf('<') + 1, fullName.IndexOf('>') - 1);
I’m not sure the pros or cons of using either. Maybe only Unity version is available to javascript and boo?