question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage of Camera.main throughout the repository

See original GitHub issue

Using 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:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Zod-commented, Aug 12, 2017

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.

1reaction
Zod-commented, Aug 12, 2017

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 like HandDraggable 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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found