Search Unity

Question How do I get the screen bounds/main camera from a job?

Discussion in 'Entity Component System' started by DacunaZuke, Jun 8, 2022.

  1. DacunaZuke

    DacunaZuke

    Joined:
    Apr 30, 2022
    Posts:
    10
    I have a camera that doesn't move, and I want objects that go off-screen to be disabled. For that, I need the screen bounds.

    Before ECS, I was using this function to retrieve and cache it:

    Code (CSharp):
    1. private static Rect GetScreenRect()
    2. {
    3.     Camera mainCamera = Camera.main;
    4.  
    5.     if (!mainCamera) return Rect.zero;
    6.  
    7.     Vector3 bottomLeft = mainCamera.ScreenToWorldPoint(Vector2.zero);
    8.     Vector3 topRight = mainCamera.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
    9.  
    10.     return Rect.MinMaxRect(
    11.         bottomLeft.x < 0 ? bottomLeft.x : -bottomLeft.x,
    12.         bottomLeft.y < 0 ? bottomLeft.y : -bottomLeft.y,
    13.         topRight.x > 0 ? topRight.x : -topRight.x,
    14.         topRight.y > 0 ? topRight.y : -topRight.y);
    15. }
    Unfortunately, now I get this:

    Burst error BC1016: The managed function `UnityEngine.Camera.get_main()` is not supported

    That makes sense, I guess, but how should I go about getting the screen bounds now?

    Edit: For more context, I am disabling the off-screen objects via an Entities.ForEach because it happens quite often.
     
  2. SebLazyWizard

    SebLazyWizard

    Joined:
    Jun 15, 2018
    Posts:
    233
    Just get your data as usual outside the Entities.ForEach and assign it to local variables, which then can be accessed from within Entities.ForEach.
     
    DacunaZuke likes this.
  3. DacunaZuke

    DacunaZuke

    Joined:
    Apr 30, 2022
    Posts:
    10
    Oh gosh, that was pretty obvious, I guess. Sorry, still wiring my brain for ECS/Jobs.

    Thank you!
     
    Last edited: Jun 9, 2022