1. 程式人生 > 程式設計 >Unity實現虛擬搖桿

Unity實現虛擬搖桿

本文例項為大家分享了Unity實現虛擬搖桿的具體程式碼,供大家參考,具體內容如下

Unity實現虛擬搖桿

面板上設定一些屬性,比如搖桿拖拽的距離,是否始終可視,是否限制虛擬搖桿位置(我是把虛擬搖桿限制在了螢幕的左下區域)。
使用GetDirAndLength()方法去獲得移動的方向和長度即可

Unity實現虛擬搖桿

using UnityEngine;
 
/// <summary>
/// 虛擬搖桿管理器
/// </summary>
public class VirtualJoystickManager : MonoBehaviour
{
  private static VirtualJoystickManager _instance;
  public static VirtualJoystickManager Instance
  {
    get
    {
      if (_instance == null)
      {
        _instance = FindObjectOfType<VirtualJoystickManager>();
      }
      return _instance;
    }
  }
 
  [Header("是否始終可視")]
  public bool alwaysVisible;//是否始終可視
  [Header("是否限制虛擬搖桿位置")]
  public bool restrictVirtualJoystickPos;//是否限制虛擬搖桿位置
 
  [Header("虛擬搖桿物體")]
  [Header("==========")]
  public GameObject virtualJoystick;//虛擬搖桿父物體
  public GameObject inside;//內環
  public GameObject outside;//外環
 
  [Header("最大拖拽距離")]
  [Header("==========")]
  public float maxDragLength;//最大拖拽距離
 
  private Vector3 virtualJoystickCenter;//虛擬軸中心
 
  private void Update()
  {
    //如果限制虛擬軸位置並且虛擬軸位置超出了限制範圍則不進行任何操作
    if (restrictVirtualJoystickPos && JudgeIsValidRange() == false)
    {
      return;
    }
 
    //更新顯示
    UpdateShow();
 
    //更新虛擬搖桿位置
    if (Input.GetMouseButtonDown(0))
    {
      UpdateVirtualJoystickPos();
    }
    else if (Input.GetMouseButtonUp(0))
    {
      inside.transform.position = virtualJoystickCenter;
    }
 
    //更新內環位置(限制拖拽範圍)
    if (Input.GetMouseButton(0))
    {
      UpdateInsidePos();
    }
  }
 
  /// <summary>
  /// 更新顯示
  /// </summary>
  private void UpdateShow()
  {
    if (alwaysVisible)
    {
      inside.SetActive(true);
      outside.SetActive(true);
    }
    else if (alwaysVisible == false)
    {
      if (Input.GetMouseButtonDown(0))
      {
        inside.SetActive(true);
        outside.SetActive(true);
      }
      if (Input.GetMouseButtonUp(0))
      {
        inside.SetActive(false);
        outside.SetActive(false);
      }
    }
  }
 
  /// <summary>
  /// 更新虛擬搖桿位置
  /// </summary>
  private void UpdateVirtualJoystickPos()
  {
    //得到虛擬軸的中心位置
    virtualJoystickCenter = ScreenToWorld(Input.mousePosition);
 
    //設定虛擬軸的位置
    virtualJoystick.transform.position = virtualJoystickCenter;
  }
 
  /// <summary>
  /// 更新內環位置
  /// </summary>
  private void UpdateInsidePos()
  {
    inside.transform.position = ScreenToWorld(Input.mousePosition);
    if (Vector3.Distance(inside.transform.position,virtualJoystickCenter) > maxDragLength)
    {
      Vector3 normalizedPos = (inside.transform.position - virtualJoystickCenter).normalized;
      inside.transform.position = normalizedPos * maxDragLength + virtualJoystickCenter;
    }
  }
 
  /// <summary>
  /// 判斷是否為有效的範圍
  /// </summary>
  /// <returns>是否為有效的範圍</returns>
  private bool JudgeIsValidRange()
  {
    if (inside.activeInHierarchy)
    {
      return true;
    }
 
    Vector2 v = Input.mousePosition;
    if (v.x > Screen.width / 2 || v.x < 0)
    {
      return false;
    }
    else if (v.y > Screen.height / 2 || v.y < 0)
    {
      return false;
    }
    return true;
  }
 
  /// <summary>
  /// 螢幕座標轉世界座標
  /// </summary>
  /// <param name="screenPos">螢幕座標位置</param>
  /// <param name="camera">相機</param>
  /// <returns>轉換後的世界座標</returns>
  public static Vector3 ScreenToWorld(Vector3 screenPos,Camera camera = null)
  {
    if (camera == null)
    {
      camera = Camera.main;
    }
    Vector3 _screenPos = new Vector3(screenPos.x,screenPos.y,-camera.transform.position.z);
    Vector3 v = camera.ScreenToWorldPoint(_screenPos);
    return v;
  }
 
  /// <summary>
  /// 得到運動的方向和長度
  /// </summary>
  /// <returns>方向和長度</returns>
  public Vector3 GetDirAndLength()
  {
    return inside.transform.position - virtualJoystickCenter;
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。