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
1
Question by Fattie · Jun 07, 2013 at 08:50 AM · reflection.net

Finding all USES of a class or function .... at runtime.

Say you have a class

 static class teste
     {
     function test( s:String ) { Debug.Log("called with "+s); }
     }

Of course, anywhere at all in the project there could be calls such as:

 teste.say("abc");
 teste.say("def");
 teste.say("mno");

What I would like to be able to do ...

while the project is running on a device, find all the places where "teste.test()" is called. (Of course, some calls may not be constants, adding a further complication.)

Perhaps this is impossible and only really possible at editor-script time? (Like using Doxygen, say.)

Any experts in .Net / reflection / etc have any thoughts on this?

Comment
Add comment · Show 1
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 shaderop · Jun 12, 2013 at 10:32 AM 0
Share

Is there a reason why you can't use conditional compilation with a dash of #ifdef sprinkled through out the code? It's seems to be a lot less hassle that way and would effectively produce the same result.

3 Replies

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

Answer by Jamora · Jun 07, 2013 at 09:28 AM

An interesting question. I would approach this problem by first modifying the speak method such, that it has an argument of who called it.

Then I would create,say class CallerList that contains a list of MonoBehaviours with a static function AddCaller(message:string, caller:MonoBehvaiour), and at each call to teste.speak I would add the caller to that list. The calling would be teste.speak("the message",this)

This way, you will have all calls to the stored in the list, which could be printed out or whatever you need to do with them. And the final result would be something like

function speak( s:String, caller:MonoBehaviour ){ voc.deutsche(phonemes(prosidics((s))); CallerList.AddCaller(s,caller); }

presudocode:
class CallerList{ private static List monoBehaviourList private static List messageList

static method AddCaller(string message, MonoBehaviour caller){
monoBehviourList.Add(caller)
messageList.Add(message)}
}

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 Fattie · Jun 07, 2013 at 09:34 AM 0
Share

Hi Jam !

two thoughts..

(A) for the "caller" you could use Reflection to get who called, so that could be automated.

(B) the only problem with your approach: unfortunately it only collates friends who have already called the function.

Unfortunately, in contrast, what I want to do is get them "all ahead of time". At launch, say.

the non-automatic approach would be to add call in Awake(), such as:

  teste.IAmGoingToUseThisLater( "go to hell" );

and that's fine. That would achieve everything I'm saying. But I want automation! :)

avatar image
1

Answer by Bunny83 · Jun 12, 2013 at 09:55 AM

I've seen the question a few days ago, but had no time to answer it.

AFAIK there's no built-in way to do something like this because:

  • Reflection only works on types / classes / instances / members.

  • You need to find references in plain code which can't be reflected, but only decompiled / disassembled.

  • Even when you have a disassembler / decompiler at runtime Unity doesn't make the job very easy since you can use SendMessage / Invoke / or reflection to call a method or to change a value.

The only practical way would be to disassemble the code (not at runtime but after building it) and do a plain text search through the code. This is far from being automated, is not very reliable and still requires user interaction / knowledge to comprehend the findings.

At edit time, so before compilation, you can use MonoDevelop and it's refactoring features and tools. However since i figured out that the "find references" feature could fail miserably, i don't use it anymore. If i really need a more or less reliable list, i open the project in Visual Studio and search there ;)

The funny thing is starting VisualStudio, open the project, search for references and switching back to Mono is faster than waiting for the "find references" in MonoDevelop...

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 Fattie · Jun 12, 2013 at 11:59 AM 0
Share

you ROC$$anonymous$$ - but you knew that

avatar image
1

Answer by shaderop · Jun 12, 2013 at 10:30 AM

Mono Cecil is supposed to enable injecting code into compiled assemblies, but the documentation is abysmal and I have never used myself. So I can't offer any specifics on usage. But in theory you should be able to load a .NET DLL or EXE, find a method, modify its implementation, and then save out the results.

[2]: http://codeinject.codeplex.com/

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 Fattie · Jun 12, 2013 at 11:58 AM 0
Share

thanks for that important tip

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Add to .net Arrays 1 Answer

Open With... a Build Unity program 3 Answers

How and where is serialized data stored? 1 Answer

Type or namespace not found 1 Answer

SpeechRecognitionEngine.InstalledRecognizers() returns null within Unity. 3 Answers


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