Unity實現(xiàn)簡單的虛擬搖桿
更新時間:2020年04月14日 11:35:24 作者:RiKoPon
這篇文章主要為大家詳細介紹了Unity實現(xiàn)簡單的虛擬搖桿,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Unity實現(xiàn)簡單虛擬搖桿的具體代碼,供大家參考,具體內容如下
需求:點擊創(chuàng)建一個虛擬搖桿底盤,鼠標拖拽時候上方搖桿會跟隨鼠標方向移動,并且不會超出搖桿盤范圍
*搖桿功能另外實現(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;
}
}
預制:

操作控制
#region 鼠標操作
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 手機操作
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);
}
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#過濾DataTable中空數(shù)據(jù)和重復數(shù)據(jù)的示例代碼
這篇文章主要給大家介紹了關于C#過濾DataTable中空數(shù)據(jù)和重復數(shù)據(jù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
C#使用Fleck實現(xiàn)創(chuàng)建WebSocket服務器
這篇文章主要為大家詳細介紹了C#如何使用Fleck實現(xiàn)創(chuàng)建WebSocket服務器,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-01-01
C#實現(xiàn)將數(shù)據(jù)導出到word或者Excel中的方法
這篇文章主要介紹了C#實現(xiàn)將數(shù)據(jù)導出到word或者Excel中的方法,涉及C#操作word及Excel格式文件的方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
在C#中List集合使用First()方法獲取第一個元素的操作
這篇文章主要介紹了在C#中List集合使用First()方法獲取第一個元素的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12

