- Home /
Screen orientation change notification
Hello,
Is is possible for my script to be notified at the moment the device changes orientation? I'd rather not keep testing for device orientation on each frame... Maybe there's something like the delegate protocols in Obj-C, but no amount of searching or looking through the docs has helped.
Any help much appreciated!
Answer by franklynw · Jan 09, 2012 at 12:21 PM
I figured it out using UnitySendMessage - in Xcode, in "AppController.mm", in -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation I added:
NSString *theOrientation = [NSString stringWithFormat:@"%i", interfaceOrientation];
UnitySendMessage("Game Manager", "DeviceRotated", [theOrientation UTF8String]);
and in Unity, in the script on my Game Manager gameObject I added -
public void DeviceRotated(string theMessage) {
// do something here which needs to be done when device orientation changes
}
Being new to Unity I hadn't realised there could be 2-way communication between Unity & the Obj-c stuff. Probably elementary to most people here, but I thought I'd post it in case it helps someone avoid the troubles I had.
I guess I'm newer to Unity, because I don't understand that. You wrote "in Xcode...", but I thought you could only code in C# or unityscript. Where does that code go?
Not the best solution, if you can avoid editing your xCode project you should, Because a new build won't have this included.
You can always edit your initial AppController.mm, and every new project will have your lines included, well.. at least until you upgrade Unity with new version.
/Applications/Unity/Unity.app/Contents/PlaybackEngines/iPhonePlayer/iPhone-Trampoline/Classes/AppController.mm
Your answer
Follow this Question
Related Questions
Android Game Orientation cannot be fixed to potrait 0 Answers
How to allow automatic screen rotation in iOS? 2 Answers
Orientation Problem 1 Answer
How to test Screen Orientation? 3 Answers