Unity UGUI通過搖桿控制角色移動
本文實例為大家分享了Unity UGUI通過搖桿控制角色移動的具體代碼,供大家參考,具體內(nèi)容如下
簡單版:控制方塊的移動。

進(jìn)階版:控制人物的移動

知識鋪墊:
首先我們必須要知道,在Unity的UGUI中,對UI的操作有八個回調(diào),分別需要實現(xiàn)八個接口。分別是:
鼠標(biāo)進(jìn)入,鼠標(biāo)離開,鼠標(biāo)點下,鼠標(biāo)抬起,鼠標(biāo)開始拖拽,鼠標(biāo)拖拽中,拖拽結(jié)束
如下所示:

我們可以先對這幾個接口方法進(jìn)行一下測試:
測試結(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>
/// 當(dāng)鼠標(biāo)滑入控件的范圍
/// </summary>
/// <param name="eventData"></param>
public void OnPointerEnter(PointerEventData eventData) {
Debug.Log("鼠標(biāo)劃入");
}
/// <summary>
/// 當(dāng)鼠標(biāo)離開控件的范圍
/// </summary>
/// <param name="eventData"></param>
public void OnPointerExit(PointerEventData eventData) {
Debug.Log("鼠標(biāo)離開");
}
/// <summary>
/// 當(dāng)鼠標(biāo)在控件范圍內(nèi)按下
/// </summary>
/// <param name="eventData"></param>
public void OnPointerDown(PointerEventData eventData) {
Debug.Log("鼠標(biāo)按下");
}
/// <summary>
/// 當(dāng)鼠標(biāo)在控件范圍內(nèi)抬起
/// </summary>
/// <param name="eventData"></param>
public void OnPointerUp(PointerEventData eventData) {
Debug.Log("鼠標(biāo)抬起");
}
/// <summary>
/// 當(dāng)鼠標(biāo)在控件范圍內(nèi)點擊
/// </summary>
/// <param name="eventData"></param>
public void OnPointerClick(PointerEventData eventData) {
Debug.Log("鼠標(biāo)點擊");
}
/// <summary>
/// 當(dāng)鼠標(biāo)開始拖拽
/// </summary>
/// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData) {
Debug.Log("開始拖拽");
}
/// <summary>
/// 當(dāng)鼠標(biāo)拖拽過程中
/// </summary>
/// <param name="eventData"></param>
public void OnDrag(PointerEventData eventData) {
Debug.Log("拖拽中");
}
/// <summary>
/// 當(dāng)拖拽完成
/// </summary>
/// <param name="eventData"></param>
public void OnEndDrag(PointerEventData eventData) {
Debug.Log("拖拽完成");
}
}
下面開始講解案例:
第一步:實現(xiàn)對遙感按鈕的操作, 從上面的八大接口方法可以了解到,如果想實現(xiàn)遙感的方法我們需要實現(xiàn)有關(guā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{
//圖標(biāo)移動最大半徑
public float maxRadius = 100;
//初始化背景圖標(biāo)位置
private Vector2 moveBackPos;
// Use this for initialization
void Start () {
//初始化背景圖標(biāo)位置
moveBackPos = transform.parent.transform.position;
}
/// <summary>
/// 當(dāng)鼠標(biāo)開始拖拽時
/// </summary>
/// <param name="eventData"></param>
public void OnDrag(PointerEventData eventData) {
//獲取鼠標(biāo)位置與初始位置之間的向量
Vector2 oppsitionVec = eventData.position - moveBackPos;
//獲取向量的長度
float distance = Vector3.Magnitude(oppsitionVec);
//最小值與最大值之間取半徑
float radius = Mathf.Clamp(distance, 0, maxRadius);
//限制半徑長度
transform.position = moveBackPos + oppsitionVec.normalized * radius;
}
/// <summary>
/// 當(dāng)鼠標(biāo)停止拖拽時
/// </summary>
/// <param name="eventData"></param>
public void OnEndDrag(PointerEventData eventData) {
transform.position = moveBackPos;
}
}
如何控制木塊的移動呢:
初學(xué)者一般在學(xué)習(xí)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{
//圖標(biāo)移動最大半徑
public float maxRadius = 100;
//初始化背景圖標(biāo)位置
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 () {
//初始化背景圖標(biāo)位置
moveBackPos = transform.parent.transform.position;
}
// Update is called once per frame
void Update () {
horizontal = transform.localPosition.x;
vertical = transform.localPosition.y;
}
/// <summary>
/// 當(dāng)鼠標(biāo)開始拖拽時
/// </summary>
/// <param name="eventData"></param>
public void OnDrag(PointerEventData eventData) {
//獲取鼠標(biāo)位置與初始位置之間的向量
Vector2 oppsitionVec = eventData.position - moveBackPos;
//獲取向量的長度
float distance = Vector3.Magnitude(oppsitionVec);
//最小值與最大值之間取半徑
float radius = Mathf.Clamp(distance, 0, maxRadius);
//限制半徑長度
transform.position = moveBackPos + oppsitionVec.normalized * radius;
}
/// <summary>
/// 當(dāng)鼠標(biāo)停止拖拽時
/// </summary>
/// <param name="eventData"></param>
public void OnEndDrag(PointerEventData eventData) {
transform.position = moveBackPos;
transform.localPosition = Vector3.zero;
}
}
如何用遙感控制角色的移動,這里我們通過動畫的位移來控制移動。只需當(dāng)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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺拷貝和深拷貝深入理解(shallow copy VS deep copy)
淺拷貝和深拷貝深入理解(shallow copy VS deep copy) 本文重點討論引用類型變量的拷貝機制和實現(xiàn)2014-01-01
C#多線程學(xué)習(xí)之(四)使用線程池進(jìn)行多線程的自動管理
這篇文章主要介紹了C#多線程學(xué)習(xí)之使用線程池進(jìn)行多線程的自動管理,實例分析了C#中線程池的概念與相關(guān)的使用技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
Unity3D Shader實現(xiàn)掃描顯示效果(2)
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實現(xiàn)掃描顯示效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03
C# DoubleClick與MouseDoubleClick區(qū)別,雙擊事件引發(fā)順序
從邏輯上來說,由于比MouseDoubleClick 描述更抽象,DoubleClick 事件是控件的更高級別的事件2009-09-09
解析Silverlight調(diào)用WCF/Rest異常的解決方法
本篇文章對Silverlight調(diào)用WCF/Rest異常的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C#實現(xiàn)SSE(Server-Sent Events)服務(wù)端和客戶端的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)SSE(Server-Sent Events)服務(wù)端和客戶端的相關(guān)知識,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下2024-03-03
C#實現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義
ContextMenu 類表示當(dāng)用戶在控件或窗體的特定區(qū)域上單擊鼠標(biāo)右鍵時會顯示的快捷菜單,要想實現(xiàn)自定義的Menu和ContextMenu效果,大家可以通過派生ProfessionalColorTable類,下面小編把實現(xiàn)Menu和ContextMenu自定義風(fēng)格及ContextMenu自定義給大家整理一下2015-08-08

