Unity搖桿制作的方法
本文實例為大家分享了Unity搖桿制作方法的具體代碼,供大家參考,具體內(nèi)容如下
一.UI制作
1.首先創(chuàng)建一個空物體,命名為搖桿,錨點調(diào)至左下角
2.創(chuàng)建一個image作為搖桿廚盆的有效地方,并命名為tough_place
3.創(chuàng)建一個image放入搖桿的背景圖片,并命名為bg。
4.創(chuàng)建一個image放入搖桿的控制點,并命名為point。
5.把tough_place的顏色設(shè)置為透明
二.代碼編寫
創(chuàng)建腳本命名為PEListener,并引入相關(guān)接口,并進(jìn)行封裝。
using System; using UnityEngine; using UnityEngine.EventSystems; public class PEListener : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler { public Action<PointerEventData> onClickDown;//鼠標(biāo)按下時觸發(fā)的事件 public Action<PointerEventData> onClickUp;//鼠標(biāo)抬起時觸發(fā)的事件 public Action<PointerEventData> onClickDrag;//鼠標(biāo)按下時觸發(fā)的事件 public void OnPointerDown(PointerEventData eventData) { if (onClickDown != null) { onClickDown(eventData); } } public void OnPointerUp(PointerEventData eventData) { if (onClickUp != null) { onClickUp(eventData); } } public void OnDrag(PointerEventData eventData) { if (onClickDrag!=null) { onClickDrag(eventData); } } }
創(chuàng)建腳本命名為Test,進(jìn)行相關(guān)的邏輯操作。
(1)定義相關(guān)數(shù)據(jù)并傳入
using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class Test : MonoBehaviour { //輪盤UI public Image imageTough;//輪盤觸摸區(qū)域 public Image imgDir;//輪盤背景 public Image imaPoint;//輪盤操作點 //輪盤相關(guān)數(shù)據(jù) Vector2 startPos;//輪盤按下的坐標(biāo) Vector2 defulPos;//輪盤按下的坐標(biāo) float poinDis=90;// 輪盤中心和邊緣的最大距離(數(shù)據(jù)自己測試) private void Start() { defulPos = imageTough.transform.position; //由于我們的Canvas選擇的是Scale with Screen Size,且Match為1,也就是UI縮放受高度影響到大,具體數(shù)據(jù)見下圖 //poinDis實際大小=poinDis *縮放 poinDis = Screen.height * 1.0f / 1334 * poinDis; } }
(2)進(jìn)行相關(guān)邏輯的書寫
using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class Test : MonoBehaviour { //輪盤UI public Image imageTough;//輪盤觸摸區(qū)域 public Image imgDir;//輪盤背景 public Image imaPoint;//輪盤操作點 //輪盤相關(guān)數(shù)據(jù) Vector2 startPos;//輪盤按下的坐標(biāo) Vector2 defulPos;//輪盤按下的坐標(biāo) float poinDis=90;// 輪盤中心和邊緣的最大距離(數(shù)據(jù)自己測試) private void Start() { defulPos = imageTough.transform.position; //由于我們的Canvas選擇的是Scale with Screen Size,且Match為1,也就是UI縮放受高度影響到大,具體數(shù)據(jù)見下圖 //poinDis實際大小=poinDis *縮放 poinDis = Screen.height * 1.0f / 1334 * poinDis; RegisterTouchEvts(); } public void RegisterTouchEvts() { PEListener lister = imageTough.gameObject.AddComponent<PEListener>(); lister.onClickDown = (PointerEventData evt) => { //鼠標(biāo)按下時設(shè)置輪盤背景位置 startPos = evt.position; imgDir.transform.position = evt.position; }; lister.onClickUp = (PointerEventData evt) => { //鼠標(biāo)抬起時設(shè)置輪盤背景位置和輪盤操作點為默認(rèn)位置 imgDir.transform.position = defulPos; imaPoint.transform.localPosition = Vector2.zero; }; lister.onClickDrag = (PointerEventData evt) => { //計算是否超過輪盤中心和邊緣的最大距離 Vector2 dir = evt.position - startPos; float len = dir.magnitude; //超過了,限制為最大位置 if (len > poinDis) { Vector2 clampDir = Vector2.ClampMagnitude(dir, poinDis); imaPoint.transform.position = startPos + clampDir; } else { imaPoint.transform.position = evt.position; } }; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#二進(jìn)制讀寫B(tài)inaryReader、BinaryWriter、BinaryFormatter
這篇文章介紹了C#二進(jìn)制讀寫B(tài)inaryReader、BinaryWriter、BinaryFormatter的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06C#用正則表達(dá)式Regex.Matches 方法檢查字符串中重復(fù)出現(xiàn)的詞
使用正則表達(dá)式用Regex類的Matches方法,可以檢查字符串中重復(fù)出現(xiàn)的詞,Regex.Matches方法在輸入字符串中搜索正則表達(dá)式的所有匹配項并返回所有匹配,本文給大家分享C#正則表達(dá)式檢查重復(fù)出現(xiàn)的詞,感興趣的朋友一起看看吧2024-02-02