Unity ScrollRect实现游戏摇杆

在这里插入图片描述
移动ScrollRect组件

using UnityEngine;
using UnityEngine.UI;
 
public class Script_05_05 :ScrollRect
{
	protected float mRadius=0f;

	protected override void Start()
	{
		base.Start();
		//计算摇杆块的半径
		mRadius = (transform as RectTransform).sizeDelta.x * 0.5f; //各锚点之间的距离,相当于宽度
        Debug.Log(mRadius);
	}

	public override void OnDrag (UnityEngine.EventSystems.PointerEventData eventData)
	{
		base.OnDrag (eventData);
		var contentPostion = this.content.anchoredPosition; //content距锚点(中心)的距离
		if (contentPostion.magnitude > mRadius){
			contentPostion = contentPostion.normalized * mRadius ;
			SetContentAnchoredPosition(contentPostion);
		}
	}
}
发布了67 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Icecoldless/article/details/103725208
今日推荐