[Unity3d]基于UGUI写的Joystick
2017-01-02 14:59
232 查看
翻蛮牛看到一篇ugui实现摇杆的文章,我在它的基础上做了扩展,能够像EasyTouch一样派发 begin,moving,end 事件,
并且 在按住摇杆的情况下也能持续调用moving 事件。
参考:http://www.manew.com/thread-98879-1-1.html
JoyStick.cs代码:
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.EventSystems;
public class JoyStickArgs : EventArgs
{
public Vector3 dir;
public JoyStickArgs(Vector3 _dir)
{
dir = _dir;
}
}
public delegate void JoyStickHandler(object o, JoyStickArgs e);
public class JoyStick : MonoBehaviour ,IDragHandler , IBeginDragHandler , IEndDragHandler{
public event JoyStickHandler moveHandler;
public event JoyStickHandler beginHandler;
public event JoyStickHandler endHandler;
private static JoyStick mIns;
public RectTransform viewPivot;
public RectTransform pot;
private float radius;
private Vector3 offset = Vector3.zero;
private bool hasMoving = false;
void Awake()
{
mIns = this;
}
void OnEnable()
{
radius = viewPivot.sizeDelta.x * 0.5f;
hasMoving = false;
offset = Vector3.zero;
}
void OnDisable()
{
// todo:
}
void Update()
{
if(hasMoving)
{
if (moveHandler != null)
moveHandler(this, new JoyStickArgs(offset.normalized));
}
}
// get instance
public static JoyStick Inst()
{
return mIns;
}
// rigister listener
public void RigisterJoyStickHandler(JoyStickHandler _onBeginHandler = null, JoyStickHandler _onMoveHandler = null, JoyStickHandler _onEndHandler = null)
{
if (_onBeginHandler != null)
beginHandler += _onBeginHandler;
if (_onMoveHandler != null)
moveHandler += _onMoveHandler;
if (_onEndHandler != null)
endHandler += _onEndHandler;
}
public void UnRigisterJoyStickHandler(JoyStickHandler _onBeginHandler = null, JoyStickHandler _onMoveHandler = null, JoyStickHandler _onEndHandler = null)
{
if (_onBeginHandler != null)
beginHandler -= _onBeginHandler;
if (_onMoveHandler != null)
moveHandler -= _onMoveHandler;
if (_onEndHandler != null)
endHandler -= _onEndHandler;
}
public void OnDrag(PointerEventData data)
{
//limit radius
if (pot.anchoredPosition.magnitude > radius)
pot.anchoredPosition = pot.anchoredPosition.normalized * radius;
offset = pot.anchoredPosition3D;
if (offset.x < 1.0f && offset.x > -1.0f)
offset.x = 0.0f;
if (offset.y < 1.0f && offset.y > -1.0f)
offset.y = 0.0f;
}
public void OnBeginDrag(PointerEventData data)
{
hasMoving = true;
if (beginHandler != null)
beginHandler(this,new JoyStickArgs(offset.normalized));
}
public void OnEndDrag(PointerEventData data)
{
hasMoving = false;
if (endHandler != null)
endHandler(this, new JoyStickArgs(offset.normalized));
}
}
JoyStick 继承了 3个DragHandler 用于判断操作状态.
自定义了 一个 delegate 用于调用注册的物体事件.
摇杆UI的动作 都是用的ScrollRect 本身的特性,因此实现摇杆的代码非常简短,无需专门的代码控制摇杆的图片运动。
gif:
并且 在按住摇杆的情况下也能持续调用moving 事件。
参考:http://www.manew.com/thread-98879-1-1.html
JoyStick.cs代码:
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.EventSystems;
public class JoyStickArgs : EventArgs
{
public Vector3 dir;
public JoyStickArgs(Vector3 _dir)
{
dir = _dir;
}
}
public delegate void JoyStickHandler(object o, JoyStickArgs e);
public class JoyStick : MonoBehaviour ,IDragHandler , IBeginDragHandler , IEndDragHandler{
public event JoyStickHandler moveHandler;
public event JoyStickHandler beginHandler;
public event JoyStickHandler endHandler;
private static JoyStick mIns;
public RectTransform viewPivot;
public RectTransform pot;
private float radius;
private Vector3 offset = Vector3.zero;
private bool hasMoving = false;
void Awake()
{
mIns = this;
}
void OnEnable()
{
radius = viewPivot.sizeDelta.x * 0.5f;
hasMoving = false;
offset = Vector3.zero;
}
void OnDisable()
{
// todo:
}
void Update()
{
if(hasMoving)
{
if (moveHandler != null)
moveHandler(this, new JoyStickArgs(offset.normalized));
}
}
// get instance
public static JoyStick Inst()
{
return mIns;
}
// rigister listener
public void RigisterJoyStickHandler(JoyStickHandler _onBeginHandler = null, JoyStickHandler _onMoveHandler = null, JoyStickHandler _onEndHandler = null)
{
if (_onBeginHandler != null)
beginHandler += _onBeginHandler;
if (_onMoveHandler != null)
moveHandler += _onMoveHandler;
if (_onEndHandler != null)
endHandler += _onEndHandler;
}
public void UnRigisterJoyStickHandler(JoyStickHandler _onBeginHandler = null, JoyStickHandler _onMoveHandler = null, JoyStickHandler _onEndHandler = null)
{
if (_onBeginHandler != null)
beginHandler -= _onBeginHandler;
if (_onMoveHandler != null)
moveHandler -= _onMoveHandler;
if (_onEndHandler != null)
endHandler -= _onEndHandler;
}
public void OnDrag(PointerEventData data)
{
//limit radius
if (pot.anchoredPosition.magnitude > radius)
pot.anchoredPosition = pot.anchoredPosition.normalized * radius;
offset = pot.anchoredPosition3D;
if (offset.x < 1.0f && offset.x > -1.0f)
offset.x = 0.0f;
if (offset.y < 1.0f && offset.y > -1.0f)
offset.y = 0.0f;
}
public void OnBeginDrag(PointerEventData data)
{
hasMoving = true;
if (beginHandler != null)
beginHandler(this,new JoyStickArgs(offset.normalized));
}
public void OnEndDrag(PointerEventData data)
{
hasMoving = false;
if (endHandler != null)
endHandler(this, new JoyStickArgs(offset.normalized));
}
}
JoyStick 继承了 3个DragHandler 用于判断操作状态.
自定义了 一个 delegate 用于调用注册的物体事件.
摇杆UI的动作 都是用的ScrollRect 本身的特性,因此实现摇杆的代码非常简短,无需专门的代码控制摇杆的图片运动。
gif:
相关文章推荐
- 基于Unity3D(UGUI)的背包系统<二>
- Unity3D基于UGUI的虚拟摇杆实现
- 基于Unity3D(UGUI)的背包系统(装备系统,锻造系统,购买系统)
- 基于Unity3D(UGUI)的背包系统<三>
- 基于Unity3D(UGUI)的背包系统<四>完结篇
- unity3D(基于UGUI)网络游戏正在开发中 <一>"最近用UGUI做人物血条"
- Unity3D 基于UGUI的图文混排组件
- [置顶] unity3d基于ugui的ui模块
- 【Unity3d】UGUI小贴士:使用不规则按钮
- Unity3D之UGUI——利用Camera与RawImage组件制作3D游戏小地图
- Unity3D之UGUI——制作纸牌类游戏加入房间的输入面板
- 使用Unity原生方法进行录音即使播放基于UGUI
- 基于Unity3d的第三人称射击类游戏开发——创建游戏角色
- 基于Unity3D的循环路径的实现
- 【Unity3D_UGUI速成班】——07.Scrollbar
- Unity3D-UGUI无限滚动加载TableView列表
- Unity3D 05-理解Unity的新GUI系统(UGUI)
- Unity3D -- (UGUI)Toggle
- 基于Unity3D的相机功能的实现(一)——VR相机
- Unity3d-UGUI中Butten随鼠标的移动点击而移动