1

I have this Android plugin for Unity. Currently it's re-calculating a projection matrix for the camera once per frame and, while this works, it is obviously very wasteful. I'd like to be able to recalculate the matrix only when the screen orientation changes, as this is the only time it would need to be recalculated.

However, when I look online, all the answers I can seem to find always implement onConfigurationChanged or some such, which ultimately all require you to do the same thing: Extend the Activity class, which is something I can't really do, since there is no Activity. It's just a library that gets built, not a full application.

I know there's OrientationEventListener, but that fires every time the orientation of the device changes by even one degree, so that approach would still be very wasteful.

What I want to know is, is there a way to detect screen orientation changes like some kind of onConfigurationChanged event that doesn't require extending Activity to use?

Basically what I want to end up with is something like:

// Event fired only when the screen orientation changes
void onScreenOrientationChanged (ScreenOrientation orientation)
{
    recalculateMatrix(orientation);
}

Rather than what I have now which is more like:

// Method called once per frame
void Draw ()
{
    // Draw code
    ScreenOrientation orientation = getOrientation();
    recalculateMatrix(orientation);
}
DisturbedNeo
  • 713
  • 2
  • 5
  • 19

1 Answers1

1

As you mention, I think you can use OrientationEventListener implementing:

void onOrientationChanged (int orientation) {
   if (orientationIsChanged(orientation)) {
                      // recalculate matrix
   }
}

and you have to implement

boolean orientationIsChanged(int angle)

taking into consideration that: "orientation is 0 degrees when the device is oriented in its natural position, 90 degrees when its left side is at the top, 180 degrees when it is upside down, and 270 degrees when its right side is to the top. ORIENTATION_UNKNOWN is returned when the device is close to flat and the orientation cannot be determined."

chiarotto.alessandro
  • 1,491
  • 1
  • 13
  • 31
  • If this is the only way to do this, then thanks :) it kinda sucks though, you'd think Android and iOS would both have standard screen orientation change events that anyone can hook into at any time by now. – DisturbedNeo Mar 14 '17 at 14:16