How to get a list of all functions which were used in a target script?

Hi guys,

Does anyone know how to get a list of all functions which were used by a specific script?

For example this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyExampleScript : MonoBehaviour {

    List<string> stringList;

    void Start ()
    {
        stringList = new List<string>();

        Method1();
        Method2();

    }

    void TestMethod()
    {
        Debug.Log("Test");
    }

    void Method1()
    {

    }

    void Method2()
    {

    }

    void Method3()
    {

    }
}

And through some sort of function, I want it to output this:

Method1
Method2
Debug.Log

Any ideas how could this be done?

Thanks!

Only the ones that were used… Not that I’m aware of. I mean, that isn’t writing it to a log, of course. :slight_smile:

I would guess that you could. possibly with reflection if its compiled code.

the real issue is finding a reason why that would be a good idea, cause I’m certain there isn’t one. the very idea feels like it defys one of the fundamental principals of OO programming, encapsulation

1 Like

Script would run while in-editor rather than at runtime in games

Looked into it and this is something that reflection can’t do. there are certain libraries that allow you to see the method bodys, but thats about it.

you’d be better off opening the file in a stream, reading the text and manually parsing the file. And at that point you’re practically writing the grounds of an IDE. that will take a ton of work, so the real question remains, why would you want such a tool? current IDE’s are already your best tool for this.

But even this would only work if your type hierarchy was pretty flat. If you’ve got some class that calls protected methods of its super class then just parsing the text content of the file wouldn’t pick up on that.

So yeah, explaining the problem that’s trying to be solved would help…

I am working on an Editor Extension - Project Scan which scans for performance bottlenecks.

Currently, I am trying to look whether some scripts contain functions like Destroy() and Instantiate() and tell the user that it’s bad to use them and suggest object pooling instead.

That’s correct, I need to access a full call hierarchy.

The reflection looks into the metadata of a Class and therefore only looks at variables and functions that are defined within that class. While that information is good to know, it’s not enough.

But that’s not a universal truth that could be derived just from looking at the code without context and therefore wouldn’t be reliable advice.

What you’re trying to accomplish the Profiler already does; and does correctly by actually measuring the execution time of the code.

Now for things like looking for Debug.Log and saying “hey, remove these” then fine but that you could just do with string parsing.

1 Like

True, which is why if the user finds it irrelevant or they believe it doesn’t affect the performance in their case then they can Dismiss the notification and it would never appear again.

I am not measuring the performance of the game, but scanning user’s project and, based on various conditions (e.g. whether it’s a mobile project or not), give user suggestions on how performance could be improved. I am pretty sure Profiler doesn’t do that.

I am not making a Profiler-killer but rather an extension of utility tools which developer can use to gather additional information on their project.

You’re trying to do something like FindBugs, but for performance in Unity projects?

That’s probably something that fits into the compilation pipeline, maybe as a Visual Studio extension or something along those lines. The data you’re looking for exists in a more manageable form at compile time than if you’re looking at the code or trying to reflect.

As an example, Resharper for Visual Studio outputs the inverse of what you’re asking for - it’ll complain that TestMethod and Method3 are unused.

Something like that but it looks not just at scripts, but it also looks at Editor settings, in-scene GameObjects and other types of assets as well. Some results also link to various articles to explain the problem in more detail.

I have no problems when it comes to settings and gameobjects. The biggest challenge for me right now is gathering information from scripts like the one I described previously.

As you said before, Resharper has no problems finding whether methods are unused and it didn’t need to compile scripts.

Yes it does. Resharper uses its own compiler and keeps a compiled version of your code in memory. (Visual Studio does this too)