ScrollRect是UGUI提供的基础拖动组件,给它的Content绑定一个滑块就能工作了。如图

基于Unity3D ScrollRect的游戏摇杆实现

代码如下:


using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Example : ScrollRect {
    protected float mRadius = 0f;
    protected override void Start()
    {
        base.Start();
        mRadius = (transform as RectTransform).sizeDelta.x * 0.5f;
    }
    public override void OnDrag(PointerEventData eventData)
    {
        base.OnDrag(eventData);
        var contentPosition = this.content.anchoredPosition;
        if (contentPosition.magnitude > mRadius)
        {
            contentPosition = contentPosition.normalized * mRadius;
            SetContentAnchoredPosition(contentPosition);
        }
    }
    private void Update()
    {
        Debug.Log(content.anchoredPosition/ mRadius);
    }
}

 

相关文章:

  • 2021-12-10
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2021-09-16
  • 2021-11-27
  • 2022-12-23
  • 2021-09-16
猜你喜欢
  • 2022-01-23
  • 2022-02-02
  • 2022-12-23
  • 2022-02-15
  • 2021-08-07
  • 2022-12-23
  • 2021-10-28
相关资源
相似解决方案