亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Unity3D基于UGUI實現(xiàn)虛擬搖桿

 更新時間:2020年04月14日 11:14:00   作者:一縷殘陽  
這篇文章主要為大家詳細介紹了Unity3D基于UGUI實現(xiàn)虛擬搖桿,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

虛擬搖桿在移動游戲開發(fā)中,是很常見的需求,今天我們在Unity中,使用UGUI來實現(xiàn)一個簡單的虛擬搖桿功能。

1.打開Unity,新創(chuàng)建一個UIJoystick.cs腳本,代碼如下:

using UnityEngine;
using UnityEngine.EventSystems;
 
public class UIJoystick : MonoBehaviour, IDragHandler, IEndDragHandler
{  
  /// <summary>
 /// 被用戶拖動的操縱桿
  /// </summary>
  public Transform target;
 
  /// <summary>
 /// 操縱桿可移動的最大半徑
  /// </summary>
  public float radius = 50f;
  
  /// <summary>
 /// 當前操縱桿在2D空間的x,y位置
  /// 搖桿按鈕的值【-1,1】之間
  /// </summary>
  public Vector2 position;
    
 //操縱桿的RectTransform組件
 private RectTransform thumb;
 
 void Start()
 {
 thumb = target.GetComponent<RectTransform>();
 }
 
  /// <summary>
 /// 當操縱桿被拖動時觸發(fā)
  /// </summary>
  public void OnDrag(PointerEventData data)
 {
 //獲取搖桿的RectTransform組件,以檢測操縱桿是否在搖桿內(nèi)移動
 RectTransform draggingPlane = transform as RectTransform;
 Vector3 mousePos;
 
 //檢查拖動的位置是否在拖動rect內(nèi),
 //然后設(shè)置全局鼠標位置并將其分配給操縱桿
 if (RectTransformUtility.ScreenPointToWorldPointInRectangle (draggingPlane, data.position, data.pressEventCamera, out mousePos)) {
  thumb.position = mousePos;
 }
  
 //觸摸向量的長度(大?。?
 //計算操作桿的相對位置
 float length = target.localPosition.magnitude;
 
 //如果操縱桿超過了搖桿的范圍,則將操縱桿設(shè)置為最大半徑
 if (length > radius) {
  target.localPosition = Vector3.ClampMagnitude (target.localPosition, radius);
 }
  
 //在Inspector顯示操縱桿位置
 position = target.localPosition;
 //將操縱桿相對位置映射到【-1,1】之間
 position = position / radius * Mathf.InverseLerp (radius, 2, 1);
 }
  /// <summary>
 /// 當操縱桿結(jié)束拖動時觸發(fā)
  /// </summary>
  public void OnEndDrag(PointerEventData data)
 {
 //拖拽結(jié)束,將操縱桿恢復(fù)到默認位置
 position = Vector2.zero;
 target.position = transform.position;
 }
}

2.如圖創(chuàng)建UGUI,所用資源可在網(wǎng)上自行下載。

效果圖如下:

3.打包運行即可。這樣一個簡單的虛擬搖桿就實現(xiàn)了。

下面是對以上虛擬搖桿代碼的擴展(ps:只是多了一些事件,便于其他腳本訪問使用)廢話不多說來代碼了

using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
 
// 
// Joystick component for controlling player movement and actions using Unity UI events.
// There can be multiple joysticks on the screen at the same time, implementing different callbacks.
// 
public class UIJoystick : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
  /// 
  /// Callback triggered when joystick starts moving by user input.
  /// 
  public event Action onDragBegin;
  
  /// 
  /// Callback triggered when joystick is moving or hold down.
  /// 
  public event Action onDrag;
  
  /// 
  /// Callback triggered when joystick input is being released.
  /// 
  public event Action onDragEnd;
 
  /// 
  /// The target object i.e. jostick thumb being dragged by the user.
  /// 
  public Transform target;
 
  /// 
  /// Maximum radius for the target object to be moved in distance from the center.
  /// 
  public float radius = 50f;
  
  /// 
  /// Current position of the target object on the x and y axis in 2D space.
  /// Values are calculated in the range of [-1, 1] translated to left/down right/up.
  /// 
  public Vector2 position;
  
  //keeping track of current drag state
  private bool isDragging = false;
  
  //reference to thumb being dragged around
 private RectTransform thumb;
 
  //initialize variables
 void Start()
 {
 thumb = target.GetComponent();
 
 //in the editor, disable input received by joystick graphics:
    //we want them to be visible but not receive or block any input
 #if UNITY_EDITOR
  Graphic[] graphics = GetComponentsInChildren();
 // for(int i = 0; i < graphics.Length; i++)
 // graphics[i].raycastTarget = false;
 #endif
 }
 
  /// 
  /// Event fired by UI Eventsystem on drag start.
  /// 
  public void OnBeginDrag(PointerEventData data)
  {
    isDragging = true;
    if(onDragBegin != null)
      onDragBegin();
  }
 
  /// 
  /// Event fired by UI Eventsystem on drag.
  /// 
  public void OnDrag(PointerEventData data)
  {
    //get RectTransforms of involved components
    RectTransform draggingPlane = transform as RectTransform;
    Vector3 mousePos;
 
    //check whether the dragged position is inside the dragging rect,
    //then set global mouse position and assign it to the joystick thumb
    if (RectTransformUtility.ScreenPointToWorldPointInRectangle(draggingPlane, data.position, data.pressEventCamera, out mousePos))
    {
      thumb.position = mousePos;
    }
 
    //length of the touch vector (magnitude)
    //calculated from the relative position of the joystick thumb
    float length = target.localPosition.magnitude;
 
    //if the thumb leaves the joystick's boundaries,
    //clamp it to the max radius
    if (length > radius)
    {
      target.localPosition = Vector3.ClampMagnitude(target.localPosition, radius);
    }
 
    //set the Vector2 thumb position based on the actual sprite position
    position = target.localPosition;
    //smoothly lerps the Vector2 thumb position based on the old positions
    position = position / radius * Mathf.InverseLerp(radius, 2, 1);
  }
  
  //set joystick thumb position to drag position each frame
  void Update()
  {
    //in the editor the joystick position does not move, we have to simulate it
 //mirror player input to joystick position and calculate thumb position from that
 #if UNITY_EDITOR
  target.localPosition = position * radius;
  target.localPosition = Vector3.ClampMagnitude(target.localPosition, radius);
 #endif
 
    //check for actual drag state and fire callback. We are doing this in Update(),
    //not OnDrag, because OnDrag is only called when the joystick is moving. But we
    //actually want to keep moving the player even though the jostick is being hold down
    if(isDragging && onDrag != null)
      onDrag(position);
  }
 
  /// 
  /// Event fired by UI Eventsystem on drag end.
  /// 
  public void OnEndDrag(PointerEventData data)
  {
    //we aren't dragging anymore, reset to default position
    position = Vector2.zero;
    target.position = transform.position;
    
    //set dragging to false and fire callback
    isDragging = false;
    if (onDragEnd != null)
      onDragEnd();
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 教你如何用C#制作文字轉(zhuǎn)換成聲音程序

    教你如何用C#制作文字轉(zhuǎn)換成聲音程序

    近突發(fā)奇想,想玩玩文字轉(zhuǎn)語音的東東,想了下思路,用C#簡單實現(xiàn)了下,分享給大家,打算下面搞搞語音識別,下次分享給大家
    2014-09-09
  • Unity查找游戲物體的六種方式詳解

    Unity查找游戲物體的六種方式詳解

    最近學(xué)習unity3d做游戲,總結(jié)了一些實用的內(nèi)容,所以下面這篇文章主要給大家介紹了關(guān)于Unity查找游戲物體的六種方式,需要的朋友可以參考下
    2021-06-06
  • C#實現(xiàn)簡單的字符串加密

    C#實現(xiàn)簡單的字符串加密

    這篇文章介紹了C#實現(xiàn)字符串加密的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • c# FTP上傳文件實例代碼(簡易版)

    c# FTP上傳文件實例代碼(簡易版)

    下面小編就為大家分享一篇c# FTP上傳文件的實例代碼,超簡單哦~希望對大家有所幫助。一起跟隨小編過來看看吧,
    2017-12-12
  • C#下載歌詞文件的同步和異步方法

    C#下載歌詞文件的同步和異步方法

    這篇文章主要為大家詳細介紹了C#下載歌詞文件的同步和異步方法,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 100行C#代碼實現(xiàn)經(jīng)典掃雷游戲

    100行C#代碼實現(xiàn)經(jīng)典掃雷游戲

    這篇文章主要為大家詳細介紹了如何用100行C#代碼實現(xiàn)經(jīng)典的掃雷游戲,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下
    2023-02-02
  • 詳解C#中的泛型以及編程中使用泛型的優(yōu)點

    詳解C#中的泛型以及編程中使用泛型的優(yōu)點

    這篇文章主要介紹了詳解C#中的泛型以及編程中使用泛型的優(yōu)點,對泛型的支持時C#語言中的重要特性,需要的朋友可以參考下
    2016-02-02
  • 使用C#編寫15子游戲

    使用C#編寫15子游戲

    這篇文章主要為大家詳細介紹了使用C#編寫15子游戲的具體代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • C#定義的MP3播放類實例

    C#定義的MP3播放類實例

    這篇文章主要介紹了C#定義的MP3播放類,實例分析了C#操作多媒體音頻文件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C#實現(xiàn)漢字轉(zhuǎn)換為拼音縮寫的代碼

    C#實現(xiàn)漢字轉(zhuǎn)換為拼音縮寫的代碼

    這篇文章主要為大家詳細介紹了C#實現(xiàn)漢字轉(zhuǎn)換為拼音縮寫的代碼,感興趣的小伙伴們可以參考一下
    2016-07-07

最新評論