Usage of Camera.main throughout the repository
See original GitHub issueUsing Camera.main
basically, does a FindByTag
on the scene and searches through all tagged objects to find the main camera. With more tagged objects the performance gets worse and worse. Instead, the reference should be cached in Awake/Start.
There are a lot of occurrences of Camera.main
in the repository that doesn’t do this and might potentially cause performance issues at some point or at least drain the performance unnecessarily. Some scripts call Camera.main
up to 8 times on every Update
.
There are functions that will have to be made obsolete like MathUtils.IsInFOV(position)
to include the camera reference in the call.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Camera.main is inefficient in frequently called methods
Unity support for both ReSharper and Rider. Contribute to JetBrains/resharper-unity development by creating an account on GitHub.
Read more >Camera.main - Scripting API
//Place this script on a GameObject to switch between the main Camera and your own second Camera on the press of the "L"...
Read more >Lighting changing depending on main camera
I tried to use the Post Processing Stack repository but i'm not sure wether fog is even supported, as stated in this thread: ......
Read more >Multiple Cameras and When to use them… [Unity3D]
If you have worked on unity and been overusing the Main Camera and thinking why ... Clone the repository in your desired folder...
Read more >Using more than one camera
... in the corner of the screen while the main view is first-person. You can set the size of a camera's onscreen rectangle...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t think the GazeManager would be a good place for it since it would require an instance of the GazeManager and make the utility scripts coupled to it. This should probably be standalone.
My first thought was that a project that changes the main camera often would probably implement their own system to handle this. But then I realized they would have to update the references in every single script, which will probably be quite a challenge to keep track of all of them in the scene and all the different ways they are cached (
GazeTransform
,TargetTransform
,mainCamera
,MainCamera
). Some scripts likeHandDraggable
even have private caches so this wouldn’t be entirely possible.I’m not sure what the best way would be to handle this.
If we just keep the references public, add an interface to at-least make this consistent? Some scripts cache the transform and some the camera.
Provide a cache version of the camera through an extension like for example
Camera.cachedMain
?