using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//用ScrollCircle代替ScrollRect组件,达到在一个圆形范围内拖动的效果
public class ScrollCircle : ScrollRect {
protected float mRadius = 0f;
protected float mWidth, bWidth, contentWidth;
// Use this for initialization
protected override void Start () {
base.Start();
//bWidth 为整个摇杆宽度占画布宽度的比例
bWidth = (transform as RectTransform).anchorMax.x - (transform as RectTransform).anchorMin.x;
//contentWidth为摇块宽度占摇杆背景宽度的比例
contentWidth = content.anchorMax.x - content.anchorMin.x;
GetRadius();
}
// Update is called once per frame
void Update()
{
GetRadius();
}
public override void OnDrag(PointerEventData eventData)
{
base.OnDrag(eventData);
var contentPosition = this.content.anchoredPosition;
if (contentPosition.magnitude > mRadius)
{
contentPosition = contentPosition.normalized * (mRadius);
SetContentAnchoredPosition(contentPosition);
}
}
//获取摇块能够移动的范围
public void GetRadius()
{
//mWidth 为整个摇杆的宽度
mWidth = transform.GetComponentInParent<Canvas>().pixelRect.width * bWidth;
//mRadius 为摇块能够移动的最大距离
mRadius = mWidth * 0.5f * (1 - contentWidth);
}
}
网友评论