1. 程式人生 > >UGUI 虛擬搖桿的實現

UGUI 虛擬搖桿的實現

建2張 image,大小自己看著辦:
這裡寫圖片描述

第一步:將上一次拖拽實現的程式碼全部複製過來.將指令碼放在較小的正方形 image

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Rocker : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler{

    public float radius = 100f;
    private RectTransform selfTransform;


    void
Start(){ } public void OnBeginDrag(PointerEventData eventData){ } public void OnDrag(PointerEventData eventData){ Vector3 pos; RectTransformUtility.ScreenPointToWorldPointInRectangle(selfTransform,eventData.position,eventData.pressEventCamera,out pos); selfTransform.position = pos; public
void OnEndDrag(PointerEventData eventData){ } }

我們來試一試效果
這裡寫圖片描述
很明顯,我們需要將圖片設定範圍,已經鬆開之後,小圖片應該回到原點.
我們先讓它回原點:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Rocker : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler{

    public
float radius = 100f; private RectTransform selfTransform; private Vector2 origin_position; void Start(){ selfTransform = GetComponent<RectTransform>(); //記住原點 origin_position = selfTransform.anchoredPosition; } public void OnBeginDrag(PointerEventData eventData){ } public void OnDrag(PointerEventData eventData){ Vector3 pos; RectTransformUtility.ScreenPointToWorldPointInRectangle(selfTransform,eventData.position,eventData.pressEventCamera,out pos); selfTransform.position = pos; } public void OnEndDrag(PointerEventData eventData){ //拖拽結束,回到原點 selfTransform.anchoredPosition = origin_position; } }

添加了4句話,再來看看效果:
這裡寫圖片描述

下步限定它的範圍:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Rocker : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler{

    public float radius = 100f;
    private RectTransform selfTransform;
    private Vector2 origin_position;
    void Start(){


        selfTransform = GetComponent<RectTransform>();
        //記住原點
        origin_position = selfTransform.anchoredPosition;
    }

    public void OnBeginDrag(PointerEventData eventData){
    }


    public void OnDrag(PointerEventData eventData){

        Vector3 pos;
        RectTransformUtility.ScreenPointToWorldPointInRectangle(selfTransform,eventData.position,eventData.pressEventCamera,out pos);
        selfTransform.position = pos;
        Vector2 touchAxis = selfTransform.anchoredPosition - origin_position;
        if(touchAxis.magnitude >= radius)
        {
            touchAxis = touchAxis.normalized * radius;
            selfTransform.anchoredPosition = touchAxis;
        }

    }
    public void OnEndDrag(PointerEventData eventData){
        //拖拽結束,回到原點
        selfTransform.anchoredPosition = origin_position;
    }
}

再次添加了4句話,看看效果:
這裡寫圖片描述

我們添加個物體來移動看看,程式碼需修改一下,為了方便我直接使用靜態變數:

using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Rocker : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler{

    public float radius = 100f;
    private RectTransform selfTransform;
    private Vector2 origin_position;
    public static Vector2 touchAxis;
    public static bool is_ready_move = false;
    void Start(){


        selfTransform = GetComponent<RectTransform>();
        //記住原點
        origin_position = selfTransform.anchoredPosition;
    }

    public void OnBeginDrag(PointerEventData eventData){
        is_ready_move = true;
    }


    public void OnDrag(PointerEventData eventData){

        Vector3 pos;
        RectTransformUtility.ScreenPointToWorldPointInRectangle(selfTransform,eventData.position,eventData.pressEventCamera,out pos);
        selfTransform.position = pos;
        touchAxis = selfTransform.anchoredPosition - origin_position;
        if(touchAxis.magnitude >= radius)
        {
             touchAxis = touchAxis.normalized * radius;
            selfTransform.anchoredPosition = touchAxis;
        }

    }
    public void OnEndDrag(PointerEventData eventData){
        //拖拽結束,回到原點
        selfTransform.anchoredPosition = origin_position;
        is_ready_move = false;
    }
}

然後掛在物體上的程式碼:


public class Unit : MonoBehaviour {

    public float speed =20f;
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        if (Rocker.is_ready_move)
        {
            transform.Translate(new Vector3(Rocker.touchAxis.normalized.x,0,Rocker.touchAxis.normalized.y)*Time.deltaTime*speed);

        }

    }
}

看看效果
這裡寫圖片描述

我們發現,滑鼠點選的情況下,移動到中間,物體還在移動,這裡相信你們自己就可以搞定了.