Unity获取鸿蒙系统的键盘高度与导航高度
·
在Unity的Plugins文件夹,放置HarmonyHelper.etslib文件,可以直接编译调用鸿蒙系统的API。
import { window } from '@kit.ArkUI';
import { promptAction } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
import { vibrator } from '@kit.SensorServiceKit';
const context = globalThis.context as common.UIAbilityContext;
export class HarmonyHelper {
static keyboardHeight : number = 0;
static navigationBarHeight: number = 0;
static init(): void {
window.getLastWindow(context).then((win: window.Window) => {
win.on('keyboardHeightChange', (height: number) => {
HarmonyHelper.keyboardHeight = height;
});
win.on('avoidAreaChange', (options: window.AvoidAreaOptions) => {
if (options.type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {
HarmonyHelper.navigationBarHeight = options.area.bottomRect.height;
}
});
win.setWindowLayoutFullScreen(true);
win.setWindowSystemBarEnable(['status', 'navigation']);
});
}
static getNavigationBarHeight(): number {
return HarmonyHelper.navigationBarHeight;
}
static getKeyboardHeight(): number {
return HarmonyHelper.keyboardHeight;
}
}
export function RegisterHarmonyHelper() {
const register: Record<string, Object> = {};
register["HarmonyHelper"] = HarmonyHelper;
return register;
}
在Unity中调用如下:
var harmonyHelperClass = new OpenHarmonyJSClass("HarmonyHelper");
harmonyHelperClass.CallStatic("init");
var keyboardHeight = harmonyHelperClass.CallStatic<float>("getKeyboardHeight");
var navegationHeight = harmonyHelperClass.CallStatic<float>("getNavigationBarHeight");
注意:Native代码返回的类型,如果C#用object赋值后,强转必须用与Native一致的类型,否则运行时得不到想要的结果——如:Android返回int,赋值给object,之后就不能将object强转为float,而必须是int——当然可以先转为int,再转为float。
更多推荐


所有评论(0)