UnityPlugins文件夹,放置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

Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐