這篇文章主要為大家展示了unity如何實現手游虛擬搖桿,內容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。
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;//搖桿原始屏幕坐標 Touch[] touches;//屏幕上觸控點數組 int touch_Id = -1;//觸控點數組下標 bool isMove = false;//是否移動 float m_ScreenScale; /// <summary> /// 給外部調用的偏移向量,告知搖桿參數 /// </summary> 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; } } } /// <summary> /// 觸點在操作盤內時 /// 搖桿控制方法 /// </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> /// 觸點在操作盤外時 /// 搖桿控制方法 /// </summary> void SetOffetOut() { Vector2 tempDir;//臨時偏移向量 tempDir = touches[touch_Id].position - m_originalPos; //更新搖桿位置:距離原始位置127各單位 GetComponent<Image>().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<Image>().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); } }
以上就是關于unity如何實現手游虛擬搖桿的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。