温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

unity如何实现手游虚拟摇杆

发布时间:2020-08-03 13:53:52 来源:亿速云 阅读:155 作者:小猪 栏目:编程语言

这篇文章主要为大家展示了unity如何实现手游虚拟摇杆,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 绑定到摇杆上的摇杆类,参考半径50
/// </summary>
public class Rocker : MonoBehaviour {

  Vector2 m_offet;//偏移向量
  Vector2 m_originalPos;//摇杆原始屏幕坐标
  Touch[] touches;//屏幕上触控点数组
  int touch_Id = -1;//触控点数组下标
  bool isMove = false;//是否移动
  float m_ScreenScale;
  /// <summary>
  /// 给外部调用的偏移向量,告知摇杆参数
  /// </summary>
  public Vector3 Offet
  {
    get
    {
      return m_offet;
    }
  }

  // Use this for initialization
  void Start () {

    m_originalPos = transform.position;//摇杆中心的屏幕坐标位置
    m_ScreenScale = Screen.width / 800f;
  }

  // Update is called once per frame
  void Update () {
    //得到屏幕触控数组
    touches = Input.touches;
    if (touches.Length > 0)//如果触点开启
    {
      //得到离摇杆中心最近的触点下标 touch_Id;
      if (touches.Length == 1)//只有一个触点时
      {
        touch_Id = 0;
      }
      else if (touches.Length > 1)//触点大于1个时
      {
        touch_Id = 0;//先假设下标为0
        for (int i = 1; i < touches.Length; i++)//遍历触点数组
        {
          if (Vector2.SqrMagnitude(touches[i].position - m_originalPos) < Vector2.SqrMagnitude(touches[touch_Id].position - m_originalPos))//第i个点比假设的点近
          {
            touch_Id = i;//假设的点改为第i个点
          }
        }

      }

      //如果得到的触点不是取消或抬起
      if (Input.GetTouch(touch_Id).phase != TouchPhase.Canceled && Input.GetTouch(touch_Id).phase != TouchPhase.Ended)
      {
        //触点在摇杆范围内
        if(Vector2.SqrMagnitude(touches[touch_Id].position - m_originalPos) <= 50*50 * m_ScreenScale * m_ScreenScale)//50为背景半径
        {
          isMove = true;//开启遥控
          //摇杆开始控制,计算偏移量
          SetOffetIn();
        }
        else if(isMove)//触点在摇杆范围外,但是遥控已经开启
        {
          SetOffetOut();
        }
      }
      else// 手指抬起,摇杆回归原始位置
      {
        transform.position = m_originalPos;
        m_offet = Vector2.zero;
        isMove = false;
        touch_Id = -1;
      }
    }

  }
  /// <summary>
  /// 触点在操作盘内时
  /// 摇杆控制方法
  /// </summary>
  void SetOffetIn()
  {
    //距离过小视为不偏移摇杆位置不变
    if(Vector2.SqrMagnitude(touches[touch_Id].position - m_originalPos) < 5 * m_ScreenScale)
    {
      GetComponent<Image>().rectTransform.position = m_originalPos;//摇杆定位在原始位置
      m_offet = Vector3.zero;
    }
    else
    {
      //摇杆位置追踪
      GetComponent<Image>().rectTransform.position = touches[touch_Id].position;
      m_offet = touches[touch_Id].position - m_originalPos;//赋值偏移值
      m_offet = m_offet.normalized;//归一化
    }
  }
  /// <summary>
  /// 触点在操作盘外时
  /// 摇杆控制方法
  /// </summary>
  void SetOffetOut()
  {
    Vector2 tempDir;//临时偏移向量
    tempDir = touches[touch_Id].position - m_originalPos;
    //更新摇杆位置:距离原始位置127各单位
    GetComponent<Image>().rectTransform.position = m_originalPos + (tempDir.normalized) * 25*m_ScreenScale;
    //偏移量
    m_offet = tempDir.normalized;//归一化
  }
  private void OnGUI()
  {
    GUIStyle style = new GUIStyle(); //实例化一个新的GUIStyle,名称为style ,后期使用
    style.fontSize = 50; //字体的大小设置数值越大,字越大,默认颜色为黑色 
    style.normal.textColor = new Color(1, 1, 1); //设置文本的颜色为 新的颜色(0,0,0)修改值-代表不同的颜色,值为整数 我个人觉得有点像RGB的感觉
    GUI.Label(new Rect(20, 30, 300, 60), "原始位置:" + m_originalPos.ToString(),style);
    GUI.Label(new Rect(20, 100, 300, 60), "摇杆位置:" + GetComponent<Image>().rectTransform.position.ToString(), style);
    GUI.Label(new Rect(20, 170, 300, 60), "触点位置:" + touches[touch_Id].position.ToString(), style);
    GUI.Label(new Rect(20, 240, 300, 60), "屏幕分辨率:" + Screen.currentResolution, style);


  }
}

以上就是关于unity如何实现手游虚拟摇杆的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI