一.首先點擊UI創(chuàng)建兩個Image,將Image的Source Image改成自己想要的Texture即可,然后在Canvas下創(chuàng)建一空物體,將兩個Image放在空物體下作為他的子對象;
創(chuàng)新互聯(lián)專注于懷來企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),商城建設(shè)。懷來網(wǎng)站建設(shè)公司,為懷來等地區(qū)提供建站服務(wù)。全流程按需規(guī)劃網(wǎng)站,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)然后為可以拖動的Image編寫腳本,腳本如下:(有注釋,就不多說了)
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems; public class JoyStick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler//需要注意繼承的接口,接口內(nèi)的方法需要實現(xiàn) { ////// 搖桿大半徑 /// 以像素為單位 /// public float JoyStickRadius = 50; ////// 搖桿重置所訴 /// public float JoyStickResetSpeed = 5.0f; ////// 當前物體的Transform組件 /// private RectTransform selfTransform; ////// 是否觸摸了虛擬搖桿 /// private bool isTouched = false; ////// 虛擬搖桿的默認位置 /// private Vector2 originPosition; ////// 虛擬搖桿的移動方向 /// private Vector2 touchedAxis; public Vector2 TouchedAxis { get { if (touchedAxis.magnitude < JoyStickRadius) return touchedAxis.normalized / JoyStickRadius; return touchedAxis.normalized; } } ////// 定義觸摸開始事件委托 /// public delegate void JoyStickTouchBegin(Vector2 vec); ////// 定義觸摸過程事件委托 /// /// 虛擬搖桿的移動方向 public delegate void JoyStickTouchMove(Vector2 vec); ////// 定義觸摸結(jié)束事件委托 /// public delegate void JoyStickTouchEnd(); ////// 注冊觸摸開始事件 /// public event JoyStickTouchBegin OnJoyStickTouchBegin; ////// 注冊觸摸過程事件 /// public event JoyStickTouchMove OnJoyStickTouchMove; ////// 注冊觸摸結(jié)束事件 /// public event JoyStickTouchEnd OnJoyStickTouchEnd; void Start() { //初始化虛擬搖桿的默認方向 selfTransform = this.GetComponent(); 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() { //當虛擬搖桿移動到大半徑時搖桿無法拖動 //為了確保被控制物體可以繼續(xù)移動 //在這里手動觸發(fā)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; } /// /// 返回虛擬搖桿的偏移量 /// ///The joy stick axis. /// Event data. 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(); js.OnJoyStickTouchBegin += OnJoyStickBegin; js.OnJoyStickTouchMove += OnJoyStickMove; js.OnJoyStickTouchEnd += OnJoyStickEnd; } void OnJoyStickBegin(Vector2 vec) { Debug.Log("開始觸摸虛擬搖桿"); } void OnJoyStickMove(Vector2 vec) { Debug.Log("正在移動虛擬搖桿"); //設(shè)置角色朝向 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("觸摸移動搖桿結(jié)束"); } void OnGUI() { GUI.Label(new Rect(30, 30, 200, 30), "3D模式下的虛擬搖桿測試"); } }
三.接著可以試著運行,可以看到物體可以隨著p_w_picpath的拖動而移動,但是有不好的地方,就是拖拽的p_w_picpath超過半徑所設(shè)置的半徑JoyStickRadius時,p_w_picpath的位置就會固定在一個位置不動,
而且當在半徑范圍內(nèi)時,物體不會持續(xù)移動,這個就不好了,不知道有那位大神能夠邦杰呢?
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。