您好,登錄后才能下訂單哦!
一.首先點擊UI創建兩個Image,將Image的Source Image改成自己想要的Texture即可,然后在Canvas下創建一空物體,將兩個Image放在空物體下作為他的子對象;
然后為可以拖動的Image編寫腳本,腳本如下:(有注釋,就不多說了)
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems; public class JoyStick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler//需要注意繼承的接口,接口內的方法需要實現 { /// <summary> /// 搖桿最大半徑 /// 以像素為單位 /// </summary> public float JoyStickRadius = 50; /// <summary> /// 搖桿重置所訴 /// </summary> public float JoyStickResetSpeed = 5.0f; /// <summary> /// 當前物體的Transform組件 /// </summary> private RectTransform selfTransform; /// <summary> /// 是否觸摸了虛擬搖桿 /// </summary> private bool isTouched = false; /// <summary> /// 虛擬搖桿的默認位置 /// </summary> private Vector2 originPosition; /// <summary> /// 虛擬搖桿的移動方向 /// </summary> private Vector2 touchedAxis; public Vector2 TouchedAxis { get { if (touchedAxis.magnitude < JoyStickRadius) return touchedAxis.normalized / JoyStickRadius; return touchedAxis.normalized; } } /// <summary> /// 定義觸摸開始事件委托 /// </summary> public delegate void JoyStickTouchBegin(Vector2 vec); /// <summary> /// 定義觸摸過程事件委托 /// </summary> /// <param name="vec">虛擬搖桿的移動方向</param> public delegate void JoyStickTouchMove(Vector2 vec); /// <summary> /// 定義觸摸結束事件委托 /// </summary> public delegate void JoyStickTouchEnd(); /// <summary> /// 注冊觸摸開始事件 /// </summary> public event JoyStickTouchBegin OnJoyStickTouchBegin; /// <summary> /// 注冊觸摸過程事件 /// </summary> public event JoyStickTouchMove OnJoyStickTouchMove; /// <summary> /// 注冊觸摸結束事件 /// </summary> public event JoyStickTouchEnd OnJoyStickTouchEnd; void Start() { //初始化虛擬搖桿的默認方向 selfTransform = this.GetComponent<RectTransform>(); originPosition = selfTransform.anchoredPosition; } public void OnPointerDown(PointerEventData eventData) { isTouched = true; touchedAxis = GetJoyStickAxis(eventData); if (this.OnJoyStickTouchBegin != null) this.OnJoyStickTouchBegin(TouchedAxis); } public void OnPointerUp(PointerEventData eventData) { isTouched = false; selfTransform.anchoredPosition = originPosition; touchedAxis = Vector2.zero; if (this.OnJoyStickTouchEnd != null) this.OnJoyStickTouchEnd(); } public void OnDrag(PointerEventData eventData) { touchedAxis = GetJoyStickAxis(eventData); if (this.OnJoyStickTouchMove != null) this.OnJoyStickTouchMove(TouchedAxis); } void Update() { //當虛擬搖桿移動到最大半徑時搖桿無法拖動 //為了確保被控制物體可以繼續移動 //在這里手動觸發OnJoyStickTouchMove事件 if (isTouched && touchedAxis.magnitude >= JoyStickRadius) { if (this.OnJoyStickTouchMove != null) this.OnJoyStickTouchMove(TouchedAxis); } //松開虛擬搖桿后讓虛擬搖桿回到默認位置 if (selfTransform.anchoredPosition.magnitude > originPosition.magnitude) selfTransform.anchoredPosition -= TouchedAxis * Time.deltaTime * JoyStickResetSpeed; } /// <summary> /// 返回虛擬搖桿的偏移量 /// </summary> /// <returns>The joy stick axis.</returns> /// <param name="eventData">Event data.</param> private Vector2 GetJoyStickAxis(PointerEventData eventData) { //獲取手指位置的世界坐標 Vector3 worldPosition; if (RectTransformUtility.ScreenPointToWorldPointInRectangle(selfTransform, eventData.position, eventData.pressEventCamera, out worldPosition)) selfTransform.position = worldPosition; //獲取搖桿的偏移量 Vector2 touchAxis = selfTransform.anchoredPosition - originPosition; //搖桿偏移量限制 if (touchAxis.magnitude >= JoyStickRadius) { touchAxis = touchAxis.normalized * JoyStickRadius; selfTransform.anchoredPosition = touchAxis; } return touchAxis; } }
二.接下來,就是拖拽p_w_picpath使物體能夠移動了;
為該物體添加腳本:
using UnityEngine; using System.Collections; public class JoyStick3D : MonoBehaviour { private JoyStick js; void Start() { js = GameObject.FindObjectOfType<JoyStick>(); js.OnJoyStickTouchBegin += OnJoyStickBegin; js.OnJoyStickTouchMove += OnJoyStickMove; js.OnJoyStickTouchEnd += OnJoyStickEnd; } void OnJoyStickBegin(Vector2 vec) { Debug.Log("開始觸摸虛擬搖桿"); } void OnJoyStickMove(Vector2 vec) { Debug.Log("正在移動虛擬搖桿"); //設置角色朝向 Quaternion q = Quaternion.LookRotation(new Vector3(vec.x, 0, vec.y)); transform.rotation = q; //移動角色 transform.Translate(Vector3.forward * 75f * Time.deltaTime); } void OnJoyStickEnd() { Debug.Log("觸摸移動搖桿結束"); } void OnGUI() { GUI.Label(new Rect(30, 30, 200, 30), "3D模式下的虛擬搖桿測試"); } }
三.接著可以試著運行,可以看到物體可以隨著p_w_picpath的拖動而移動,但是有不好的地方,就是拖拽的p_w_picpath超過半徑所設置的半徑JoyStickRadius時,p_w_picpath的位置就會固定在一個位置不動,
而且當在半徑范圍內時,物體不會持續移動,這個就不好了,不知道有那位大神能夠邦杰呢?
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。