x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Dynamically Change Screen Orientation on Android

sorry for my poor English.
In my game I want to achieve a function that can Dynamically change screen orientation.
for example:I want switch screen orientation to landscape when player move into some room and switch it back to Portrait when player leave those room. How can I Do this?

Product Version: UE 4.15
Tags:
more ▼

asked Jun 10 '17 at 02:33 AM in Using UE4

avatar image

Jack Myth
149 10 16 24

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

I Find the answer by myself.
It use JNI to call Java method "setRequestedOrientation" to done this.
Here is the Code:

 #include "Android/AndroidJNI.h"  
 #include "Android/AndroidApplication.h"  

 JNIEnv* JE = FAndroidApplication::GetJavaEnv();  
 jmethodID SetOrientationID = FJavaWrapper::FindMethod(JE, FJavaWrapper::GameActivityClassID, "setRequestedOrientation", "(I)V", false);  
 FJavaWrapper::CallVoidMethod(JE, FJavaWrapper::GameActivityThis, SetOrientationID,0);  

more ▼

answered Jun 19 '17 at 08:53 AM

avatar image

Jack Myth
149 10 16 24

avatar image Yvasquez Jan 14 '19 at 08:25 PM

Hi. I was investigating how to do what you mention. I see you did it. I only have one doubt? You do this in a new empty class or where you implement the code that you left here. Thank you and sorry for the inconvenience

avatar image Jack Myth Jan 14 '19 at 08:46 PM

For myself,I have an BPFuncLib class, It store some function and can be called in blueprint.The function is very simple,so you can just make a static function in your utility class and add them into it. call it directly, it doesn't need any context because the GameActivity will always valid.
By the way: There have a paramter that control the target orientation that you want to change to, typically it use a variable like SCREEN_ORIENTATION_LANDSCAPE,but for convenience I use it's value directly ,you can find the value of them in AndroidSDK and make them to an enum, It will make you fell better than use a magic number.
And, AndroidJNI.h may not valid for windows, add a macro like #if defined(PLATFORM_ANDROID)&&PLATFORM_ANDROID or your code may not be able to compile for editor.

avatar image Yvasquez Jan 14 '19 at 09:08 PM

muchas gracias por tu pronta respuesta. Una pregunta mas. De donde has bajado "AndroidJNI.h"?

avatar image Jack Myth Jan 14 '19 at 09:19 PM

It's provided by Unreal Engine, add Launch module dependence will allow you use it.

avatar image Yvasquez Jan 14 '19 at 09:36 PM

I think I'm not doing my class well as you indicated. what I did was make a new class where I define a function ToggleOrientation (). My cpp is composed as follows.

 #include "funcLib.h"
 #include "Android / AndroidJNI.h"
 #include "Android / AndroidApplication.h"
 .......
 void AfuncLib :: ToggleOrientation ()
 {
 #if PLATFORM_ANDROID || PLATFORM_IOS
         JNIEnv * JE = FAndroidApplication :: GetJavaEnv ();
 jmethodID SetOrientationID = FJavaWrapper :: FindMethod (JE, FJavaWrapper :: GameActivityClassID, "setRequestedOrientation", "(I) V", false);
 FJavaWrapper :: CallVoidMethod (JE, FJavaWrapper :: GameActivityThis, SetOrientationID, 0);
 #else
 #endif
 }
 

Where it says 'Android / AndroidJNI.h': No such file or directory

Again, thanks for the help. Sorry for the inconvenience

avatar image Jack Myth Jan 14 '19 at 09:44 PM

open your build.cs, add "Launch" to PublicDependencyModuleNames. It's not about C++, It's use for UBT. Read some docs you will learn more about it.
https://docs.unrealengine.com/en-us/Programming/BuildTools
And,#include should also inside of #if PLATFORM_ANDROID macro.

avatar image Yvasquez Jan 14 '19 at 10:06 PM

Thanks for all the help Jack Myth. moved the #include "Android / AndroidJNI.h" #include "Android / AndroidApplication.h" in the #if PLATFORM_ANDROID || PLATFORM_IOS as you told me and in my builds.cs add what you indicated to me.

 PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "Launch","InputCore" });
 

For some reason I still can not compile apparently when adding "Launch" to the build.cs an error that says that it fails to produce DebugGame.dll. If I remove the "Launch" I can compile in visual but when I try to build for android from the engine it tells me that Android / AndroidJNI.h does not fount

avatar image Jack Myth Jan 14 '19 at 10:22 PM

Launch module is not valid for windows, if you add this you will failed to compile for windows. Only add the dependency for android by using "if(Target.IsInPlatformGroup(UnrealPlatformGroup.Android))" in build.cs.

avatar image Jack Myth Jan 14 '19 at 10:31 PM

All in all, those code and module should always be ignored for all platforms except Android. Because they are platform-related. No other platform use AndroidJNI.

avatar image Yvasquez Jan 14 '19 at 10:33 PM

Thank you, visual and let me compile doing what you told me. but now the engine when wanting to compile for android generates several errors that say the same thing. D: / Program Files / Epic Games / UE_4.20 / Engine / Source / Runtime / Launch / Public \ Android / AndroidJNI.h (37,19): error: static data member 'AndroidThunkJava_KeepScreenOn' not allowed in local class 'FJavaWrapper' alt text

Thanks again for all the help

captura.png (137.6 kB)
avatar image Jack Myth Jan 14 '19 at 10:41 PM

I guess your cpp have.some mistake. Did You Put #include Inside Of the Defination?

avatar image Yvasquez Jan 14 '19 at 10:43 PM

Yes. My cpp

     #include "funcLib.h"
     
     void AfuncLib::ToggleOrientation()
     {
     #if PLATFORM_ANDROID || PLATFORM_IOS 
     #include "Android/AndroidJNI.h"
     #include "Android/AndroidApplication.h"
     
         JNIEnv* JE = FAndroidApplication::GetJavaEnv();
         jmethodID SetOrientationID = FJavaWrapper::FindMethod(JE, FJavaWrapper::GameActivityClassID, "setRequestedOrientation", "(I)V", false);
         FJavaWrapper::CallVoidMethod(JE, FJavaWrapper::GameActivityThis, SetOrientationID, 0);
     #else 
         
     #endif 
     }

avatar image Jack Myth Jan 14 '19 at 10:45 PM
  #if PLATFORM_ANDROID || PLATFORM_IOS 
  #include "Android/AndroidJNI.h"
  #include "Android/AndroidApplication.h"
 #endif
 void AfuncLib::ToggleOrientation()
  {
  #if PLATFORM_ANDROID || PLATFORM_IOS 
      JNIEnv* JE = FAndroidApplication::GetJavaEnv();
      jmethodID SetOrientationID = FJavaWrapper::FindMethod(JE, FJavaWrapper::GameActivityClassID, "setRequestedOrientation", "(I)V", false);
      FJavaWrapper::CallVoidMethod(JE, FJavaWrapper::GameActivityThis, SetOrientationID, 0);
  #else 
      
  #endif 
  }

avatar image Yvasquez Jan 14 '19 at 10:34 PM

My build.cs public Math_Maze(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

         PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
 
         if (Target.Platform == UnrealTargetPlatform.IOS)
         {
             PublicDependencyModuleNames.AddRange(new string[] { "Launch" });
         }
         else if (Target.Platform == UnrealTargetPlatform.Android)
         {
             PublicDependencyModuleNames.AddRange(new string[] { "Launch" });
         }
 
 
             PrivateDependencyModuleNames.AddRange(new string[] {  });
 
     
 
         // Uncomment if you are using Slate UI
         // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
 
         // Uncomment if you are using online features
         // PrivateDependencyModuleNames.Add("OnlineSubsystem");
 
         // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
     }
avatar image Yvasquez Jan 14 '19 at 11:05 PM

thank you. It already compiles without problems. A thousand thanks for all the help

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Hello JackTM,

You should set the camera aspect ratio. If your screen ratio is 16:9 then camera ratio 0.5625 for landscape and 1.7777 for portrait. So, PortraitRatio= ScreenHeight / ScreenWidth and LandscapeRatio= ScreenWidth/ScreenHeight.

alt text

ratio.png (11.5 kB)
more ▼

answered Jun 10 '17 at 07:50 AM

avatar image

Sertac Ogan
1.5k 30 11 46

avatar image Jack Myth Jun 11 '17 at 01:58 AM

Sorry For my late reply but it is not what I need.
This setting can not change the really screen Orientation.It can only give me this result:
alt textalt text
the screen Orientation is still portrait.

0.png (2.2 kB)
1.jpg (9.2 kB)
(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question