Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by insominx · Mar 15, 2013 at 05:19 AM · coroutinedebugreflection

How can I get the name of a coroutine method using a StackTrace?

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)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by kromenak · Mar 15, 2013 at 05:48 AM

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 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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image insominx · Mar 15, 2013 at 04:22 PM 0
Share

Unity's StackTraceUtility returns this:

Game$$anonymous$$anger:GameUpdateDebug() (at Assets/Tone Assets/Scripts/Singletons/Game$$anonymous$$anger.cs:342) c__IteratorF:$$anonymous$$oveNext() (at Assets/Tone Assets/Scripts/Singletons/Game$$anonymous$$anger.cs:292)

It's "GameUpdate" (stackframe(1)) that I'm looking for specifically and it's in there but only as part of the full name which I'd have to fish out. The same data exists for the regular .net version too.

$$anonymous$$y purpose is just to be careful about running debug code so that it can only be called from one particular function. Not really necessary but it helps me be explicit about my code's intentions.

avatar image
0

Answer by insominx · Mar 15, 2013 at 04:37 PM

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weird coroutine behaviour, speed increases too much 2 Answers

Why isn't this coroutine fully firing? 1 Answer

Get a stack trace to find unknown caller 0 Answers

How to debug a coroutine? 2 Answers

My code is working wierd! Please help me understand why!,My code is working weird! Pleas help me understand why 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges