欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity實(shí)現(xiàn)簡(jiǎn)單的虛擬搖桿

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

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

需求:點(diǎn)擊創(chuàng)建一個(gè)虛擬搖桿底盤(pán),鼠標(biāo)拖拽時(shí)候上方搖桿會(huì)跟隨鼠標(biāo)方向移動(dòng),并且不會(huì)超出搖桿盤(pán)范圍
*搖桿功能另外實(shí)現(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);

    }
   }
  }

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

相關(guān)文章

  • 基于C#實(shí)現(xiàn)文檔打印功能

    基于C#實(shí)現(xiàn)文檔打印功能

    在軟件開(kāi)發(fā)過(guò)程中,文檔打印是一個(gè)常見(jiàn)的功能需求,本文將詳細(xì)介紹如何在C#中實(shí)現(xiàn)文檔打印,并通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定幫助,需要的朋友可以參考下
    2024-10-10
  • C#讀取文件MD5值的實(shí)現(xiàn)代碼

    C#讀取文件MD5值的實(shí)現(xiàn)代碼

    這篇文章主要介紹了C#讀取文件MD5值的實(shí)現(xiàn)代碼,有了這個(gè)核心代碼,就可以實(shí)現(xiàn)校驗(yàn)文件MD5值的一些程序了,需要的朋友可以參考下
    2014-08-08
  • C#壓縮和解壓文件的兩種方法

    C#壓縮和解壓文件的兩種方法

    在C#中,我們可以使用內(nèi)置的System.IO命名空間下的幾個(gè)類(lèi)來(lái)處理文件的壓縮和解壓縮,主要涉及到兩個(gè)常用的庫(kù):System.IO.Compression和WinRAR,以下是使用這些類(lèi)進(jìn)行文件壓縮和解壓縮的基本步驟,需要的朋友可以參考下
    2024-08-08
  • C#過(guò)濾DataTable中空數(shù)據(jù)和重復(fù)數(shù)據(jù)的示例代碼

    C#過(guò)濾DataTable中空數(shù)據(jù)和重復(fù)數(shù)據(jù)的示例代碼

    這篇文章主要給大家介紹了關(guān)于C#過(guò)濾DataTable中空數(shù)據(jù)和重復(fù)數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C#使用Fleck實(shí)現(xiàn)創(chuàng)建WebSocket服務(wù)器

    C#使用Fleck實(shí)現(xiàn)創(chuàng)建WebSocket服務(wù)器

    這篇文章主要為大家詳細(xì)介紹了C#如何使用Fleck實(shí)現(xiàn)創(chuàng)建WebSocket服務(wù)器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • 大白話講解C# 中的委托

    大白話講解C# 中的委托

    這篇文章主要介紹了C# 中的委托的相關(guān)資料,幫助初學(xué)者更好的理解和使用c#,感興趣的朋友可以了解下
    2020-11-11
  • C#實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法

    C#實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法,涉及C#操作word及Excel格式文件的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • 如何清空文件夾里面的所有文件和文件夾

    如何清空文件夾里面的所有文件和文件夾

    以下是對(duì)c#中清空文件夾里面的所有文件和文件夾的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
    2013-07-07
  • 在C#中List集合使用First()方法獲取第一個(gè)元素的操作

    在C#中List集合使用First()方法獲取第一個(gè)元素的操作

    這篇文章主要介紹了在C#中List集合使用First()方法獲取第一個(gè)元素的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • 用.NET創(chuàng)建Windows服務(wù)的方法

    用.NET創(chuàng)建Windows服務(wù)的方法

    用.NET創(chuàng)建Windows服務(wù)的方法...
    2007-03-03

最新評(píng)論