用于控制角色的移动的最好办法便是使用摇杆事件,使用最多的摇杆插件EastyJoystick,也是EasyTouch摇杆的部分功能,

导入下载好的资源包,即EasyTouch插件
添加一个摇杆/按钮,,,
在Inspector面板中设置摇杆相关参数,,,

Proerties: 摇杆属性设置
Position & Size : 摇杆位置及大小
Joystick Axes properties & Events: 摇杆轴属性及事件
Joystick: 纹理图片
Dynamic joystick: 是否为动态摇杆,即在按下手指的位置出现摇杆。
Anchor: 预置位置 默认Lower Left(左右角)
Offset:偏移量
其它:调整摇杆的大小等。
Interaction Type:交互类型:
Direct 直接拖动物体到启用轴上去,选择交互效果。
Event Notification : 事件通知方式,较为常用。
使用面板属性操作实现Buton按钮

使用代码实现Button按钮

Button 上图代码:
using UnityEngine; public class quest1 : MonoBehaviour { // Use this for initialization void Start() { EasyButton.On_ButtonDown += Show; //注册事件 } void Show(string name) //自定义方法,注意:必须由参数和委托对应 { Debug.Log(name + "按钮触发事件"); } private void OnDisable() //表示当前脚本不可用(未激活)时调用 { EasyButton.On_ButtonDown -= Show; //注销事件 } private void OnDestroy() //销毁时调用,,为了严谨最好写上,作用同上 { EasyButton.On_ButtonDown -= Show; } }

使用EasyJoystick,移动物体
上图所示代码
public class Easyjoy : MonoBehaviour { float vx; float vy; // Use this for initialization void Start () { EasyJoystick.On_JoystickMove += Move; //注册移动事件 EasyJoystick.On_JoystickMoveEnd += End; //注册停止事件 } // Update is called once per frame void Update () { this.gameObject.transform.Translate(new Vector3(vx,0,vy)); } void Move(MovingJoystick mo) //自定义移动方法 { vx = mo.joystickAxis.x; vy = mo.joystickAxis.y; } void End(MovingJoystick mo) //自定义停止方法 { vx = vy = 0; } }
作者:Czhenya 发表于2017/8/14 18:31:17 原文链接
阅读:15 评论:0 查看评论