Unity UGUI摇杆两种方式实现

Unity UGUI摇杆两种方式实现

方式1:继承UI事件接口

using GameFramework.UI.UGUI;
using UnityEngine;

public class TestBackGround : MonoBehaviour
{
    private UIListener _uiListenerBackGround;
    private GameObject _image1;
    private GameObject _image2;
    private Vector2 _startPos;
    private Vector2 _currPos;
    [SerializeField] private float radius;
    [SerializeField] private float speed;
    private bool _isMove = false;

    void Start()
    {
        _image1 = transform.GetChild(0).gameObject;
        _image2 = _image1.transform.GetChild(0).gameObject;


        _uiListenerBackGround = gameObject.AddComponent<UIListener>();
        _uiListenerBackGround.BindGameObject(gameObject);


        _uiListenerBackGround.BeginDrag += (eventData) =>
        {
            _isMove = true;
            _image1.SetActive(true);
            _image1.transform.position = eventData.position;
            _startPos = eventData.position;
        };

        _uiListenerBackGround.Drag += (eventData) => { _currPos = eventData.position; };

        _uiListenerBackGround.EndDrag += (eventData) =>
        {
            _isMove = false;
            _image1.SetActive(false);
            _startPos = Vector2.zero;
            _currPos = Vector2.zero;
        };
    }

    private void Update()
    {
        if (!_isMove) return;
        _image2.transform.localPosition = _currPos - _startPos;
        if (_image2.transform.localPosition.magnitude > radius)
            _image2.transform.localPosition = _image2.transform.localPosition.normalized * radius;

        //使操作杆向鼠标位置移动
        if (Vector2.Distance(_currPos, _image1.transform.position) > 0.1f)
        {
            _image1.transform.position =
                Vector2.MoveTowards(_image1.transform.position, _currPos, speed * Time.deltaTime);
            if (_image1.transform.localPosition.magnitude > 300f)
            {
                _image1.transform.localPosition = _image1.transform.localPosition.normalized * 300f;
            }
        }
    }
}

方式2:继承UI基类ScrollRect

using UnityEngine;
using UnityEngine.UI;

public class Joystick : ScrollRect
{
    private float radius = 0f;
    public Vector3 stickPos;

    protected override void Start()
    {
        base.Start();
        radius = viewRect.rect.width / 2;
    }

    private void Update()
    {
        if (content.localPosition.magnitude > radius)
            content.localPosition = content.localPosition.normalized * radius;
        stickPos = content.localPosition.normalized;
    }
}

参考链接:https://www.cnblogs.com/csymaet/p/9532008.html

你可能感兴趣的:(Unity,unity,游戏引擎)