- Home /
Do reflective script calls compile away to pure code?
I'm referring to how Unity uses reflection to access methods and properties by providing string names of methods.
Obviously reflection is something you never never never want to do if optimization is your goal, which it IS is game development.
So it surprised me that Unity makes such extensive use of reflection for even the most basic things.
This leads me to wonder if reflection is just used in the script languages, but is then compiled down to pure, raw calls during build time?
I'm just wondering, because if it is, then I can get over my fear of reflection and use it begrudgingly in my scrips, knowing that in the end, it will be converted to actual raw method calls.
As I understand it methods like Send$$anonymous$$essage and GetComponent stay reflection based when compiled. So the general advice is not to use them unless you require the reflection behaviour.
In many cases its possible to cache the result of a reflection call and use that. This drops the performance cost dramatically.