# SDK初始化

# 1、入口UIAbility初始化(必接)

TIP

游戏默认入口UIAbility代码位置为entry/src/main/ets/entryability/EntryAbility.ets;

Unity游戏默认入口UIAbility代码位置为entry/src/main/ets/ability/TuanjiePlayerAbility.ets;

在onWindowStageCreate生命周期方法调用DHSDKHelper.onWindowStageCreate方法,示例如下

import { DHSDKHelper } from 'dhsdkhelper'
    
class EntryAbility extends UIAbility {
   ...
       
   onWindowStageCreate(windowStage: window.WindowStage): void {
       //调用sdk的onWindowStageCreate方法
       DHSDKHelper.onWindowStageCreate(this.context,windowStage)
           
       windowStage.loadContent('pages/Index', (err) => {
      		if (err.code) {
       			 hilog.error(0x9527, 'DHUnion', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
       		 return;
      		}
      		hilog.info(0x9527, 'DHUnion', 'Succeeded in loading the content.');
    	});
   }
    
   ...
}   

# 2、启动页初始化(必接)

# 2.1 设置页面显示与隐藏

DANGER

游戏启动页默认不允许显示,需要在收到sdk的同意温馨提示回调后显示

代码示例如下:

//第一步:声明显示标识字段
@State visible: Visibility = Visibility.Hidden
//第二步:build的根元素设置属性visibility
build(){
    Column(){...}
	.visibility(this.visible)
}

# 2.2 初始化接口

DANGER

初始化成功后才允许调用其他接口,初始化成功后不允许反复调用初始化接口,游戏自行保存本次初始化结果,防止多次调用初始化

在aboutToAppear生命周期中调用DHSDKHelper.init初始化方法

代码示例如下:

import { DHRequestCode, DHResultCode, DHSDKHelper, DHUploadGameInfoType } from 'dhsdkhelper'


@Entry
@Component
struct Index {
    ...
    aboutToAppear(): void {
        /**
         * @param context    上下文
         * @param callback sdk回调
         */
        DHSDKHelper.init(getContext(this), (requestCode, resultCode, data) => {
          this.transferCallbackInfoAndShow(requestCode, resultCode, data)
          //研发收到温馨提示成功回调,代表用户已经同意了温馨提示,研发这时设置visible变量,可以把界面显示出来
          if (requestCode == DHRequestCode.REQ_AGREE_WENXIN_TIPS && resultCode == DHResultCode.OK) {
            this.visible = Visibility.Visible
          }
          switch (requestCode) {
              //初始化结果
              case DHRequestCode.REQ_INIT:
                  if (resultCode == DHResultCode.OK) {
                    //初始化成功
                  } else {
                    //初始化失败
                  }
                  break
               }
        })
    }
}
Last Updated: 2024/11/6 09:22:16