SDK提供了內置的虛擬手柄供業務方使用,方便業務方快速調試游戲。
內置虛擬手柄配置及啟用方法
1.手柄庫依賴
1.1 將joystick-uikit包添加到項目依賴中。
1.2 在build.gradle文件中添加該aar包依賴
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation files('libs/joystick-uikit.aar')
2.初始化手柄庫
在啟動游戲前(最好是啟動完應用時)調用如下方法:GamePad.defaultInit()
注:默認手柄會添加到傳入SDK的containerView上,containerView是通過如下方法傳入到SDK的
ACGGamePaasService.getInstance().start(this, containerView);
3.啟動游戲時設置參數enableCustomInputEvent
CGGamePrepareObj prepareObj = new CGGamePrepareObj();
prepareObj.token = "用戶token";
prepareObj.userId = "用戶userid";
prepareObj.mixGameId = "要啟動游戲的id";
prepareObj.enableCustomInputEvent = false; //注:可以不設置,該參數默認為false
自定義虛擬手柄
1.添加自定義手柄View
在游戲頁的布局文件中添加自定義手柄的view,示例如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btn_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X"
/>
</LinearLayout>
</RelativeLayout>
2.事件傳入SDK
調用ACGGamePaasService.getInstance().customGamepadEvent(eventObj),將自定義事件傳入SDK,示例如下:
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction() || MotionEvent.ACTION_UP == event.getAction()) {
CGCustomGamepadEventObj gameEvent = new CGCustomGamepadEventObj();
gameEvent.playerIndex = mStickIdx;
gameEvent.action = event.getAction();
gameEvent.event = (int) v.getTag();
ACGGamePaasService.getInstance().customGamepadEvent(gameEvent);
}
return false;
}
});
3.調用customGamepadEvent方法所需的對象
public class CGCustomGamepadEventObj implements Serializable {
public int playerIndex;//手柄index
public int event;//手柄按鍵值
public int action;//手柄事件(按下、抬起、移動)
public int xValue;//搖桿或LT/RT傳入的x值
public int yValue;//搖桿或LT/RT傳入的y值
}
4.自定義手柄事件的按鍵值及按鍵事件
public class CGGamePadInputAction {
public static final int CG_GAMEPAD_BUTTON_DOWN = 0; //手柄按鈕按下
public static final int CG_GAMEPAD_BUTTON_UP = 1; //手柄按鈕抬起
public static final int CG_GAMEPAD_MOVE = 2; //搖桿移動
}
public class CGGamePadInputEvent {
public static final int CG_GAMEPAD_BUTTON_X = 0x88; //手柄X按鈕,手柄圖索引(1)
public static final int CG_GAMEPAD_BUTTON_A = 0x89; //手柄A按鈕,手柄圖索引(2)
public static final int CG_GAMEPAD_BUTTON_B = 0x8A; //手柄B按鈕,手柄圖索引(3)
public static final int CG_GAMEPAD_BUTTON_Y = 0x8B; //手柄Y按鈕,手柄圖索引(4)
public static final int CG_GAMEPAD_LEFT_BUMPER = 0x8C; //手柄LB,手柄圖索引(5)
public static final int CG_GAMEPAD_RIGHT_BUMPER = 0x8D; //手柄RB,手柄圖索引(6)
public static final int CG_GAMEPAD_LEFT_TRIGGER = 0x8E; //手柄LT,手柄圖索引(7);對應xValue從0到255,初始狀態為0
public static final int CG_GAMEPAD_RIGHT_TRIGGER = 0x8F; //手柄RT,手柄圖索引(8);對應yValue從0到255,初始狀態為0
public static final int CG_GAMEPAD_BACK = 0x97; //手柄back,手柄圖索引(9)
public static final int CG_GAMEPAD_START = 0x98; //手柄start,手柄圖索引(10)
public static final int CG_GAMEPAD_LEFT_STICK_BUTTON = 0x99; //手柄左側搖桿按鍵事件,手柄圖索引(11)
public static final int CG_GAMEPAD_LEFT_STICK = 0xB8; //手柄左側搖桿搖桿事件,手柄圖索引(11);對應xValue從0到255,yValue從0到255,初始狀態(127,127)
public static final int CG_GAMEPAD_RIGHT_STICK_BUTTON = 0x9A; //手柄右側搖桿按鍵事件,手柄圖索引(12)
public static final int CG_GAMEPAD_RIGHT_STICK = 0xB9; //手柄右側搖桿搖桿事件,手柄圖索引(12);對應xValue從0到255,yValue從0到255,初始狀態(127,127)
public static final int CG_GAMEPAD_DIRECTPAD_LEFT = 0x9B; //手柄方向鍵左按鈕,手柄圖索引(13)
public static final int CG_GAMEPAD_DIRECTPAD_UP = 0x9C; //手柄方向鍵上按鈕,手柄圖索引(13)
public static final int CG_GAMEPAD_DIRECTPAD_RIGHT = 0x9D; //手柄方向鍵右按鈕,手柄圖索引(13)
public static final int CG_GAMEPAD_DIRECTPAD_DOWN = 0x9E; //手柄方向鍵下按鈕,手柄圖索引(13)
}