Unity UGUI通过摇杆控制角色移动
本文实例为大家分享了UnityUGUI通过摇杆控制角色移动的具体代码,供大家参考,具体内容如下
简单版:控制方块的移动。
进阶版:控制人物的移动
知识铺垫:
首先我们必须要知道,在Unity的UGUI中,对UI的操作有八个回调,分别需要实现八个接口。分别是:
鼠标进入,鼠标离开,鼠标点下,鼠标抬起,鼠标开始拖拽,鼠标拖拽中,拖拽结束
如下所示:
我们可以先对这几个接口方法进行一下测试:
测试结束后,大家就会对这些接口方法有一些初步的了解。
usingUnityEngine;
usingUnityEngine.EventSystems;
//UGUI提供了一些用来操作控件的一些方法,这些方法是以回调的形式提供的
//通过接口回调来实现的
/*
*IPointerEnterHandlervoidOnPointerEnter(PointerEventDataeventData)
*IPointerExitHandlervoidOnPointerExit(PointerEventDataeventData)
*
*IPointerDownHandlervoidOnPointerDown(PointerEventDataeventData)
*IPointerUpHandlervoidOnPointerUp(PointerEventDataeventData)
*IPointerClickHandlervoidOnPointerClick(PointerEventDataeventData)
*
*IBeginDragHandlervoidOnBeginDrag(PointerEventDataeventData)
*IDragHandlervoidOnDrag(PointerEventDataeventData)
*IEndDragHandlervoidOnEndDrag(PointerEventDataeventData)
*/
publicclassUGUICallBack:MonoBehaviour,
IPointerEnterHandler,IPointerExitHandler,
IPointerDownHandler,IPointerUpHandler,IPointerClickHandler,
IBeginDragHandler,IDragHandler,IEndDragHandler
{
///
///当鼠标滑入控件的范围
///
///
publicvoidOnPointerEnter(PointerEventDataeventData){
Debug.Log("鼠标划入");
}
///
///当鼠标离开控件的范围
///
///
publicvoidOnPointerExit(PointerEventDataeventData){
Debug.Log("鼠标离开");
}
///
///当鼠标在控件范围内按下
///
///
publicvoidOnPointerDown(PointerEventDataeventData){
Debug.Log("鼠标按下");
}
///
///当鼠标在控件范围内抬起
///
///
publicvoidOnPointerUp(PointerEventDataeventData){
Debug.Log("鼠标抬起");
}
///
///当鼠标在控件范围内点击
///
///
publicvoidOnPointerClick(PointerEventDataeventData){
Debug.Log("鼠标点击");
}
///
///当鼠标开始拖拽
///
///
publicvoidOnBeginDrag(PointerEventDataeventData){
Debug.Log("开始拖拽");
}
///
///当鼠标拖拽过程中
///
///
publicvoidOnDrag(PointerEventDataeventData){
Debug.Log("拖拽中");
}
///
///当拖拽完成
///
///
publicvoidOnEndDrag(PointerEventDataeventData){
Debug.Log("拖拽完成");
}
}
下面开始讲解案例:
第一步:实现对遥感按钮的操作,从上面的八大接口方法可以了解到,如果想实现遥感的方法我们需要实现有关拖拽的回调:UI过拽中,UI拖拽结束
对遥感的操作代码如下(非移动完整版,下面有移动完整版EasyTouchMove):
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
usingUnityEngine.EventSystems;
publicclassEasyTouchMove:MonoBehaviour,IDragHandler,IEndDragHandler{
//图标移动最大半径
publicfloatmaxRadius=100;
//初始化背景图标位置
privateVector2moveBackPos;
//Usethisforinitialization
voidStart(){
//初始化背景图标位置
moveBackPos=transform.parent.transform.position;
}
///
///当鼠标开始拖拽时
///
///
publicvoidOnDrag(PointerEventDataeventData){
//获取鼠标位置与初始位置之间的向量
Vector2oppsitionVec=eventData.position-moveBackPos;
//获取向量的长度
floatdistance=Vector3.Magnitude(oppsitionVec);
//最小值与最大值之间取半径
floatradius=Mathf.Clamp(distance,0,maxRadius);
//限制半径长度
transform.position=moveBackPos+oppsitionVec.normalized*radius;
}
///
///当鼠标停止拖拽时
///
///
publicvoidOnEndDrag(PointerEventDataeventData){
transform.position=moveBackPos;
}
}
如何控制木块的移动呢:
初学者一般在学习Unity的时候都是WSAD控制移动的,遥感控制移动只需要更改一个很小的地方即可:
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
publicclassCube:MonoBehaviour{
publicEasyTouchMovetouch;
//Usethisforinitialization
voidStart(){
}
//Updateiscalledonceperframe
voidUpdate(){
//获取horizontal和vertical的值,其值位遥感的localPosition
floathor=touch.Horizontal;
floatver=touch.Vertical;
Vector3direction=newVector3(hor,0,ver);
if(direction!=Vector3.zero){
//控制转向
transform.rotation=Quaternion.Lerp(transform.rotation,Quaternion.LookRotation(direction),Time.deltaTime*10);
//向前移动
transform.Translate(Vector3.forward*Time.deltaTime*5);
}
}
}
木块版本遥感操作代码:
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
usingUnityEngine.EventSystems;
publicclassEasyTouchMove:MonoBehaviour,IDragHandler,IEndDragHandler{
//图标移动最大半径
publicfloatmaxRadius=100;
//初始化背景图标位置
privateVector2moveBackPos;
//hor,ver的属性访问器
privatefloathorizontal=0;
privatefloatvertical=0;
publicfloatHorizontal{
get{returnhorizontal;}
}
publicfloatVertical{
get{returnvertical;}
}
//Usethisforinitialization
voidStart(){
//初始化背景图标位置
moveBackPos=transform.parent.transform.position;
}
//Updateiscalledonceperframe
voidUpdate(){
horizontal=transform.localPosition.x;
vertical=transform.localPosition.y;
}
///
///当鼠标开始拖拽时
///
///
publicvoidOnDrag(PointerEventDataeventData){
//获取鼠标位置与初始位置之间的向量
Vector2oppsitionVec=eventData.position-moveBackPos;
//获取向量的长度
floatdistance=Vector3.Magnitude(oppsitionVec);
//最小值与最大值之间取半径
floatradius=Mathf.Clamp(distance,0,maxRadius);
//限制半径长度
transform.position=moveBackPos+oppsitionVec.normalized*radius;
}
///
///当鼠标停止拖拽时
///
///
publicvoidOnEndDrag(PointerEventDataeventData){
transform.position=moveBackPos;
transform.localPosition=Vector3.zero;
}
}
如何用遥感控制角色的移动,这里我们通过动画的位移来控制移动。只需当director!=vector3.zero的时候更改动画控制器里的Float即可:
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
publicclassPlayerController:MonoBehaviour{
//获取动画控制器
privateAnimatorani;
//获取遥感脚本
publicEasyTouchMovetouch;
voidStart(){
ani=GetComponent();
}
//Updateiscalledonceperframe
voidUpdate(){
//hor=遥感脚本中的localPosition.x
floathor=touch.Horizontal;
//hor=遥感脚本中的localPosition.y
floatver=touch.Vertical;
Vector3direction=newVector3(hor,0,ver);
if(direction!=Vector3.zero){
//控制移动
floatnewSpeed=Mathf.Lerp(ani.GetFloat("Speed"),3,Time.deltaTime*5);
ani.SetFloat("Speed",newSpeed);
//控制旋转
transform.rotation=Quaternion.Lerp(transform.rotation,Quaternion.LookRotation(direction),Time.deltaTime*10);
}else{
//停止移动
floatnewSpeed=Mathf.Lerp(ani.GetFloat("Speed"),0,Time.deltaTime*5);
ani.SetFloat("Speed",0);
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。