Resizing minimap camera fails on first function call, but not following calls

I am attempting to create a map menu (essentially a minimap that you can open/close) that fits inside of a scroll image frame. In order to work on multiple resolution screens, I’m changing the minimap camera’s rect when it is enabled based on the size of the image it should fit inside. Here’s the script to do it:

public class MinimapCamera : MonoBehaviour {

    [SerializeField]
    RectTransform mapImageRect;
    Camera cam;
    MapGenerator mapGen;

    [SerializeField]
    Vector3 lowerLeftOffset;
    [SerializeField]
    Vector3 upperRightOffset;

    private void OnEnable()
    {
        if(!cam)
            cam = GetComponent<Camera>();

        if(!mapGen)
            mapGen = GameObject.FindGameObjectWithTag("Map Generator").GetComponent<MapGenerator>();

        SetCameraRect();
    }

    void SetCameraRect()
    {
        cam.aspect = mapGen.width / mapGen.height;
        cam.orthographicSize = (mapGen.width * mapGen.areaSize + 5) / 2;

        Vector3[] mapCorners = new Vector3[4];
        mapImageRect.GetWorldCorners(mapCorners);
        Vector3[] mapCornersNormalized = new Vector3[4];

        for (int i = 0; i < 4; i++)
        {
            mapCornersNormalized _= Camera.main.WorldToViewportPoint(mapCorners*);*_

}

Rect newCamRect = new Rect(mapCornersNormalized[0] + lowerLeftOffset, mapCornersNormalized[2] - upperRightOffset - mapCornersNormalized[0]);
cam.rect = newCamRect;
}
}

The gameobject this script is attached to is enabled at the same time that the object with the RectTransform it’s using for sizing is enabled, when you click on a button. The other object has an AspectRatioFitter on it that resizes it.
The script works properly the second time you click the button to open the map, but it seems to inaccurately resize the camera the first time the OnEnable() function runs.
I tried calling SetCameraRect() twice in a row within OnEnable(), but it did not fix the issue. I suspect the issue is related to the script order of execution (for example, the mapImageRect is not enabled before this script runs), but the order of execution documentation has been unhelpful in solving the problem. Setting the MinimapCamera to run either before or after the Default Time has no effect.
Thanks for your help!

Hope this helps. So if you are calling this code from a button call. Could you instead of using OnEnable. Define your own function to do this and call it from the call. such as from another script so when you hit a key like if


psudeocode


if (input.getkey(‘m’)){ MinimapCamera.turncameraon();}


Or if your calling this from the editor or a gui button, then make sure the function is public and drag and drop it into the onclick int the editor and save as prefab. Or assign it to OnClick via script.

Solved, but in a pretty ugly way. If someone has a better solution I’d still love to hear it.

I added a new script to the mapImageRect object that uses UIBehavior.OnRectTransformDimensionsChange() to call SetCameraRect() on my MinimapCamera. It works, but I’m not a fan of needing to pass references between the two objects to both objects.

Edit: And really a big thank you to my wife for suggesting that some kind of callback should exist for when the dimensions of the RectTransform change. She deserves the credit for solving it.