Unity开发iOS/Android应用:固定设备方向切换键盘朝向
实现方案
一、Unity 侧基础设置
- 固定设备横屏:在
Player Settings中,将Default Orientation设置为Landscape Left或Landscape Right,取消其他方向勾选,确保设备始终保持横屏状态。 - 模拟竖屏UI:将竖屏界面的父物体通过
RectTransform的Rotation属性旋转90°(如(0, 0, 90)),调整UI锚点与适配规则,保证布局正常。 - 文本输入适配:让
InputField跟随竖屏UI父物体旋转,输入框文本会自动适配旋转后的布局,无需额外修改文本方向。
二、iOS 端原生脚本实现(Xcode 修改)
1. 编写原生插件代码
在Xcode工程的Classes目录下创建KeyboardOrientationHelper.m文件,添加以下代码:
#import <UIKit/UIKit.h> #import "UnityViewController.h" @implementation KeyboardOrientationHelper extern "C" void _SetKeyboardOrientation(int orientation) { dispatch_async(dispatch_get_main_queue(), ^{ UnityViewController* unityVC = (UnityViewController*)UnityGetGLViewController(); UIInterfaceOrientation targetOrientation; switch (orientation) { case 0: // 竖屏 targetOrientation = UIInterfaceOrientationPortrait; break; case 1: // 横屏左 targetOrientation = UIInterfaceOrientationLandscapeLeft; break; case 2: // 横屏右 targetOrientation = UIInterfaceOrientationLandscapeRight; break; default: targetOrientation = UIInterfaceOrientationPortrait; break; } [unityVC setInterfaceOrientation:targetOrientation]; // 重新触发键盘布局 UITextField* activeField = [UIResponder currentFirstResponder]; if (activeField) { [activeField resignFirstResponder]; [activeField becomeFirstResponder]; } }); } @end
2. Unity 侧调用原生方法
在Unity中创建C#脚本,通过DllImport调用原生方法:
using System.Runtime.InteropServices; using UnityEngine; public class KeyboardController : MonoBehaviour { #if UNITY_IOS && !UNITY_EDITOR [DllImport("__Internal")] private static extern void _SetKeyboardOrientation(int orientation); #endif // 切换键盘朝向:0=竖屏,1=横屏左,2=横屏右 public void SetKeyboardOrientation(int orientation) { #if UNITY_IOS && !UNITY_EDITOR _SetKeyboardOrientation(orientation); #endif } }
3. Xcode 工程配置
- 将
KeyboardOrientationHelper.m添加到Xcode工程中。 - 在
Info.plist中仅勾选横屏方向(Landscape Left/Landscape Right),避免系统自动旋转。
三、Android 端原生脚本实现
1. 编写原生插件代码
在Unity项目的Assets/Plugins/Android目录下创建KeyboardOrientationHelper.java文件(替换com.yourpackage为你的实际包名):
import android.app.Activity; import android.view.inputmethod.InputMethodManager; import android.content.Context; import android.view.WindowManager; public class KeyboardOrientationHelper { private static Activity unityActivity; static { unityActivity = com.unity3d.player.UnityPlayer.currentActivity; } // 切换键盘朝向:0=竖屏,1=横屏左,2=横屏右 public static void SetKeyboardOrientation(int orientation) { unityActivity.runOnUiThread(new Runnable() { @Override public void run() { WindowManager.LayoutParams params = unityActivity.getWindow().getAttributes(); switch (orientation) { case 0: // 竖屏 params.screenOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case 1: // 横屏左 params.screenOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case 2: // 横屏右 params.screenOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; } unityActivity.getWindow().setAttributes(params); // 重新唤起键盘生效 InputMethodManager imm = (InputMethodManager) unityActivity.getSystemService(Context.INPUT_METHOD_SERVICE); android.view.View activeView = unityActivity.getCurrentFocus(); if (activeView != null) { imm.hideSoftInputFromWindow(activeView.getWindowToken(), 0); imm.showSoftInput(activeView, InputMethodManager.SHOW_IMPLICIT); } } }); } }
2. Unity 侧调用原生方法
在Unity的C#脚本中添加以下代码:
using UnityEngine; public class KeyboardController : MonoBehaviour { #if UNITY_ANDROID && !UNITY_EDITOR private AndroidJavaClass keyboardHelper; #endif void Awake() { #if UNITY_ANDROID && !UNITY_EDITOR keyboardHelper = new AndroidJavaClass("com.yourpackage.KeyboardOrientationHelper"); #endif } // 切换键盘朝向:0=竖屏,1=横屏左,2=横屏右 public void SetKeyboardOrientation(int orientation) { #if UNITY_ANDROID && !UNITY_EDITOR keyboardHelper.CallStatic("SetKeyboardOrientation", orientation); #endif } }
3. Android 配置注意事项
- 在
AndroidManifest.xml中,将UnityPlayerActivity的screenOrientation设置为landscape:<activity android:name="com.unity3d.player.UnityPlayerActivity" android:screenOrientation="landscape"> - 确保插件包名与
AndroidManifest.xml中的包名一致。
四、使用流程
- 在需要切换键盘朝向的场景中挂载
KeyboardController脚本。 - 弹出竖屏键盘时调用
SetKeyboardOrientation(0);弹出横屏键盘时调用SetKeyboardOrientation(1)或2。 - 调用方法前确保
InputField已获得焦点,或调用后重新让InputField获取焦点,保证键盘方向生效。
内容的提问来源于stack exchange,提问作者Nicholas Horsma
相关产品推荐
相关产品推荐

