How can I detect device orientation change in a fast and reliable way?

I am using OnRectTransformDimensionsChange to detect between portrait and landscape. Basically I have something like this:

if (Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
    myAnimator.SetBool("isPortrait", true)
} else {
    myAnimator.SetBool("isPortrait", false);
}

However this code sometimes fails. If I do a GetBool on ‘isPortrait’, sometimes when it’s portrait the variable report that it’s not and vice-versa. To fix this, I did:

void Update() {
        if (Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
            if (myAnimator.GetBool("isPortrait") == false) {
                myAnimator.SetBool("isPortrait", true);
            }
        } else {
            if (MenuRetratilAnimator.GetBool("isPortrait") == true) {
                myAnimator.SetBool("isPortrait", false);
            }
        }
    }

The code above works fine, no issues. But it lags my experience on android. How can I change the user interface in a fast and reliable way depending on the screen orientation? Thank you.

Have you looked into DeviceOrientation?

void Update () {
if (Input.deviceOrientation==DeviceOrientation.Portrait||Input.deviceOrientation==DeviceOrientation.PortraitUpsideDown)
{
//codes for portrait
}
else
{
//codes for Landspace;
}

	}

use a slow running coroutine!