Search Unity

Iterate through all cameras - possible?

Discussion in 'General Discussion' started by dogmachris, Oct 18, 2019.

  1. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,375
    Hey everyone, I'm curious about executing code for each camera in the scene, including the scene camera.

    For objects that only need to execute code for the camera I use OnWillRenderObject(), but that will only consider cameras that will render the object in question. Is there a way to iterate through EVERY camera, whether they're rendering an object or not? Is that even possible?
     
  2. I have never tried this, but I would begin something like this:
    https://docs.unity3d.com/ScriptReference/Resources.FindObjectsOfTypeAll.html search for all camera. Probably the scene camera isn't among those.
    You can gain access to the SceneView camera through this: https://docs.unity3d.com/ScriptReference/SceneView-camera.html

    Just add this to the list. But keep in mind that you can't change the camera settings directly through this reference.
     
  3. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    SRP also allow you to do this
     
  4. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I'm confused, are you trying to run something from the camera, or iterate over the cameras from somewhere else? You could just add a component to each camera, that way every active camera will call Update on your object.

    If you want to do some iteration from another class, you could just store references to each camera in a static list. I have a number of places where I do that. In a camera object's constructor, push it onto a list, when it's destroyed, pop it off. Then from any place in the code, you can just iterate through the list without any performance hits trying to search.
     
  5. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,375
    Creating an array of cameras was an idea too, however I don't think, that's the issue. The issue is that that alone won't help me execute code for every camera individually before they render. If I do it in Update() everything will be done before the first camera even starts rendering - I need to do stuff between each camera rendering. OnWillRenderObject() seems to be the only event that will allow me to do that, but it's selective - pity. :S
     
  6. Have you tried this?
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnPreRender.html

    I don't think it is suitable for the SceneCamera though.
     
  7. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,375
    Take this as an example: I have a cube. And I want to move that cube to the middle of the screen of every camera. I can't use multiple cubes, it must be that one. OnWillRenderObject() allows me to do that, but it only works as long as each camera is rendering the cube anyway, I need every camera to render it at the exact predefined position regardless of whether it sees it or not. I can't do that anywhere in update, can I?

    P.S.: My script would be on the cube, not on every camera.
     
  8. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,375
  9. Yes, you need to attach it to every camera you have.

    I think more detailed handling is only available in SRPs. Where you can inject anything you like to the render process.
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    FindObjectsOfType<Camera>() will work.

    Its not super fast, so I would suggest caching the results if you want to use it frequently.

    Also note that at editor time this will find scene cameras as well. You may want to use Camera.cameraType to distinguish between different cameras.
     
  11. Unfortunately it is not what he's looking for. Later he stated that he needs the cameras one by one right before they render and not in a big list at some point in the update loop. He wants to move one object in the front of the camera right before rendering. The same one for every camera.
     
  12. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618