欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity UGUI通過搖桿控制角色移動

 更新時間:2020年04月15日 09:08:03   作者:Htlas  
這篇文章主要為大家詳細(xì)介紹了Unity3D基于陀螺儀實現(xiàn)VR相機(jī)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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)文章

最新評論