Unity3d+moba+UGUI摇杆
2016-05-03 03:47
896 查看
1 功能描述
使用UGUI制作摇杆,摇杆的初始位置是可变的,当按下同时改变Bg位置和thumb位置,当松手时Bg,thumb返回初始原位。主要函数:ScreenPointToLocalPointInRectangle,Vector3.Distance。2 详细设计
Bg为摇杆的大圆背景,thumb为中心小圆,JoyCollider为可触发碰撞区(可设置大小,只有按在此区域才能触发摇杆的操作)
2.1 主要类成员
public GameObject m_objJoyCollider;//碰撞区域 public GameObject m_objThumb;//中心小圆 public GameObject m_objBg;//背景大圆 public Canvas can;//主要为获取画布上的摄像机 private float m_limitBg;//大圆移动限制 private float m_limitThumb;//小圆移动限制 public GameObject m_cube; public bool m_bMoving;//可用作防在摇杆区域多点触控 public Vector3 m_dir;//方向供外部调用摇杆的方向 public Touch m_touch;//保存按下时触摸点信息
2.2 当按在触摸区域时
void onDownCollider(GameObject obj) { m_bMoving = true; RectTransform rect = transform as RectTransform; Vector3 newPos; if (Application.platform == UnityEngine.RuntimePlatform.WindowsEditor) { newPos = uiPosGet(Input.mousePosition, rect); } else { m_touch = Input.touches[Input.touches.Length - 1]; newPos = uiPosGet(m_touch.position, rect); } float dis = Vector3.Distance(newPos, new Vector3(0,0,0));//鼠标与中心距离 if (dis <= m_limitBg) { newPos = newPos*dis/m_limitBg; } else { Vector3 normalPos = newPos.normalized; newPos = normalPos*m_limitBg*1.5f; } m_objBg.transform.localPosition = newPos; StartCoroutine("yieldStickMove"); }
1.得到触摸坐标转化为相对ui父节点的localPostion
2.设置Bg的位置,注意位置有限制,不能让这个大圆的部分超过屏幕边缘。
3.开启协程
2.3 手指移动时
private IEnumerator yieldStickMove() { while (true) { RectTransform rect = m_objBg.GetComponent<RectTransform>(); Vector3 realTouchPos = Vector3.zero; if (Application.platform == UnityEngine.RuntimePlatform.WindowsEditor) { realTouchPos = uiPosGet(Input.mousePosition, rect); } else { for (int i = 0; i < Input.touchCount; i++ ) { if ( m_touch.fingerId == Input.touches[i].fingerId) { realTouchPos = uiPosGet(Input.touches[i].position, rect); } } } float dis = Vector3.Distance(new Vector3(0,0,0), realTouchPos); if (dis <= m_limitThumb) { Vector3 normalPos = realTouchPos.normalized; m_objThumb.transform.localPosition = realTouchPos; } else { Vector3 normalPos = realTouchPos.normalized; realTouchPos = normalPos * m_limitThumb; m_objThumb.transform.localPosition = realTouchPos; } m_dir = m_objThumb.transform.localPosition.normalized; Vector3 cubePos = m_cube.transform.position; m_cube.transform.LookAt(new Vector3(cubePos.x + m_dir.x, cubePos.y, cubePos.z + m_dir.y)); m_cube.transform.Translate(Vector3.forward * Time.deltaTime * 70); yield return null; } }
1.thumb坐标相对bg坐标超过限制时与没超过时处理
2.得到thumb的localPostion的移动偏移的标准向量作为摇杆的传出信息
2.3当手指松开始时
void onUpCollider(GameObject obj) { StopCoroutine("yieldStickMove"); m_objBg.transform.localPosition = new Vector3(0,0,0); m_objThumb.transform.localPosition = new Vector3(0,0,0); m_bMoving = false; }
Ui回到初始位置。
3 运行效果
相关文章推荐
- [KBEngine]安装及第一次启动
- [置顶] 在unity中Vuforia与EsayCodeScanner在移动平台的使用
- Unity3d之Mecanim(新版动画系统)
- Unity3d面试3
- 菜逼的Unity学习笔记(二)
- 【Unity】11.8 关节
- 【Unity】11.7 布料
- 【Unity】11.6 恒定力 (Constant Force)
- 【Unity】11.5 物理材质 (Physics Material)
- 【Unity】11.4 车轮碰撞体(Wheel Collider)
- 【Unity】11.3 基本碰撞体(箱体、球形、胶囊、网格)
- 【Unity】11.2 刚体(Rigidbody)
- unity3d中的lua脚本异常捕获
- 【Unity】11.1 角色控制器 (Character Controller)
- 【Unity】第11章 第三人称角色控制器和碰撞体
- Unity C# 填充算法
- 菜逼的Unity学习笔记(一)
- 2016年4月面试题(Unity)
- unity中使用WWW类动态加载pvr格式纹理
- unity3d EventTrigger的作用