Unity UGUI通過搖桿控制角色移動
本文實例為大家分享了Unity UGUI通過搖桿控制角色移動的具體代碼,供大家參考,具體內(nèi)容如下
簡單版:控制方塊的移動。
進階版:控制人物的移動
知識鋪墊:
首先我們必須要知道,在Unity的UGUI中,對UI的操作有八個回調(diào),分別需要實現(xiàn)八個接口。分別是:
鼠標進入,鼠標離開,鼠標點下,鼠標抬起,鼠標開始拖拽,鼠標拖拽中,拖拽結(jié)束
如下所示:
我們可以先對這幾個接口方法進行一下測試:
測試結(jié)束后,大家就會對這些接口方法有一些初步的了解。
using UnityEngine; using UnityEngine.EventSystems; // UGUI提供了一些用來操作控件的一些方法, 這些方法是以回調(diào)的形式提供的 // 通過接口回調(diào)來實現(xiàn)的 /* * IPointerEnterHandler void OnPointerEnter(PointerEventData eventData) * IPointerExitHandler void OnPointerExit(PointerEventData eventData) * * IPointerDownHandler void OnPointerDown(PointerEventData eventData) * IPointerUpHandler void OnPointerUp(PointerEventData eventData) * IPointerClickHandler void OnPointerClick(PointerEventData eventData) * * IBeginDragHandler void OnBeginDrag(PointerEventData eventData) * IDragHandler void OnDrag(PointerEventData eventData) * IEndDragHandler void OnEndDrag(PointerEventData eventData) */ public class UGUICallBack : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler { /// <summary> /// 當鼠標滑入控件的范圍 /// </summary> /// <param name="eventData"></param> public void OnPointerEnter(PointerEventData eventData) { Debug.Log("鼠標劃入"); } /// <summary> /// 當鼠標離開控件的范圍 /// </summary> /// <param name="eventData"></param> public void OnPointerExit(PointerEventData eventData) { Debug.Log("鼠標離開"); } /// <summary> /// 當鼠標在控件范圍內(nèi)按下 /// </summary> /// <param name="eventData"></param> public void OnPointerDown(PointerEventData eventData) { Debug.Log("鼠標按下"); } /// <summary> /// 當鼠標在控件范圍內(nèi)抬起 /// </summary> /// <param name="eventData"></param> public void OnPointerUp(PointerEventData eventData) { Debug.Log("鼠標抬起"); } /// <summary> /// 當鼠標在控件范圍內(nèi)點擊 /// </summary> /// <param name="eventData"></param> public void OnPointerClick(PointerEventData eventData) { Debug.Log("鼠標點擊"); } /// <summary> /// 當鼠標開始拖拽 /// </summary> /// <param name="eventData"></param> public void OnBeginDrag(PointerEventData eventData) { Debug.Log("開始拖拽"); } /// <summary> /// 當鼠標拖拽過程中 /// </summary> /// <param name="eventData"></param> public void OnDrag(PointerEventData eventData) { Debug.Log("拖拽中"); } /// <summary> /// 當拖拽完成 /// </summary> /// <param name="eventData"></param> public void OnEndDrag(PointerEventData eventData) { Debug.Log("拖拽完成"); } }
下面開始講解案例:
第一步:實現(xiàn)對遙感按鈕的操作, 從上面的八大接口方法可以了解到,如果想實現(xiàn)遙感的方法我們需要實現(xiàn)有關拖拽的回調(diào):UI過拽中, UI拖拽結(jié)束
對遙感的操作代碼如下(非移動完整版,下面有移動完整版EasyTouchMove):
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class EasyTouchMove : MonoBehaviour, IDragHandler,IEndDragHandler{ //圖標移動最大半徑 public float maxRadius = 100; //初始化背景圖標位置 private Vector2 moveBackPos; // Use this for initialization void Start () { //初始化背景圖標位置 moveBackPos = transform.parent.transform.position; } /// <summary> /// 當鼠標開始拖拽時 /// </summary> /// <param name="eventData"></param> public void OnDrag(PointerEventData eventData) { //獲取鼠標位置與初始位置之間的向量 Vector2 oppsitionVec = eventData.position - moveBackPos; //獲取向量的長度 float distance = Vector3.Magnitude(oppsitionVec); //最小值與最大值之間取半徑 float radius = Mathf.Clamp(distance, 0, maxRadius); //限制半徑長度 transform.position = moveBackPos + oppsitionVec.normalized * radius; } /// <summary> /// 當鼠標停止拖拽時 /// </summary> /// <param name="eventData"></param> public void OnEndDrag(PointerEventData eventData) { transform.position = moveBackPos; } }
如何控制木塊的移動呢:
初學者一般在學習Unity的時候都是WSAD控制移動的,遙感控制移動只需要更改一個很小的地方即可:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Cube : MonoBehaviour { public EasyTouchMove touch; // Use this for initialization void Start () { } // Update is called once per frame void Update () { //獲取horizontal 和 vertical 的值,其值位遙感的localPosition float hor = touch.Horizontal; float ver = touch.Vertical; Vector3 direction = new Vector3(hor, 0, ver); if(direction!= Vector3.zero) { //控制轉(zhuǎn)向 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(direction),Time.deltaTime*10); //向前移動 transform.Translate(Vector3.forward * Time.deltaTime * 5); } } }
木塊版本遙感操作代碼:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class EasyTouchMove : MonoBehaviour, IDragHandler,IEndDragHandler{ //圖標移動最大半徑 public float maxRadius = 100; //初始化背景圖標位置 private Vector2 moveBackPos; //hor,ver的屬性訪問器 private float horizontal=0; private float vertical=0; public float Horizontal { get { return horizontal; } } public float Vertical { get { return vertical; } } // Use this for initialization void Start () { //初始化背景圖標位置 moveBackPos = transform.parent.transform.position; } // Update is called once per frame void Update () { horizontal = transform.localPosition.x; vertical = transform.localPosition.y; } /// <summary> /// 當鼠標開始拖拽時 /// </summary> /// <param name="eventData"></param> public void OnDrag(PointerEventData eventData) { //獲取鼠標位置與初始位置之間的向量 Vector2 oppsitionVec = eventData.position - moveBackPos; //獲取向量的長度 float distance = Vector3.Magnitude(oppsitionVec); //最小值與最大值之間取半徑 float radius = Mathf.Clamp(distance, 0, maxRadius); //限制半徑長度 transform.position = moveBackPos + oppsitionVec.normalized * radius; } /// <summary> /// 當鼠標停止拖拽時 /// </summary> /// <param name="eventData"></param> public void OnEndDrag(PointerEventData eventData) { transform.position = moveBackPos; transform.localPosition = Vector3.zero; } }
如何用遙感控制角色的移動,這里我們通過動畫的位移來控制移動。只需當director!=vector3.zero 的時候更改動畫控制器里的Float即可:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { //獲取動畫控制器 private Animator ani; //獲取遙感腳本 public EasyTouchMove touch; void Start () { ani = GetComponent<Animator>(); } // Update is called once per frame void Update () { //hor = 遙感腳本中的localPosition.x float hor = touch.Horizontal; //hor = 遙感腳本中的localPosition.y float ver = touch.Vertical; Vector3 direction = new Vector3(hor, 0, ver); if (direction != Vector3.zero) { //控制移動 float newSpeed = Mathf.Lerp(ani.GetFloat("Speed"), 3, Time.deltaTime * 5); ani.SetFloat("Speed", newSpeed); //控制旋轉(zhuǎn) transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * 10); }else { //停止移動 float newSpeed = Mathf.Lerp(ani.GetFloat("Speed"), 0, Time.deltaTime * 5); ani.SetFloat("Speed", 0); } } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解C#設置Excel數(shù)據(jù)自適應行高、列寬的2種情況
這篇文章主要介紹了C#設置Excel數(shù)據(jù)自適應行高、列寬的2種情況,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04C#實現(xiàn)獲取文本文件的編碼的一個類(區(qū)分GB2312和UTF8)
這篇文章主要介紹了C#實現(xiàn)獲取文本文件的編碼一個類,本文給出類可以自動區(qū)分GB2312和UTF8,并同時給出了使用方法,需要的朋友可以參考下2014-09-09可替代log4j日志的c#簡單日志類隊列實現(xiàn)類代碼分享
簡單日志類隊列實現(xiàn)??砂刺熘茉履甏笮》指钗募???珊唵翁娲鷏og4j2013-12-12