unity實(shí)現(xiàn)手游虛擬搖桿
更新時間:2020年04月15日 11:19:45 作者:玉骨寒
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)手游虛擬搖桿,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了unity實(shí)現(xiàn)手游虛擬搖桿的具體代碼,供大家參考,具體內(nèi)容如下
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;//搖桿原始屏幕坐標(biāo) Touch[] touches;//屏幕上觸控點(diǎn)數(shù)組 int touch_Id = -1;//觸控點(diǎn)數(shù)組下標(biāo) bool isMove = false;//是否移動 float m_ScreenScale; /// <summary> /// 給外部調(diào)用的偏移向量,告知搖桿參數(shù) /// </summary> public Vector3 Offet { get { return m_offet; } } // Use this for initialization void Start () { m_originalPos = transform.position;//搖桿中心的屏幕坐標(biāo)位置 m_ScreenScale = Screen.width / 800f; } // Update is called once per frame void Update () { //得到屏幕觸控?cái)?shù)組 touches = Input.touches; if (touches.Length > 0)//如果觸點(diǎn)開啟 { //得到離搖桿中心最近的觸點(diǎn)下標(biāo) touch_Id; if (touches.Length == 1)//只有一個觸點(diǎn)時 { touch_Id = 0; } else if (touches.Length > 1)//觸點(diǎn)大于1個時 { touch_Id = 0;//先假設(shè)下標(biāo)為0 for (int i = 1; i < touches.Length; i++)//遍歷觸點(diǎn)數(shù)組 { if (Vector2.SqrMagnitude(touches[i].position - m_originalPos) < Vector2.SqrMagnitude(touches[touch_Id].position - m_originalPos))//第i個點(diǎn)比假設(shè)的點(diǎn)近 { touch_Id = i;//假設(shè)的點(diǎn)改為第i個點(diǎn) } } } //如果得到的觸點(diǎn)不是取消或抬起 if (Input.GetTouch(touch_Id).phase != TouchPhase.Canceled && Input.GetTouch(touch_Id).phase != TouchPhase.Ended) { //觸點(diǎn)在搖桿范圍內(nèi) if(Vector2.SqrMagnitude(touches[touch_Id].position - m_originalPos) <= 50*50 * m_ScreenScale * m_ScreenScale)//50為背景半徑 { isMove = true;//開啟遙控 //搖桿開始控制,計(jì)算偏移量 SetOffetIn(); } else if(isMove)//觸點(diǎn)在搖桿范圍外,但是遙控已經(jīng)開啟 { SetOffetOut(); } } else// 手指抬起,搖桿回歸原始位置 { transform.position = m_originalPos; m_offet = Vector2.zero; isMove = false; touch_Id = -1; } } } /// <summary> /// 觸點(diǎn)在操作盤內(nèi)時 /// 搖桿控制方法 /// </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> /// 觸點(diǎn)在操作盤外時 /// 搖桿控制方法 /// </summary> void SetOffetOut() { Vector2 tempDir;//臨時偏移向量 tempDir = touches[touch_Id].position - m_originalPos; //更新?lián)u桿位置:距離原始位置127各單位 GetComponent<Image>().rectTransform.position = m_originalPos + (tempDir.normalized) * 25*m_ScreenScale; //偏移量 m_offet = tempDir.normalized;//歸一化 } private void OnGUI() { GUIStyle style = new GUIStyle(); //實(shí)例化一個新的GUIStyle,名稱為style ,后期使用 style.fontSize = 50; //字體的大小設(shè)置數(shù)值越大,字越大,默認(rèn)顏色為黑色 style.normal.textColor = new Color(1, 1, 1); //設(shè)置文本的顏色為 新的顏色(0,0,0)修改值-代表不同的顏色,值為整數(shù) 我個人覺得有點(diǎn)像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), "觸點(diǎn)位置:" + touches[touch_Id].position.ToString(), style); GUI.Label(new Rect(20, 240, 300, 60), "屏幕分辨率:" + Screen.currentResolution, style); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Unity?百度AI實(shí)現(xiàn)Logo商標(biāo)識別
本文主要介紹了Unity實(shí)現(xiàn)檢測和識別圖片中的品牌LOGO信息。即對于輸入的一張圖片(可正常解碼,且長寬比適宜),輸出圖片中LOGO的名稱、位置和置信度。需要的可以參考一下2022-01-01C#實(shí)現(xiàn)壓縮圖片為可控制的JPEG格式
這篇文章主要為大家詳細(xì)介紹了使用C#實(shí)現(xiàn)將圖片壓縮為質(zhì)量可自己控制的JPEG的幾種方式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-01-01C#實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出任一Word圖表的通用呈現(xiàn)方法
應(yīng)人才測評產(chǎn)品的需求,導(dǎo)出測評報(bào)告是其中一個重要的環(huán)節(jié),報(bào)告的文件類型也多種多樣,其中WORD輸出也扮演了一個重要的角色,本文給大家介紹了C#實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出任一Word圖表的通用呈現(xiàn)方法及一些體會,需要的朋友可以參考下2023-10-10C#連接SQL Server數(shù)據(jù)庫的實(shí)例講解
在本篇文章里小編給大家整理了關(guān)于C#連接SQL Server數(shù)據(jù)庫的實(shí)例內(nèi)容,有需要的朋友們參考學(xué)習(xí)下。2020-01-01C#開發(fā)WinForm項(xiàng)目實(shí)現(xiàn)HTML編輯器
這篇文章介紹了C#開發(fā)WinForm項(xiàng)目實(shí)現(xiàn)HTML編輯器的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06C#中括號強(qiáng)轉(zhuǎn)、as、is區(qū)別詳解
本文主要介紹了C#中括號強(qiáng)轉(zhuǎn)、as、is區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02