UE4

UE4 Android Screen Orientation

밤토리세상 2021. 7. 5. 18:01

https://answers.unrealengine.com/questions/789383/how-do-i-switch-from-a-landscape-widget-to-portrai.html?sort=oldest 

 

how do I switch from a landscape widget to portrait via Blueprint - UE4 AnswerHub

Hello, if you can help me? I have two widgets, 1 (Main Menu), 2 (Registration Screen). I want to set the main menu as: LANDSCAPE and the Registration screen as: PORTRAIT I need to know how to do this, it's very important to me, Any help will be welcome. Th

answers.unrealengine.com

This is going to be a little bit complicated as unreal engine 4 does not support orientation change via blueprint by default. You will need the Mobile Utils plugin by gameDNA

NOTE - This code is not perfect. I have been trying to extend android functionality using this plugin however the code is not refined. Follow these instructions to the last letter. I am not that good with C++ yet, you might have to change some code according to your need here and there.

This was done in quite a rush and I never got back to it.

  1. Find the file MobileUtils_APL in that and add this code -
    1. public boolean AndroidThunkJava_ToggleOrientation()
    2. {
    3. boolean result=false;
    4. int orientation=getResources().getConfiguration().orientation;
    5. if(orientation==Configuration.ORIENTATION_PORTRAIT){
    6. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    7. result=true;
    8. }
    9. else if(orientation==Configuration.ORIENTATION_LANDSCAPE)
    10. {
    11. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    12. result=false;
    13. }
    14. return result;
    15. }
    16.  
  2. in Plugins\MobileUtils-master\Source\MobileUtils\Classes\Interfaces\MobileUtilsInterface.h - add this
  3. virtual bool ToggleOrientation() = 0;
  4. in Plugins\MobileUtils-master\Source\MobileUtils\Classes\MobileUtilsBlueprintLibrary.h
    1. UFUNCTION(BlueprintCallable, Category = MobileUtils)
    2. static bool ToggleOrientation();
    3.  
  5. In Plugins\MobileUtils-master\Source\MobileUtils\Private\Android\MobileUtilsPlatform.cpp

First Add this -

  1. jmethodID FMobileUtilsPlatform::ToggleOrientationMethod;
  2.  

Then inside the constructor -

  1. FMobileUtilsPlatform::FMobileUtilsPlatform()
  2. {
  3. if (JNIEnv* Env = FAndroidApplication::GetJavaEnv())
  4. {
  5. ToggleOrientationMethod = FJavaWrapper::FindMethod(Env, FJavaWrapper::GameActivityClassID, "AndroidThunkJava_ToggleOrientation", "()Z", false);
  6. }
  7. }
  8.  

Then the method implementatio -

  1. bool FMobileUtilsPlatform::ToggleOrientation()
  2. {
  3. bool tResult = false;
  4. if (JNIEnv* Env = FAndroidApplication::GetJavaEnv())
  5. {
  6. tResult = FJavaWrapper::CallBooleanMethod(Env, FJavaWrapper::GameActivityThis, FMobileUtilsPlatform::ToggleOrientationMethod);
  7. }
  8. return tResult;
  9. }
  10.  
  1. Now go to Plugins\MobileUtils-master\Source\MobileUtils\Private\Android\MobileUtilsPlatform.h

add the signatures of the method -

virtual bool ToggleOrientation() override;

static jmethodID ToggleOrientationMethod;

  1. Now in Plugins\MobileUtils-master\Source\MobileUtils\Private\MobileUtilsBlueprintLibrary.cpp
  2. bool UMobileUtilsBlueprintLibrary::ToggleOrientation() { #if PLATFORM_ANDROID || PLATFORM_IOS return IMobileUtils::Get().GetPlatformInterface()->ToggleOrientation(); #else return false; #endif }

Save and compile.

Now in blueprint you should have a method called "Toggle Orientation" if everything is done right -

 

 

  1. import android.net.ConnectivityManager;
  2. import android.net.NetworkInfo;
  3. import com.google.android.gms.common.ConnectionResult;
  4. import android.telephony.TelephonyManager;
  5. import android.provider.Settings.Secure;
  6. import android.net.wifi.WifiManager;
  7. import java.security.MessageDigest;
  8. import java.security.NoSuchAlgorithmException;
  9. import android.content.pm.ActivityInfo;

유틸 깃허브

https://github.com/gameDNAstudio/MobileUtils

 

gameDNAstudio/MobileUtils

A plugin for Unreal Engine 4 that lets you integrate mobile utilities into your project. - gameDNAstudio/MobileUtils

github.com

 

4.23.0 버전이 마지막이라 실행시 경고가 떠서 

플러그인 파일 수정

MobileUtils.uplugin

 

버전을 4.26.0 으로 변경함 

4.23.0 이상이면 정상동작 하는듯