8.虚拟键盘实现 概述: 硬键盘就是物理键盘,平时敲的那种。软键盘是虚拟的键盘,不是在键盘上,而是在"屏幕"上。虚拟按键就是虚拟键盘的一部分,根据功能需求,提供部分按键效果的UI可操控按钮,尤其是在平板电脑、触控手机上,虚拟按键得到了更多的应用。在触控平台上的游戏,一部分通过手指在屏幕上相应的滑动产生相应的指令;还有一部分增设虚拟按键,完成对游戏的操控。 实现原理: UI基本制作->功能实现 实现方法: 步骤1: 按照7UI的制作方法,完成UI的制作,最终效果图,如8-1-1所示。左侧为摇杆按钮,为双侧的BUTTON,一层做摇杆的底部按钮背景,一层与用户交互发生相应事件。右侧围控制攻击和跳跃的按钮。
图8-1-1
步骤2: 脚本实现按钮功能。 初始化虚拟按键UI。
04 | GUI.RegisterLayout(_attackWindowName, @"Layout\AttackButton.layout" , false , true ); |
06 | GUI.UIWidget.SetEventMouseButtonClick(_attackWindowName, "AttackBtn" , OnAttackBtnClick, EventControl.Add); |
09 | GUI.RegisterLayout(_jumpWindowName, @"Layout\JumpButton.layout" , false , true ); |
11 | GUI.UIWidget.SetEventMouseButtonClick(_jumpWindowName, "JumpBtn" , OnJumpBtnClick, EventControl.Add); |
15 | IntSize screenSize = GUI.GetScreenSize(); |
16 | IntPoint AtkBtnUIpos = new IntPoint(( int )(screenSize.width - GUI.UIWidget.GetSize(_attackWindowName, "_Main" ).width * 2.5f), |
17 | ( int )(screenSize.height - GUI.UIWidget.GetSize(_attackWindowName, "_Main" ).height * 1.2f)); |
19 | IntPoint JumpBtnUIpos = new IntPoint(( int )(screenSize.width - GUI.UIWidget.GetSize(_attackWindowName, "_Main" ).width * 1.2f), |
20 | ( int )(screenSize.height - GUI.UIWidget.GetSize(_attackWindowName, "_Main" ).height * 1.2f)); |
21 | GUI.UIWidget.SetPosition(_attackWindowName, "_Main" , AtkBtnUIpos); |
22 | GUI.UIWidget.SetPosition(_jumpWindowName, "_Main" , JumpBtnUIpos); |
26 | private void OnAttackBtnClick(FString sender) |
28 | Logic.IputMgr.Instance.Attack_N(Code.J); |
31 | private void OnJumpBtnClick(FString sender) |
33 | Logic.IputMgr.Instance.Jump(); |
虚拟摇杆类
01 | public class AndroidJoystick |
05 | GUI.RegisterLayout(_joystickWindowName, @"Layout\JoyStickBtn.layout" , false , true ); |
07 | GUI.UIWidget.SetEventMouseButtonPressed(_joystickWindowName, "JoystickBtn" , OnJSBtnPressed, EventControl.Add); |
09 | GUI.UIWidget.SetEventMouseButtonReleased(_joystickWindowName, "JoystickBtn" , OnJSBtnReleased, EventControl.Add); |
11 | GUI.UIWidget.SetEventMouseDrag(_joystickWindowName, "JoystickBtn" , OnJSBtnDrag, EventControl.Add); |
13 | GUI.UIWidget.SetEventTick(_joystickWindowName, "JoystickBtn" , Tick, EventControl.Add); |
14 | IntSize screenSize = GUI.GetScreenSize(); |
15 | IntPoint JoystickBtnUIpos = new IntPoint(( int )(GUI.UIWidget.GetSize(_joystickWindowName, "_Main" ).width * 0.2f), |
16 | ( int )(screenSize.height - GUI.UIWidget.GetSize(_joystickWindowName, "_Main" ).height * 1.5f)); |
17 | GUI.UIWidget.SetPosition(_joystickWindowName, "_Main" , JoystickBtnUIpos); |
18 | GUI.SetLayoutVisible(_joystickWindowName, true ); |
19 | jsBtnPos = GUI.UIWidget.GetPosition(_joystickWindowName, "JoystickBtn" ); |
22 | private void OnJSBtnDrag(FString sender, int value, int value2, MouseButton btn) |
29 | int xOffset = value - lastX; |
32 | Debug.Dbgout( "IsRightSlide" ); |
33 | IntPoint JoystickBtnUIpos = new IntPoint(150, jsBtnPos.top); |
35 | GUI.UIWidget.SetPosition(_joystickWindowName, "JoystickBtn" , JoystickBtnUIpos); |
36 | Logic.LogicMgr.Instance.SetDirection(Logic.Direction.Forward); |
37 | Logic.LogicMgr.Instance.CurPlayer.State = Logic.CharacterState.Running; |
41 | Debug.Dbgout( "IsLeftSlide" ); |
42 | IntPoint JoystickBtnUIpos = new IntPoint(0, jsBtnPos.top); |
44 | GUI.UIWidget.SetPosition(_joystickWindowName, "JoystickBtn" , JoystickBtnUIpos); |
45 | Logic.LogicMgr.Instance.SetDirection(Logic.Direction.Backward); |
46 | Logic.LogicMgr.Instance.CurPlayer.State = Logic.CharacterState.Running; |
52 | private void OnJSBtnPressed(FString sender, int value, int value2, MouseButton btn) |
56 | Debug.Dbgout( "isCtrl" ); |
59 | private void OnJSBtnReleased(FString sender, int value, int value2, MouseButton btn) |
63 | Debug.Dbgout( "Released" ); |
66 | private void Tick(FString sender, float gameTime, float frameTickTime) |
71 | GUI.UIWidget.SetPosition(_joystickWindowName, "JoystickBtn" , jsBtnPos); |
72 | if (Logic.LogicMgr.Instance.CurPlayer.State == Logic.CharacterState.Running) |
74 | Logic.LogicMgr.Instance.CurPlayer.State = Logic.CharacterState.Idle; |
|