Unity实现简单虚拟摇杆
2020-04-15 12:03
148 查看
本文实例为大家分享了Unity虚拟摇杆的简单实现代码,供大家参考,具体内容如下
简单的Unity虚拟摇杆实现,有详细注释。
Game界面
Inspector界面
摇杆脚本
public class YaoGanCtrl : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public RectTransform diPan; public RectTransform anNiu; public Vector2 direction; Vector2 startPos; public float moveRange; public void OnBeginDrag(PointerEventData eventData) { //获取中心按钮的初始位置 startPos = anNiu.position; } public void OnDrag(PointerEventData eventData) { //计算摇杆方向 Vector2 newDir = eventData.position - startPos; //计算活动范围的半径 float r = Mathf.Clamp(newDir.magnitude, -moveRange, moveRange); //获取摇杆的单位方向 direction = newDir.normalized; //设置中心按钮位置 anNiu.position = startPos + direction * r; } public void OnEndDrag(PointerEventData eventData) { //重置中心按钮位置 anNiu.position = startPos; //重置单位方向 direction = Vector2.zero; } }
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- Unity实现简单的虚拟摇杆
- Unity基于NGUI的简单并可直接使用的虚拟摇杆实现(一)
- Unity中虚拟摇杆的简单实现
- Unity手游之路<五>虚拟摇杆之Unity内置插件实现
- Unity实现虚拟摇杆
- unity虚拟摇杆的实现
- Unity手游之路<五>虚拟摇杆之Unity内置插件实现
- unity中虚拟摇杆的实现
- Unity手游之路<五>虚拟摇杆之Unity内置插件实现
- Unity手游之路<五>虚拟摇杆之Unity内置插件实现
- UGUI实现Unity虚拟摇杆
- Unity_虚拟摇杆的实现_065
- Unity实现虚拟摇杆效果
- Unity2D - 4. 实现android虚拟摇杆控制人物移动
- Unity 虚拟摇杆之Unity内置插件实现
- unity 2D 透明遮罩 简单实现物体一半透明
- nginx虚拟主机三种模式的简单实现
- 高通Vuforia+Unity——实现简单的AR功能
- Unity 学习笔记 - 简单实现 用时分秒显示时间