最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity實現(xiàn)簡單的虛擬搖桿

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

本文實例為大家分享了Unity實現(xiàn)簡單虛擬搖桿的具體代碼,供大家參考,具體內(nèi)容如下

需求:點擊創(chuàng)建一個虛擬搖桿底盤,鼠標(biāo)拖拽時候上方搖桿會跟隨鼠標(biāo)方向移動,并且不會超出搖桿盤范圍
*搖桿功能另外實現(xiàn)

UI顯示

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RockingIcon : MonoBehaviour
{
 public Transform touchPoint;
 public Transform bgPoint;
 public float radius;
 bool isPressing;
 Vector3 bgPos;

 private void Update()
 {
  bool pressing;
  Vector3 pos;
  if (Application.isEditor)
   GetPressingInfoInEditor(out pressing, out pos);
  else
   GetPressingInfoInPhone(out pressing, out pos);
  SetIcon(pressing, pos);

 }

 void GetPressingInfoInEditor(out bool pressing, out Vector3 pos)
 {
  if (Input.GetMouseButton(0))
  {
   pressing = true;
   pos = Input.mousePosition;
  }
  else
  {
   pressing = false;
   pos = Vector3.zero;
  }
 }

 void GetPressingInfoInPhone(out bool pressing, out Vector3 pos)
 {
  if(Input.touchCount > 0)
  {
   pressing = true;
   pos = Input.GetTouch(0).position;
  }
  else
  {
   pressing = false;
   pos = Vector3.zero;
  }
 }


 void SetIcon(bool pressing, Vector3 pos)
 {
  if (pressing)
  {
   if (!isPressing)
   {
    bgPoint.gameObject.SetActive(true);
    bgPoint.transform.position = pos;
    bgPos = pos;
    isPressing = true;
   }
   else
   {
    bgPoint.gameObject.SetActive(true);
    SetTouchPointPos(pos);
   }
  }
  else
  {
   touchPoint.gameObject.SetActive(false);
   bgPoint.gameObject.SetActive(false);
   isPressing = false;
  }
 }

 void SetTouchPointPos(Vector3 pos)
 {
  Vector3 center = bgPoint.position;
  Vector3 touch = pos;
  Vector3 to;
  float distance = Vector3.Distance(center, touch);
  if (distance < radius)
   to = touch;
  else
  {
   Vector3 dir = touch - center;
   dir.Normalize();
   to = dir * radius;
   to += center;
  }
  touchPoint.gameObject.SetActive(true);
  touchPoint.transform.position = to;
 }
}

預(yù)制:

操作控制

#region 鼠標(biāo)操作

float min_move_x = Global.min_move_distance * (Screen.width / 1080f);
float min_move_y = Global.min_move_distance * (Screen.height / 1900f);

if(Application.platform == RuntimePlatform.WindowsEditor)
  {
   if (Input.GetMouseButtonDown(0))
   {
    touch_time = 0;
    first_touch_pos = Input.mousePosition;
   }
   else if (Input.GetMouseButton(0))
   {

    touch_time += Time.deltaTime;
    if (touch_time >= Global.touch_time_limit)
    {
     Vector2 touch_pos = Input.mousePosition;
     Vector2 distance = touch_pos - first_touch_pos;

     //Vector2 touch_pos_in_func = PosInTheFunc(touch_pos);
     //Vector2 first_pos_in_func = PosInTheFunc(first_touch_pos);
     //Vector2 distance = touch_pos_in_func - first_pos_in_func;

     if (Mathf.Abs(distance.x) > min_move_x && Mathf.Abs(distance.x) > Mathf.Abs(distance.y)) Move(distance.x > 0 ? Vector3.right : Vector3.left);
     if (Mathf.Abs(distance.y) > min_move_y && Mathf.Abs(distance.y) > Mathf.Abs(distance.x)) Move(distance.y > 0 ? Vector3.forward : Vector3.back);
    }
   }
   else if (Input.GetMouseButtonUp(0))
   {

    //if(touch_time < Global.touch_time_limit)
    //{
    // PutBoomb();
    //}
    touch_time = 0;
    first_touch_pos = Vector3.zero;
   }
  }
  #endregion
  #region 手機(jī)操作

  if (Application.platform == RuntimePlatform.Android)
  {
   if (Input.touchCount > 0)
   {

    Touch touch = Input.GetTouch(0);
    if (touch.phase == TouchPhase.Began)
    {
     first_touch_pos = touch.position;
     

    }
    else if (touch.phase == TouchPhase.Ended)
    {
     first_touch_pos = Vector3.zero;
    }
    else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
    {
     Vector2 touch_pos = touch.position;
     Vector2 distance = touch_pos - first_touch_pos;
     if (Mathf.Abs(distance.x) > min_move_x && Mathf.Abs(distance.x) > Mathf.Abs(distance.y)) Move(distance.x > 0 ? Vector3.right : Vector3.left);
     if (Mathf.Abs(distance.y) > min_move_y && Mathf.Abs(distance.y) > Mathf.Abs(distance.x)) Move(distance.y > 0 ? Vector3.forward : Vector3.back);

    }
   }
  }

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

相關(guān)文章

  • C#開發(fā)Windows UWP系列之3D變換

    C#開發(fā)Windows UWP系列之3D變換

    這篇文章介紹了C#開發(fā)Windows UWP系列之3D變換,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C#如何通過probing指定dll尋找文件夾詳解

    C#如何通過probing指定dll尋找文件夾詳解

    這篇文章主要給大家介紹了關(guān)于C#如何通過probing指定dll尋找文件夾的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • C#子類對基類方法的繼承、重寫與隱藏詳解

    C#子類對基類方法的繼承、重寫與隱藏詳解

    這篇文章主要介紹了C#子類對基類方法的繼承、重寫與隱藏的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • C#操作注冊表的方法

    C#操作注冊表的方法

    以下從‘讀’‘寫’‘刪除’‘判斷’四個事例實現(xiàn)對注冊表的簡單操作
    2007-03-03
  • C# 讀寫自定義的Config文件的實現(xiàn)方法

    C# 讀寫自定義的Config文件的實現(xiàn)方法

    本文主要介紹了C# 讀寫自定義的Config文件的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 關(guān)于System.Convert的那些事兒

    關(guān)于System.Convert的那些事兒

    本篇文章是對System.Convert的那些事兒,進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C#實現(xiàn)WinForm全屏置頂?shù)氖纠a

    C#實現(xiàn)WinForm全屏置頂?shù)氖纠a

    我們在運行一些?Windows?應(yīng)用程序的時候,需要將其運行在窗體置頂?shù)哪J?并且進(jìn)入全屏狀態(tài),本文將介紹如何使用?C#?來實現(xiàn)?WinForm?的全屏置頂?shù)幕竟δ?感興趣的可以了解下
    2024-12-12
  • 字符串替換Replace僅替換第一個字符串匹配項

    字符串替換Replace僅替換第一個字符串匹配項

    C#里面的String.Replace(string,string)方法替換的時候是替換所有的匹配項,我們需要只替換第一個匹配項,寫一個方法來實現(xiàn)這個功能
    2013-12-12
  • C#實現(xiàn)讓窗體永遠(yuǎn)在窗體最前面顯示的實例

    C#實現(xiàn)讓窗體永遠(yuǎn)在窗體最前面顯示的實例

    這篇文章主要介紹了C#實現(xiàn)讓窗體永遠(yuǎn)在窗體最前面顯示,功能非常實用,需要的朋友可以參考下
    2014-07-07
  • gridview 顯示圖片的實例代碼

    gridview 顯示圖片的實例代碼

    gridview 圖片的二進(jìn)制數(shù)據(jù)庫存儲和顯示
    2013-04-04

最新評論

孟州市| 周至县| 浪卡子县| 东平县| 莎车县| 芜湖市| 阜城县| 新平| 镇平县| 镇远县| 郸城县| 集安市| 股票| 邳州市| 台东县| 阳西县| 田林县| 鲜城| 宁强县| 隆回县| 图片| 靖西县| 本溪市| 甘泉县| 淮安市| 台北市| 清远市| 渭源县| 临沂市| 林芝县| 门源| 靖宇县| 连云港市| 都江堰市| 同心县| 广东省| 高邑县| 平泉县| 吉水县| 普定县| 萨嘎县|