I am looking for a way to easily add “Profiler.BeginSample()/Profiler.EndSample()” to methods i choose in my application. I really like the default behaviour of unity where the Monobehaviour methods automatically show up in the profiler without cluttering my code. I would like to have something similar for custom code, since we are mostly not using monobehaviours since most of our computations are tied up in systems that loop over plain c# objects.
I would prefer to have a simple [UnityProfiler] attribute that surrounds the method body with the begin and end calls, like some kind of code weaver or Aspect oriented programming approach.
Currently i am using “Profiler.BeginSample()/Profiler.EndSample()” in most of my code with these problems
- I have to be very careful to match up beginsample and endsample. return statements and coroutines can make it very hard to see problems here and a mistake is easily made. In case i make a mistake, there is no easy way to find the problem without doing a manual binary search over all my code. Since i am calling all my systems from a single monobehaviour run, this can easily take over half an hour to find a mismatch.
- I simply want to use BeginSample(“{classname}.{method}”), like unity is using for its defaultprofilers. I now have to manually keep this in sync.
- To be correct, i should add try/finally blocks for each of these sample blocks. I am currently not doing this as my code is already getting cluttered enough, but it is another problem in the back of my mind.
Does anyone know of a simpler/more reliable way, or some easy to use code manipulation tools or weaving mechanics for unity?
MH