Unity之使用ScrollRect制作游戏摇杆

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Csoap2/article/details/100719153

在这里插入图片描述

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;
	}

	public override void OnDrag (UnityEngine.EventSystems.PointerEventData eventData)
	{
		base.OnDrag (eventData);
		var contentPostion = this.content.anchoredPosition;

        //contentPostion.magnitude 计算滑动摇杆的长度,需要让摇杆保持在圆形区域内
        if (contentPostion.magnitude > mRadius){
			contentPostion = contentPostion.normalized * mRadius; //contentPostion.normalized表示摇杆的单位向量,contentPostion:最终位置
            SetContentAnchoredPosition(contentPostion);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/Csoap2/article/details/100719153
今日推荐