栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

unity实现手游虚拟摇杆

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity实现手游虚拟摇杆

本文实例为大家分享了unity实现手游虚拟摇杆的具体代码,供大家参考,具体内容如下

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

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

  }
  /// 
  /// 触点在操作盘内时
  /// 摇杆控制方法
  /// 
  void SetOffetIn()
  {
    //距离过小视为不偏移摇杆位置不变
    if(Vector2.SqrMagnitude(touches[touch_Id].position - m_originalPos) < 5 * m_ScreenScale)
    {
      GetComponent().rectTransform.position = m_originalPos;//摇杆定位在原始位置
      m_offet = Vector3.zero;
    }
    else
    {
      //摇杆位置追踪
      GetComponent().rectTransform.position = touches[touch_Id].position;
      m_offet = touches[touch_Id].position - m_originalPos;//赋值偏移值
      m_offet = m_offet.normalized;//归一化
    }
  }
  /// 
  /// 触点在操作盘外时
  /// 摇杆控制方法
  /// 
  void SetOffetOut()
  {
    Vector2 tempDir;//临时偏移向量
    tempDir = touches[touch_Id].position - m_originalPos;
    //更新摇杆位置:距离原始位置127各单位
    GetComponent().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().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);


  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/122207.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号